How do I check if the three last characters of a variable match another variable?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite












I have %var1% and %var2%



var1="UserInput"
var2=file.log


If a user types %var1% as "file.txt", how can I (if possible) check only the three last characters (Or the file format if that's possible) to see if it matches %var2%'s three last characters or file format?



Another method that does the same thing is also welcome (But the user-input variable must be there)










share|improve this question









New contributor




Tom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    4
    down vote

    favorite












    I have %var1% and %var2%



    var1="UserInput"
    var2=file.log


    If a user types %var1% as "file.txt", how can I (if possible) check only the three last characters (Or the file format if that's possible) to see if it matches %var2%'s three last characters or file format?



    Another method that does the same thing is also welcome (But the user-input variable must be there)










    share|improve this question









    New contributor




    Tom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      I have %var1% and %var2%



      var1="UserInput"
      var2=file.log


      If a user types %var1% as "file.txt", how can I (if possible) check only the three last characters (Or the file format if that's possible) to see if it matches %var2%'s three last characters or file format?



      Another method that does the same thing is also welcome (But the user-input variable must be there)










      share|improve this question









      New contributor




      Tom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I have %var1% and %var2%



      var1="UserInput"
      var2=file.log


      If a user types %var1% as "file.txt", how can I (if possible) check only the three last characters (Or the file format if that's possible) to see if it matches %var2%'s three last characters or file format?



      Another method that does the same thing is also welcome (But the user-input variable must be there)







      windows-7 command-line batch script environment-variables






      share|improve this question









      New contributor




      Tom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Tom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 24 mins ago









      phuclv

      8,47663488




      8,47663488






      New contributor




      Tom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 13 hours ago









      Tom

      235




      235




      New contributor




      Tom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Tom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Tom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          5
          down vote













          How do I check if the three last characters of a variable match another variable?



          Use variable substring syntax to extract the last 3 characters of each variable.



          Example batch file:



          @echo off
          setlocal
          set /p var1="UserInput: "
          set var1last3=%var1:~-3%
          set var2=file.log
          set var2last3=%var2:~-3%
          if %var1last3% == %var2last3% (
          echo Match
          ) else (
          echo No Match
          )
          endlocal



          Further Reading



          • An A-Z Index of the Windows CMD command line | SS64.com

          • Windows CMD Commands (categorized) - Windows CMD - SS64.com

          • If - Conditionally perform command - Windows CMD - SS64.com

          • Set - Environment Variable - Windows CMD - SS64.com

          • variable substring - Windows CMD - SS64.com





          share|improve this answer






















          • It doesn't affect your answer's functionality, but why have you set enabledelayedexpansion when you don't use any !var! expansions?
            – AFH
            12 hours ago










          • @AFH Force of habit. I've removed it to avoid any confusion:)
            – DavidPostill♦
            10 hours ago


















          up vote
          4
          down vote













          If you are going to check a fixed number of characters, then you should check the last 4, else "file.Log" will match "file.myLog".



          But the best option is to get the actual extension, regardless the length:



          @echo off
          setlocal
          set "var1=file.Log"
          set "var2=file.myLog"

          :: Option 1 using FOR loops
          for %%A in ("%var1%") do for %%B in ("%var2%") do if "%%~xA"=="%%~xB" (
          echo Match
          ) else (
          echo No Match
          )

          : Option 2 using CALL parameters
          call :matchExtension "%var1%" "%var2%" &&echo Match||echo No Match
          exit /b

          :matchExtension file1 fil2 --> returns 0 (success) if match, 1 (error) if no match
          if "%~x1"=="%~x2" exit /b 0
          exit /b 1





          share|improve this answer




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "3"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );






            Tom is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1368721%2fhow-do-i-check-if-the-three-last-characters-of-a-variable-match-another-variable%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            5
            down vote













            How do I check if the three last characters of a variable match another variable?



            Use variable substring syntax to extract the last 3 characters of each variable.



            Example batch file:



            @echo off
            setlocal
            set /p var1="UserInput: "
            set var1last3=%var1:~-3%
            set var2=file.log
            set var2last3=%var2:~-3%
            if %var1last3% == %var2last3% (
            echo Match
            ) else (
            echo No Match
            )
            endlocal



            Further Reading



            • An A-Z Index of the Windows CMD command line | SS64.com

            • Windows CMD Commands (categorized) - Windows CMD - SS64.com

            • If - Conditionally perform command - Windows CMD - SS64.com

            • Set - Environment Variable - Windows CMD - SS64.com

            • variable substring - Windows CMD - SS64.com





            share|improve this answer






















            • It doesn't affect your answer's functionality, but why have you set enabledelayedexpansion when you don't use any !var! expansions?
              – AFH
              12 hours ago










            • @AFH Force of habit. I've removed it to avoid any confusion:)
              – DavidPostill♦
              10 hours ago















            up vote
            5
            down vote













            How do I check if the three last characters of a variable match another variable?



            Use variable substring syntax to extract the last 3 characters of each variable.



            Example batch file:



            @echo off
            setlocal
            set /p var1="UserInput: "
            set var1last3=%var1:~-3%
            set var2=file.log
            set var2last3=%var2:~-3%
            if %var1last3% == %var2last3% (
            echo Match
            ) else (
            echo No Match
            )
            endlocal



            Further Reading



            • An A-Z Index of the Windows CMD command line | SS64.com

            • Windows CMD Commands (categorized) - Windows CMD - SS64.com

            • If - Conditionally perform command - Windows CMD - SS64.com

            • Set - Environment Variable - Windows CMD - SS64.com

            • variable substring - Windows CMD - SS64.com





            share|improve this answer






















            • It doesn't affect your answer's functionality, but why have you set enabledelayedexpansion when you don't use any !var! expansions?
              – AFH
              12 hours ago










            • @AFH Force of habit. I've removed it to avoid any confusion:)
              – DavidPostill♦
              10 hours ago













            up vote
            5
            down vote










            up vote
            5
            down vote









            How do I check if the three last characters of a variable match another variable?



            Use variable substring syntax to extract the last 3 characters of each variable.



            Example batch file:



            @echo off
            setlocal
            set /p var1="UserInput: "
            set var1last3=%var1:~-3%
            set var2=file.log
            set var2last3=%var2:~-3%
            if %var1last3% == %var2last3% (
            echo Match
            ) else (
            echo No Match
            )
            endlocal



            Further Reading



            • An A-Z Index of the Windows CMD command line | SS64.com

            • Windows CMD Commands (categorized) - Windows CMD - SS64.com

            • If - Conditionally perform command - Windows CMD - SS64.com

            • Set - Environment Variable - Windows CMD - SS64.com

            • variable substring - Windows CMD - SS64.com





            share|improve this answer














            How do I check if the three last characters of a variable match another variable?



            Use variable substring syntax to extract the last 3 characters of each variable.



            Example batch file:



            @echo off
            setlocal
            set /p var1="UserInput: "
            set var1last3=%var1:~-3%
            set var2=file.log
            set var2last3=%var2:~-3%
            if %var1last3% == %var2last3% (
            echo Match
            ) else (
            echo No Match
            )
            endlocal



            Further Reading



            • An A-Z Index of the Windows CMD command line | SS64.com

            • Windows CMD Commands (categorized) - Windows CMD - SS64.com

            • If - Conditionally perform command - Windows CMD - SS64.com

            • Set - Environment Variable - Windows CMD - SS64.com

            • variable substring - Windows CMD - SS64.com






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 10 hours ago

























            answered 12 hours ago









            DavidPostill♦

            101k25213249




            101k25213249











            • It doesn't affect your answer's functionality, but why have you set enabledelayedexpansion when you don't use any !var! expansions?
              – AFH
              12 hours ago










            • @AFH Force of habit. I've removed it to avoid any confusion:)
              – DavidPostill♦
              10 hours ago

















            • It doesn't affect your answer's functionality, but why have you set enabledelayedexpansion when you don't use any !var! expansions?
              – AFH
              12 hours ago










            • @AFH Force of habit. I've removed it to avoid any confusion:)
              – DavidPostill♦
              10 hours ago
















            It doesn't affect your answer's functionality, but why have you set enabledelayedexpansion when you don't use any !var! expansions?
            – AFH
            12 hours ago




            It doesn't affect your answer's functionality, but why have you set enabledelayedexpansion when you don't use any !var! expansions?
            – AFH
            12 hours ago












            @AFH Force of habit. I've removed it to avoid any confusion:)
            – DavidPostill♦
            10 hours ago





            @AFH Force of habit. I've removed it to avoid any confusion:)
            – DavidPostill♦
            10 hours ago













            up vote
            4
            down vote













            If you are going to check a fixed number of characters, then you should check the last 4, else "file.Log" will match "file.myLog".



            But the best option is to get the actual extension, regardless the length:



            @echo off
            setlocal
            set "var1=file.Log"
            set "var2=file.myLog"

            :: Option 1 using FOR loops
            for %%A in ("%var1%") do for %%B in ("%var2%") do if "%%~xA"=="%%~xB" (
            echo Match
            ) else (
            echo No Match
            )

            : Option 2 using CALL parameters
            call :matchExtension "%var1%" "%var2%" &&echo Match||echo No Match
            exit /b

            :matchExtension file1 fil2 --> returns 0 (success) if match, 1 (error) if no match
            if "%~x1"=="%~x2" exit /b 0
            exit /b 1





            share|improve this answer
























              up vote
              4
              down vote













              If you are going to check a fixed number of characters, then you should check the last 4, else "file.Log" will match "file.myLog".



              But the best option is to get the actual extension, regardless the length:



              @echo off
              setlocal
              set "var1=file.Log"
              set "var2=file.myLog"

              :: Option 1 using FOR loops
              for %%A in ("%var1%") do for %%B in ("%var2%") do if "%%~xA"=="%%~xB" (
              echo Match
              ) else (
              echo No Match
              )

              : Option 2 using CALL parameters
              call :matchExtension "%var1%" "%var2%" &&echo Match||echo No Match
              exit /b

              :matchExtension file1 fil2 --> returns 0 (success) if match, 1 (error) if no match
              if "%~x1"=="%~x2" exit /b 0
              exit /b 1





              share|improve this answer






















                up vote
                4
                down vote










                up vote
                4
                down vote









                If you are going to check a fixed number of characters, then you should check the last 4, else "file.Log" will match "file.myLog".



                But the best option is to get the actual extension, regardless the length:



                @echo off
                setlocal
                set "var1=file.Log"
                set "var2=file.myLog"

                :: Option 1 using FOR loops
                for %%A in ("%var1%") do for %%B in ("%var2%") do if "%%~xA"=="%%~xB" (
                echo Match
                ) else (
                echo No Match
                )

                : Option 2 using CALL parameters
                call :matchExtension "%var1%" "%var2%" &&echo Match||echo No Match
                exit /b

                :matchExtension file1 fil2 --> returns 0 (success) if match, 1 (error) if no match
                if "%~x1"=="%~x2" exit /b 0
                exit /b 1





                share|improve this answer












                If you are going to check a fixed number of characters, then you should check the last 4, else "file.Log" will match "file.myLog".



                But the best option is to get the actual extension, regardless the length:



                @echo off
                setlocal
                set "var1=file.Log"
                set "var2=file.myLog"

                :: Option 1 using FOR loops
                for %%A in ("%var1%") do for %%B in ("%var2%") do if "%%~xA"=="%%~xB" (
                echo Match
                ) else (
                echo No Match
                )

                : Option 2 using CALL parameters
                call :matchExtension "%var1%" "%var2%" &&echo Match||echo No Match
                exit /b

                :matchExtension file1 fil2 --> returns 0 (success) if match, 1 (error) if no match
                if "%~x1"=="%~x2" exit /b 0
                exit /b 1






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 12 hours ago









                dbenham

                7,48631828




                7,48631828




















                    Tom is a new contributor. Be nice, and check out our Code of Conduct.









                     

                    draft saved


                    draft discarded


















                    Tom is a new contributor. Be nice, and check out our Code of Conduct.












                    Tom is a new contributor. Be nice, and check out our Code of Conduct.











                    Tom is a new contributor. Be nice, and check out our Code of Conduct.













                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1368721%2fhow-do-i-check-if-the-three-last-characters-of-a-variable-match-another-variable%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    Long meetings (6-7 hours a day): Being “babysat” by supervisor

                    Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                    Confectionery