Looping batchfile with counter

-1

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

BondUniverse

Posted 2014-05-02T14:19:28.480

Reputation: 455

-1 for editing the question to remove the original question. – Scott – 2019-03-08T20:45:56.760

Updated it for current version, based on @JoeNahmias sample, but seems that I can't fully read the variables in the code or create a temp incremental value within the loop (Prompting for next numbered CD...). – BondUniverse – 2014-05-02T17:54:17.293

Most importantly though, the 'Set FileName=%PathName%%BookName% CD%%N.mp3' line comes out blank... – BondUniverse – 2014-05-02T17:54:59.567

Answers

2

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

JoeNahmias

Posted 2014-05-02T14:19:28.480

Reputation: 262

If I use the following, I get an error of

"CDNumTot) was unexpected at this time."

, FOR /L %I IN (1, 1, %CDNumTot%) DO ( @echo %I ECHO Next step1 CD%I ECHO Next step1 CD%I ), – BondUniverse – 2014-05-02T14:37:47.137

Sorry, don't know how to enter code in a comment so it shows up right... – BondUniverse – 2014-05-02T14:38:48.483

@BondUniverse can you run the first commands that i gave from the command prompt and see if that works? – JoeNahmias – 2014-05-02T14:56:31.193

@BondUniverse, you have to use TWO percent signs for the loop variable inside a batch file – JoeNahmias – 2014-05-02T14:57:38.440

With that latest edit, I get a value of "n" instead of a number... – BondUniverse – 2014-05-02T15:59:37.900

@BondUniverse please post your current code as an edit on the question – JoeNahmias – 2014-05-02T16:05:05.357

Updated my original code and put comments on what isn't working. – BondUniverse – 2014-05-02T17:55:52.233

That WORKED! I'm going to post my slightly tweaked version in my original note. – BondUniverse – 2014-05-02T21:08:16.987