Looping Batchfile with Counter

Looping Batchfile with Counter

Ok, I know the loop part is invalid, but hopefully helps give an idea of what I want to do. Ultimately, I want to get prompted for the 3 values (commented out the prompts for now and hard coding for ease of testing), then I want it to run through the loop once per CD (so 8 times in this example). I want it to name the .mp3 file with an incrementing CD#... CD1, CD2, CD3, etc. If anyone has any improvements, those are welcome too! I'm posting my full code here to share with the community.

@ECHO OFF

REM set /p BookName="Book Name:  "
REM set /p AuthorName="Author Name:  "
REM set /p CDNumber="Number of CDs:  "

set BookName=Victory and Honor
set AuthorName=W.E.B. Griffin
set CDNumTot=8

set PathName=C:\Rip\%AuthorName%\%BookName%
mkdir "C:\Rip\%AuthorName%\%BookName%\"
REM cd C:\Program Files (x86)\freac

ECHO CD Count:  %CDNumTot%

FOR /L %%N IN (1, 1, %CDNumTot%) DO (
  Set FileName=%PathName%\%BookName% CD%%N.mp3
  ECHO Filename:  %FileName%
  ECHO\
  ECHO Ripping CD# %%N...
  ECHO freaccmd -track all -o %FileName%
  ECHO CLS
  ECHO\
  ECHO\
  set /A NextCD=%%N
  ECHO Next:  %NextCD
  ECHO Please Eject CD and insert next CD# %NextCD%...
  pause
)

Final version of this code (WORKING thanks to @JoeNahmias):

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

set /p BookName="Book Name:  "
set /p AuthorName="Author Name:  "
set /p CDNumTot="Total number of CDs:  "

set PathName=C:\Rip\%AuthorName%\%BookName%


FOR /L %%N IN (1, 1, %CDNumTot%) DO (
  cls
  ECHO\
  ECHO Please insert CD# %%N...
  ECHO\
  pause
  SET FileName=%PathName%\%BookName% CD%%N.mp3
  cls
  ECHO\
  ECHO Ripping CD# %%N...
  ECHO\
  "C:\Program Files (x86)\freac\freaccmd" -track all -o "!FileName!"
  REM You need the id3 executable downloaded/installed for the next line:
  REM id3 -l "!BookName!" -a "!AuthorName!" -n %%N "!FileName!"
  rundll32 user32.dll,MessageBeep -1
  ECHO\
  ECHO Please eject CD# %%N...
  pause
)

cls
ECHO\
ECHO DONE!
pause
3

1 Answer

You want the FOR /L command. For example:

C:\>SET num=7
C:\>FOR /L %I IN (1, 1, %num%) DO @echo %I
1
2
3
4
5
6
7

Note that you have to double the lead percent sign in a batch file. So, for your CD ripping loop:

FOR /L %%n IN (1, 1, %CDNumTot%) DO (
  SET FileName=%PathName%\%BookName% CD %%n.mp3
  ...
)

If you're having difficulty, try putting the following in a batch file and seeing if you get it working:

@ECHO OFF
SET num=7
FOR /L %%I IN (1, 1, %num%) DO @echo %%I

Try this for a complete solution:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

REM set /p BookName="Book Name:  "
REM set /p AuthorName="Author Name:  "
REM set /p CDNumTot="Total number of CDs:  "

set BookName=Victory and Honor
set AuthorName=W.E.B. Griffin
set CDNumTot=8

set PathName=C:\Rip\%AuthorName%\%BookName%
REM mkdir "C:\Rip\%AuthorName%\%BookName%\"
REM cd C:\Program Files (x86)\freac

ECHO CD Count:  %CDNumTot%

FOR /L %%N IN (1, 1, %CDNumTot%) DO (
  ECHO Please Eject CD and insert next CD# %%N...
  pause
  SET FileName=%PathName%\%BookName% CD%%N.mp3
  ECHO Filename:  !FileName!
  ECHO.
  ECHO Ripping CD# %%N...
  ECHO freaccmd -track all -o "!FileName!"
  ECHO CLS
  ECHO.
  ECHO.
)
8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Robert Thorne
Author

Robert Thorne

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.