![提示在 For 循环内输入](https://linux22.com/image/1587259/%E6%8F%90%E7%A4%BA%E5%9C%A8%20For%20%E5%BE%AA%E7%8E%AF%E5%86%85%E8%BE%93%E5%85%A5.png)
我有一个目录,里面可以包含一百多个文件。我想创建一个循环,遍历该目录中的每个文件,并询问用户是否要处理下一个文件或完全退出脚本。我似乎无法让 For 循环内部出现提示。任何想法都将不胜感激。
@echo off
Color 1F
SetLocal EnableExtensions EnableDelayedExpansion
Echo File Processing Screen
Echo.
:UserPrompt1
Set /p "strContProc=Are you sure you wish to process these files? (Y/N) %=%"
If /i "%strContProc%" == "y" (
Goto ProcFiles
) Else If /i "%strContProc%" == "n" (
Goto ProcCancel1
) Else (
Goto UserPrompt1
)
:ProcFiles
If exist "%userprofile%\Desktop\PCExport\Disable" (
Set /a numTotFile = 0
Set /a numFileCount = 0
For /r %%f in (*.txt) do (
Set /a numTotFile += 1
)
) ELSE (
Goto DirNotFound
)
If %numTotFile% EQU 0 (
Goto NoFilesFound
) ELSE If %numTotFile% EQU 1 (
Echo Processing file !numFileCount! of %numTotFile%...
Goto EndProc
) ELSE (
Echo.
Echo %numTotFile% Files will now be processed.
Echo.
)
:ReturnProc
For /r %%f in (*.txt) do (
Set /a numFileCount += 1
Echo Processing file !numFileCount! of %numTotFile%. . .
Echo.
Echo !numFileCount! >> %%f
If !numFileCount! EQU %numTotFile% (
Echo !numFileCount! of %numTotFile% files have been processed.
Goto EndProc
)
:UserPrompt2
Set /p "strNextProc=Do you wish to process the next file? (Y/N) %=%"
If /i "%strNextProc%" == "y" (
Goto ReturnProc
) Else If /i "%strNextProc%" == "n" (
Goto ProcCancel2
) Else (
Goto UserPrompt2
)
)
Goto EndProc
:DirNotFound
Echo The specified processing directory could not be found!
Goto EndProc
:NoFilesFound
Echo Processing cannot continue. There were no export files found to process.
Goto EndProc
:ProcCancel1
Echo.
Echo Processing of files was canceled at the user's request.
Goto EndProc
:ProcCancel2
Echo.
Echo Processing of files was canceled at the user's request. Please make a note of the last file that was processed.
Goto EndProc
:EndProc
EndLocal
Echo.
Pause
答案1
GOTO
在循环体内发出会FOR
立即终止循环。
:subroutine
一个简单的解决方案是在包含的循环外部编写一个,然后在循环内部GOTO
调用。:subroutine
伪代码如下:
for ... in (...) do (
Possibly do some processing
call :userPrompt
Possibly do some more processing with !var!
)
exit /b
:userPrompt
set "var="
set /p "var=Your prompt
if ...InvalidInput... goto :userPrompt
exit /b