1.

Solve : substring question?

Answer»

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. 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 messageHi, RENO
please explain what is %%g mean? ThanksQuote from: Geek-9pm on April 18, 2009, 11:34:22 PM

Hi, reno
please explain what is %%g mean? Thanks

It is a small relative of your %%G

Quote from: Geek-9pm on April 18, 2009, 11:34:22 PM
Hi, reno
please explain what is %%g mean? Thanks
Quote from: Z.K. on April 18, 2009, 07:00:19 PM
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/?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.



Discussion

No Comment Found