我有一个 BATCH.BAT,当我运行它时,我需要询问我想要创建多少个文件夹:
echo How many folders you want? (enter below)
SET /P "ANSWER="
然后我需要输入数字(不大于 50 但大于 1),当我按回车键时,我需要在此文件夹中创建文件夹%~dp0..\batch\
。例如,如果我输入数字12这将被创建:
%~dp0..\batch\
|_____ 01
|
|_____ 02
|
|_____ 03
|
|_____ 04
|
|_____ 05
|
|_____ 06
|
|_____ 07
|
|_____ 08
|
|_____ 09
|
|_____ 10
|
|_____ 11
|
|_____ 12
当创建文件夹时我需要将其放入每个创建的文件夹中:
IF EXIST "%~dp0..\batch\01\" (
ROBOCOPY "%~dp0..\scripts" "%~dp0..\batch\01" "script.1s" /Z /B
BREAK>"%~dp0..\batch\01\t.ini"
BREAK>"%~dp0..\batch\01\k.txt"
BREAK>"%~dp0..\batch\01\s.txt" )
IF EXIST "%~dp0..\batch\02\" (
ROBOCOPY "%~dp0..\scripts" "%~dp0..\batch\02" "script.1s" /Z /B
BREAK>"%~dp0..\batch\02\t.ini"
BREAK>"%~dp0..\batch\02\k.txt"
BREAK>"%~dp0..\batch\02\s.txt" )
IF EXIST "%~dp0..\batch\03\" (
...
我如何才能根据输入的数字创建文件夹?
以及如何避免出现 49x IF EXIST "%~dp0..\batch\XY\" (
??
编辑:这是我的尝试:
rem @echo off
setlocal enabledelayedexpansion
:0001
echo How many folders you want? (enter below)
SET /P "ANSWER="
set ANSWER="%%F"
IF %ANSWER% LSS 2 GOTO :0001
IF %ANSWER% GTR 50 GOTO :0001
SET batch="%~dp0..\batch\"
SET max=25
SET min=2
FOR /L %%F IN (1,%max%,%min%) DO (
IF NOT EXIST "%batch%\0-%%F" (
md "%batch%\0-%%F")
)
答案1
您的尝试存在未解决的问题:
查找对于/l句法
10 以下的数字需要前导零(通过加 100 并取最后 2 位来解决)
- 该变量
%%F
仅在 for 命令范围内有效(同一行/代码块)
由于将代码放在被调用的子程序中并将数字作为参数传递,因此以下批处理不需要延迟扩展。
:: Q:\Test\2018\05\26\SU_1325998.cmd
@Echo off
SET min=2
SET max=50
:0001
Set "ANSWER="
echo How many folders do you want? (enter below)
SET /P "ANSWER="
If not defined ANSWER Exit /B
IF %ANSWER% LSS %min% (Echo %ANSWER% is not enaugh min=%min%& GOTO :0001 )
IF %ANSWER% GTR %max% (Echo %ANSWER% is too much max=%max%& GOTO :0001 )
FOR /L %%F IN (1,1,%ANSWER%) DO Call :Sub %%F
Echo Done
Pause
Goto :Eof
:Sub
Set /A "N=100 + %1"
SET "batch=%~dp0..\batch\%N:~-2%"
IF NOT EXIST "%batch%" md "%batch%" >NUL
:: IMO RoboCopy is overkill here
COPY "%~dp0..\scripts\script.1s" "%batch%" >NUL
for %%A in (t.ini k.txt s.txt) Do if not exist "%batch%\%%A" Break>"%batch%\%%A"
> SU_1325998.cmd
How many folders do you want? (enter below)
1
1 is not enaugh min=2
How many folders do you want? (enter below)
99
99 is too much max=50
How many folders do you want? (enter below)
3
Done
Drücken Sie eine beliebige Taste . . .
> tree \ /F
├───batch
│ ├───01
│ │ k.txt
│ │ s.txt
│ │ script.1s
│ │ t.ini
│ │
│ ├───02
│ │ k.txt
│ │ s.txt
│ │ script.1s
│ │ t.ini
│ │
│ └───03
│ k.txt
│ s.txt
│ script.1s
│ t.ini
│
├───scripts
│ script.1s
│
└───test
SU_1325998.cmd