我有以下MS Windows批处理文件代码:
@echo off
cd\
dir/s *.docx *.xlsx *.ppt *.docx
SET /p input= %data% "
copy "%data%" C:\abc\
pause
此命令显示所有4种类型的扩展名列表,但是我想从用户那里获取输入,然后复制到所需的位置。
我错过了什么?
答案 0 :(得分:0)
您的主要错误是将批处理文件的用户输入的字符串分配给环境变量input
,但是在命令copy
上引用了根本没有定义的环境变量data
,因此,在执行命令行之前,%data%
被替换两次。
使用此批次代码怎么样?
@echo off
rem The next 2 commands are commented out as not needed for this task.
rem cd \
rem dir /s *.doc *.docx *.xlsx *.ppt
setlocal EnableExtensions EnableDelayedExpansion
:UserPrompt
echo/
echo Enter the file name with complete path or drag and drop
echo the file to copy from Windows Explorer over this window.
echo/
set "FileName=""
set /p "FileName=Enter file name: "
echo/
rem Remove all double quotes from entered string.
set "FileName=!FileName:"=!"
rem Has the user entered any file name at all?
if "!FileName!" == "" goto UserPrompt
rem Does the file really exist?
if exist "!FileName!" goto CopyFile
echo Error: The specified file does not exist.
echo/
goto UserPrompt
:CopyFile
copy "!FileName!" C:\abc\
echo/
endlocal
pause
阅读Why is string comparison after prompting user for a string/option not working as expected?上的答案,了解此处使用的所有额外代码的解释。
要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。
copy /?
echo /?
endlocal /?
goto /?
if /?
pause /?
rem /?
set /?
setlocal /?