我创建了一个简单的批处理文件,用于将 drop 文件复制到特定文件夹中。这是我的批处理脚本:
rem This will create a BACK-UP DATA Folder in Desktop
rem with a SUB-FOLDER which filename must be the first 5 characters of drop file filename
rem with another folder filename: "COPY" inside (just for another back up)
for %%f in (%1) do (
set oldName=%%~nf
set newName=%oldName:~0,5%
mkdir %HOMEPATH%\Desktop\"BACK-UP DATA"\"%newName%"\
mkdir %HOMEPATH%\Desktop\"BACK-UP DATA"\"%newName%"\COPY
xcopy %%f "%HOMEPATH%\Desktop\BACK-UP DATA\%newName%"
rem will copy drop file also inside COPY folder
xcopy %%f "%HOMEPATH%\Desktop\BACK-UP DATA\%newName%\COPY"
rem open created folder
start "" "%HOMEPATH%\Desktop\BACK-UP DATA\%newName%\"
PAUSE
)
这是命令提示符中的输出...
set oldName="New Text Document"
set newName=~0,5
mkdir C:\User\username\Desktop\"BACK-UP DATA"\""\
mkdir C:\User\username\Desktop\"BACK-UP DATA"\""\COPY
xcopy "C:\User\username\Desktop\New Text Document.txt" "C:\User\username\Desktop\BACK-UP DATA\"
xcopy "C:\User\username\Desktop\New Text Document.txt" "C:\User\username\Desktop\BACK-UP DATA\\COPY"
start "" "C:\User\username\Desktop\BACK-UP DATA\\"
pause
变量为什么%newName%
不起作用?我的脚本出了什么问题?
答案1
如果你想声明变量并在 for 循环或 if 中使用相同的变量,你必须使用延缓扩张前任:
SetLocal EnableDelayedExpansion
你也必须%
用以下方法替代!
set "oldName=%%~nf"
set "newName=!oldName:~0,5!"
mkdir "%HOMEPATH%\Desktop\BACK-UP DATA\!newName!\COPY"