The idea is I want to check if a substring is inside of a string with Batch script.
For example I want to check if pass
is inside of %rev%
. If no for loop is related, I can do:
if /I "%rev:pass=%" neq "%rev%" (
echo String has pass
) else (
echo it doesnt has pass
)
But I don't know how to use this "%rev:pass=%"
in side of a for loop when "%rev%"
is replaced by %%g
My code is here:
FOR /F "tokens=*" %%g IN (test.txt) do (
if /I "%%g:pass=" neq "%%g" (
echo String has pass
) else (
echo it doesnt has pass
)
)
I need to check each string inside of this test.txt if it has substring pass
. Any help is appreciated.
I try to change the form of %%g
but still cannot make it happen. The main idea is to check each string in the file, if the string has pass
as a sub-string
CodePudding user response:
You can't manipulate for
loop variables directly, so you'll need to store them in regular variables first. However, since you can't ordinarily set and use variables inside of sets, you'll need to enable delayed expansion. This can be done by adding setlocal enabledelayedexpansion
to the top of your script.
@echo off
setlocal enabledelayedexpansion
FOR /F "tokens=*" %%g IN (test.txt) do (
set "line=%%g"
if /I "!line:pass=!" neq "%%g" (
echo String has pass
) else (
echo it doesnt has pass
)
)
CodePudding user response:
Try this. Will match if the case is UPPER or lower. It works by string replacement and comparison of before and after. Also gives a running hit count. Nifty.
setlocal EnableDelayedExpansion
FOR /F "delims= tokens=*" %%g IN (test.txt) do (
set "str1=%%g"
set "str2=!str1:pass=replace!"
if not !str1!==!str2! set /a i =1 & echo YES - FOUND !i!)
Upvote me! I need reputation!!
Oh also. the other answer is wrong. The way it's written the two values will NEVER be equal. Try it.