set statements don't appear to work in my batch file -
i've found appears explanation of problem here
dos batch: why set commands resulting in nothing getting stored?
but don't understand explanation.
here script...
for /r /d %%f in (\product\database\sql\project\model\scripts\*) ( rem echo %%f set !load_file_filter= %%f\*.sql echo file: %!load_file_filter% call %!batch_file% -u %!user_name% -p %!password% -s %!server_name% -d %!database_name% -f %!load_file_filter% -o %!log_file% if %!echoerrors%==1 ( echo [ type %!log_file% echo ] )
)
the echo prints file: *.sql , script pass var complains load_file_filter empty.
i have tried adding setlocal enabledelayedexpansion
suggested in article doesn't solve problem. echo file: %!load_file_filter%
prints last subdirectory in directory i'm running from. echo %%f
prints correct value.
what '!' behind variable for/to me?
on side note, explain me difference between
set !var , set var
%var& &!var& !var! %%var
we going start simple case
set "var=" set "var=test" echo %var%
reading code, removes content of variable, assigns new value , echoes it.
let's change bit concatenating last 2 commands
set "var=" set "var=test" & echo %var%
"same" code, in case output console not show value in variable.
why? in batch files, lines execute parsed , executed. during parse phase, every variable read operation (where retrieve value of variable) replaced value stored inside variable @ parse time. once done, resulting command executed. so, in previous sample when second line parsed, converted
set "var=test" & echo
now, there no read operations on line , no value echo, when line readed variable didn't hold value (it assigned when line executed) read operation has been replaced nothing. @ point, code executed , perceived behaviour set
command failed don't "obvious" value echoed console.
this behaviour found in blocks. block set of lines enclosed in parenthesis (usually for
, if
constructs) , handled parser if lines in block 1 line concatenated commands. full block readed, variable read operations removed , replaced value inside variables, , full block, no variable references inside executed.
at execution time there no read operation on variables inside block, initial values, so, value assigned variable inside block can not retrieved inside same block, there isn't read operation.
so, in code
set "test=before" if defined test ( set "test=after" echo %test% )
after first set
executed, block (the if
command , code enclosed in parenthesis) parsed , converted into
if defined test ( set "test=after" echo before )
showing "wrong" value.
the usual way deal use delayed expansion. allow change, needed, syntax read variable %var%
!var!
, indicating parser read operation must not removed @ parse time, delayed until command executed.
setlocal enabledelayedexpansion set "var=" set "var=test" & echo !var!
the third line converted @ parse time to
set "var=test" & echo !var!
yes, variable reference not removed. read operation delayed until echo
command executed, when value of variable has been changed.
so
%var%
variable reference replaced @ parse time
!var!
variable reference replaced @ execution time
%x
x
single character for
replaceable parameter, variable hold current element being interated. own nature, expanded @ execution time. syntax single percent sign used @ command line. inside batch files percent sign need escaped , syntax refer replaceable parameters %%x
Comments
Post a Comment