O script em lote termina cedo demais

1

Preciso de ajuda com o script de lote / CMD do Windows. Eu escrevi um script para gravar mic-in para um MP3 por 12 horas. Funciona bem ao executá-lo manualmente. Ele será executado com o agendador de tarefas também - apenas que, em vez de esperar por 12 horas, ele terminará após a última declaração.

O seguinte é o código,

    @ECHO OFF
    REM You will need to change the extension, and the verify the %DATE% format.

    REM XT= Extension, eg. 55109
    SET XT=55109

    REM TA= Sales Rep, eg. John Smith
    SET TA=John Smith

    REM TY= Year, eg. 2013
    SET TY=%DATE:~0,4%

    REM TM= Month, eg. 04
    SET TM=%DATE:~5,2%

    REM TD= Dat, eg. 29
    SET TD=%DATE:~8,2%

    REM TE= Time in 24 hour format, eg. 1730
    SET TE=%TIME:~0,2%%TIME:~3,2%

    REM FL= File location.
    SET FL=\Server2\Recording$

    REM TL= Album Title, no space.
    SET TL=ACME

    REM TV= Drive letter
    SET TV=P:

    REM Do not change anything below this line.
    SET BT=16
    SET SR=16
    SET CH=2
    SET RS=16000
    TASKLIST /FI "IMAGENAME eq lame.exe" 2> NUL | FIND /I /N "lame.exe" > NUL
    IF NOT "%ERRORLEVEL%" == "0" (
        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%
    ) ELSE (
        ECHO Warning: Recorder already started. 
        TASKLIST /FI "IMAGENAME eq lame.exe"
    )

    EXIT

Alguma ideia por favor? Muito obrigado!

    
por X503JMG 30.04.2013 / 03:19

1 resposta

1

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
    
por 16.05.2018 / 20:08