带计数器的循环批处理文件

带计数器的循环批处理文件

好的,我知道循环部分无效,但希望它能帮助我了解我想要做什么。最终,我希望得到 3 个值的提示(暂时注释掉提示并进行硬编码以方便测试),然后我希望它每张 CD 运行一次循环(因此在此示例中为 8 次)。我希望它使用递增的 CD# 命名 .mp3 文件...CD1、CD2、CD3 等。如果有人有任何改进,也欢迎!我在这里发布我的完整代码与社区分享。

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

该代码的最终版本(感谢@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

答案1

您需要FOR /L命令。例如:

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

请注意,您必须在批处理文件中将前导百分号加倍。因此,对于您的 CD 翻录循环:

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

如果您遇到困难,请尝试将以下内容放入批处理文件中,看看是否可以正常工作:

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

尝试以下方法以获得完整的解决方案:

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

相关内容