Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: substring question  (Read 3264 times)

0 Members and 1 Guest are viewing this topic.

Z.K.

    Topic Starter


    Newbie

    substring question
    « on: April 18, 2009, 07:00:19 PM »
    Within a Windows XP batch file I need to find a substring within a variable string and then do something if it is equal or not equal. Find and FindStr seem to only work with files so I was wondering just how to do this with a variable in an if statement.

     in the form :
       
          if %%G contains "command" (
           
              echo a message

          )

    I have not been able to figure out how to do this in a batch file.    ???

    Reno



      Hopeful
    • Thanked: 32
      Re: substring question
      « Reply #1 on: April 18, 2009, 10:29:22 PM »
      1. echo %%g|find "command" >nul 2>&1 && echo a message
      or
      echo %%g|find "command" >nul 2>&1
      if not errorlevel 1 echo a message

      2. i assume you are inside a for loop, and has setlocal enabledelayedexpansion
      set a=%%g
      set a=!a:command=!
      if "!a!" neq "%%g" echo a message

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: substring question
      « Reply #2 on: April 18, 2009, 11:34:22 PM »
      Hi, reno
      please explain what is %%g mean?  Thanks

      Dias de verano

      • Guest
      Re: substring question
      « Reply #3 on: April 19, 2009, 12:56:27 AM »
      Hi, reno
      please explain what is %%g mean?  Thanks

      It is a small relative of your %%G


      Reno



        Hopeful
      • Thanked: 32
        Re: substring question
        « Reply #4 on: April 19, 2009, 12:58:32 AM »
        Hi, reno
        please explain what is %%g mean?  Thanks
              if %%G contains "command" (
                  echo a message
              )
        there is indention in the above code, i assume he is inside a for loop, %%g refers to token variable. besides for loop, i can't think anywhere else in coding a %%g can be used.
        Code: [Select]
        for /f "tokens=1*" %%f in (sometextfile.txt) do (
                 echo %%g|find "command" >nul 2>&1 && echo a message
        )

        example text file:
        this is a command

        sample output:
        c:>\test.bat
        a message

        for complete explanation, type for/?

        Geek-9pm


          Mastermind
        • Geek After Dark
        • Thanked: 1026
          • Gekk9pm bnlog
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Windows 10
        Re: substring question
        « Reply #5 on: April 19, 2009, 05:17:16 PM »
        Thanks, It said:

        To use the FOR command in a batch program, specify %%variable instead
        of %variable.  Variable names are case sensitive, so %i is different
        from %I.


        It also said the variable is onl a single letter.