1

I need some help with Windows batch/CMD scripting. I wrote a script to record mic-in to a MP3 for 12 hours. It runs ok when running it manually. It will run with task scheduler too - just that, instead of waiting for 12 hours, it will terminate after the last statement.

The following is the code,

    @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

Any idea please? Thank you very much!

3
  • When running it manually, does the batch script exit before the recording is finished? Apr 30, 2013 at 5:04
  • Yup, when i ran it manually it was fine. The little CMD window remained there the whole time, 12 hours to be exact.
    – X503JMG
    May 1, 2013 at 1:54
  • Have you tried adding START /WAIT before %PR%\linco.exe? It may be required when running in a non-interactive mode.
    – Goyuix
    May 21, 2013 at 20:49

1 Answer 1

1

There are many resources on the described issue - for instance Variables in batch not behaving as expected:

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!
)

Another way without using setlocal enabledelayedexpansion: use call a subroutine, e.g. as follows:

@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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .