(c)中ToTheMaker
我在这里找到了这段代码,我将使用它,但唯一的问题是我希望它有一个用户输入来创建文件夹
例如:“输入文件夹数量:”
用户输入的值将用作创建文件夹的变量。我该怎么做?
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET groupsize=10
SET n=1
SET nf=0
FOR %%f IN (*.txt) DO (
IF !n!==1 (
SET /A nf+=1
MD Cake_!nf!
)
MOVE /Y "%%f" Cake_!nf!
IF !n!==!groupsize! (
SET n=1
) ELSE (
SET /A n+=1
)
)
ENDLOCAL
PAUSE
答案 0 :(得分:0)
在命令提示符窗口中执行help set
或set /?
会导致在控制台窗口中打印多个帮助页面以获取命令 SET ,应仔细阅读。该帮助还解释了set /P
提示用户输入值或字符串。
@echo off
set "FolderCount=1"
set /P "FolderCount=Enter number of folders (default: %FolderCount%): "
for /L %%N in (1,1,%FolderCount%) do md "Folder%%N"
set "FolderCount="
这个小批量代码定义了环境变量FolderCount
,其值为1作为默认值,当用户在提示符下按键 RETURN 或 ENTER 时使用
接下来询问用户要创建的文件夹数。用户输入的字符串将分配给环境变量FolderCount
。用户希望输入正数而不是不同的东西。
FOR 循环在当前目录中创建名称为Folder
的文件夹,并附加当前编号,在每个循环运行时从值1开始自动递增1。
最后一行删除不再需要的环境变量FolderCount
。
要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。
echo /?
for /?
md /?
set /?
编辑:移动文本文件的整个任务的最终批处理代码。
@echo off
rem Delayed expansion required for variable FolderIndex in FOR loop.
rem Command setlocal additionally creates a new environment variable
rem table with copying all existing variables to the new table.
setlocal EnableDelayedExpansion
rem Ask the batch user for number of subfolders to create and create them.
set "FolderCount=1"
set /P "FolderCount=Enter number of folders (default: %FolderCount%): "
for /L %%N in (1,1,%FolderCount%) do md "Folder%%N"
rem Move all *.txt files from current folder into the created subfolders.
set "FolderIndex=0"
for %%F in (*.txt) do (
set /A FolderIndex+=1
move /Y "%%~F" "Folder!FolderIndex!\%%~nxF"
if !FolderIndex! == %FolderCount% set "FolderIndex=0"
)
rem Restore previous environment which results in destroying
rem current environment table with FolderIndex and FolderCount.
endlocal