user888270
user888270

Reputation: 631

How to expand env var in command in for loop

I have a simple for loop and want to run a command for each file in dir in a script below. Whenever I run it I get this error "INST_PATH\bin\testBin.exe was unexpected at this time."

I can see it cant expand the env var. Also seems like there are problems in expanding %%i.

Also if I want to give a default value to a variable IF, not provided at command line, how do I do that? For e.g. if the user doesn't give the Dir and I want to assume it to be current dir how do I do that in the script?

set Dir=%1
set OutDir=%2
pushd %Dir%
for %i in (*.*) do %INST_PATH%\bin\testBin.exe -I=. --cpp_out=. %%i
popd

Thanks in advance for your help.

Upvotes: 1

Views: 49

Answers (1)

Stephan
Stephan

Reputation: 56154

Two questions, two answers:

within batchfiles, you have to use double percent-signs for the for variables:

for %%i in (*.*) do echo %%i

to set a default value, check if the variable is empty:

if "%1%=="" (set "dir=Z:\DefaultDir") else (set "dir=%1")

or as an alternative:

set dir=%1
if not defined dir set "dir=Z:\DefaultDir"

Upvotes: 1

Related Questions