Reputation: 45
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?
Upvotes: 0
Views: 172
Reputation: 34899
Remove the spaces around =
in all set
commands.
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.
Upvotes: 3