Existem muitos recursos sobre o problema descrito - por exemplo, As variáveis em lote não se comportam como o esperado :
You are not the first, who fell into the famous "delayed expansion trap" (and you won't be the last)
You need delayed expansion if you want to use a variable, that you changed in the same block (a block is a series of commands within brackets (and ))
Delayed variables are referenced with
!var!
instead of%var%
Reason is the way, cmd parses the code. A complete line or block is parsed at once, replacing normal variables with their value at parse time. Delayed variables are evaluated at runtime.
Two simple batches to demonstrate:
setlocal enabledelayedexpansion set "var=hello" if 1==1 ( set "var=world" echo %var% !var! )
.
for /L %%i in (1,1,5) do ( echo %random% !random! )
…
Outra forma de sem usar setlocal enabledelayedexpansion
: use call
uma sub-rotina , por exemplo da seguinte forma:
@ECHO OFF
REM You will need to change the extension, and the verify the %DATE% format.
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: unchanged code snippet here
::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
TASKLIST /FI "IMAGENAME eq lame.exe" 2> NUL | FIND /I /N "lame.exe" > NUL
IF NOT "%ERRORLEVEL%" == "0" (
CALL :runexe
) ELSE (
ECHO Warning: Recorder already started.
TASKLIST /FI "IMAGENAME eq lame.exe"
)
EXIT
:runexe
SET TH=%TY%%TM%
SET TT=%TY%%TM%%TD%
SET OU=P:\%TT%-%TE%-%XT%.mp3
SET PR="%PROGRAMFILES(X86)%\Recorder"
IF EXIST %TV%\ ( NET USE %TV% /DELETE /Y > NUL )
NET USE %TV% %FL% /PERSISTENT:NO > NUL
IF NOT EXIST %TV%\%XT% ( MKDIR %TV%\%XT% )
IF NOT EXIST %TV%\%XT%\%TH% ( MKDIR %TV%\%XT%\%TH% )
NET USE %TV% /DELETE /Y > NUL
NET USE %TV% %FL%\%XT%\%TH% /PERSISTENT:NO > NUL
%PR%\linco.exe -B %BT% -C %CH% -R %RS% -D 12:00:00 | %PR%\lame.exe -r -s %SR% -m s -a -q 5 -c --bitwidth %BT% --tt "%TT%"-"%XT%" --ta "%TA%" --tl %TL% --ty %TY% --tg Recording - -o %OU%
goto :eof