简体   繁体   中英

echo variable is not working in batch file

My batch file execution throws error at echo echo %outfvar%. The following is the batch file I wrote:

setlocal ENABLEDELAYEDEXPANSION
set /a incvar = 1
set outfvar = "outfile"_!incvar!".res"
echo !outfvar!
echo *.txt > !outfvar!
set /a incvar = incvar+1

FOR %%pat in (%*) do(
    FOR /F %%k in (!outfvar!) DO( grep -l !pat! !k! >>outfile_!incvar!.res)
    set /a incvar = incvar+1
    set outfvar = "outfile"_!incvar!.res
                     )

Error is "%pat was unexpected at this time.." Can anybody help me to execute this batch file successfully?

Remove the spaces around = in all set comands.

There must be a space in between do and ( in the line of for .

The line

set outfvar = "outfile"_%incvar%".res"

should read

set "outfvar=outfile_%incvar%.res"

(The quotes as you stated them were part of the string value.)

for variables must consist of one letter only and need to be expanded by preceding with %% . You are trying to use %%pat in your code, which will not work. State %%p instead (also inner for ).

Finally, you need delayed expansion to be able to read variables you modify within the same (compound) command, the for in your code. See this post to learn how it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM