執行在命令提示符窗口或者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
'help set',專門看看'set/p'選項 – leppie
我已經這樣做了,但事情是我不理解它。批處理文件中的新手。 –