在满足输入条件之前批处理文件退出

batch file exits before input criteria is satisfied

我创建了一个批处理文件以在资源管理器中右键单击并要求用户提供文件夹名称。如果该文件夹已经存在,我想一直询问用户,直到输入一个不存在的文件夹。如果文件夹已经存在,文件会在两次运行后退出,我不知道为什么会这样。在我看来它应该不断调用 main 直到它获得一个不存在的文件夹名称。请参阅下面的代码

set verbose=NO
@echo off

set mydir = %cd%
cd %mydir%
cls

call:main
echo returned from main
pause
goto:eof

:main
set /p job=Enter Job Name:
echo %job%
call:check
echo returned from check
if choice==1 call:main
echo returned from main
pause
if choice==2 call:createDirectory

:check
if exist %job% (echo Folder already exists set choice=1) else (set choice=2)
echo %choice%



:createDirectory
md "%job%"

md "%job%".Development
md "%job%".Development.Budgets
md "%job%".Development.Manufacturers

md "%job%"\"2.Plans and Specs"
md "%job%"\"2.Plans and Specs".Plans
md "%job%"\"2.Plans and Specs".Specs

md "%job%".Pricing
md "%job%".Pricing.Quotes
md "%job%".Pricing.Worksheets

md "%job%".Sumbittals
md "%job%".Sumbittals\"1.Submittal Reviews"

md "%job%".Orders
md "%job%".Orders\"1.Order Summaries"
md "%job%".Orders\"2.Confirmation Pages"

md "%job%".Closeout
md "%job%".Closeout\"1.O&M Manuals"
md "%job%".Closeout.Warranty
  1. check子例程不return调用者而是继续到批处理文件的末尾,在echo %choice%
  2. 之后添加exit /b
  3. main子程序中:在下一个标签前添加goto main使其循环
  4. 你正在递归调用 :main,使用不带冒号的 goto mainif choice==1 goto main 但实际上你不需要这一行,因为上面的 #2
  5. 使用&在一行中分隔命令:(echo Folder already exists & set choice=1)
  6. 使用%variable%得到它的值:if %choice%==2 call :createDirectory

这是 :main:createDirectory 之间更改的代码:

:main
set /p job=Enter Job Name:
call:check
if %choice%==2 call :createDirectory
goto main

:check
if exist %job% (echo Folder already exists & set choice=1) else (set choice=2)
exit /b