【发布时间】:2021-05-15 03:34:33
【问题描述】:
我对使用 robocopy 的构建后事件有疑问。该代码用于几天前工作,现在它在我的任何项目中都不起作用。下面是我用于构建后事件的代码,用于为同一应用的不同构建创建插件。
根据 Squashman 和 Mofi 的建议编辑:
echo Configuration: $(Configuration)
set "Dir2018=C:\Program Files\Rune\Altem 2018\Plugins\"
set "Dir2019=C:\Program Files\Rune\Altem 2019\Plugins\"
set "Dir2020=C:\Program Files\Rune\Altem 2020\Plugins\"
if $(Configuration) == Debug2018 goto 2018
if $(Configuration) == Debug2019 goto 2019
if $(Configuration) == Debug2020 goto 2020
:2018
echo Copying results to 2018
if not exist "%Dir2018%$(ProjectName)" mkdir "%Dir2018%$(ProjectName)"
robocopy $(TargetDir) "%Dir2018%$(ProjectName)" /XF "*.pdb" /E
if %errorlevel% leq 1 exit 0 else exit %errorlevel%
goto exit
:2019
echo Copying results to 2019
if not exist "%Dir2019%$(ProjectName)" mkdir "%Dir2019%$(ProjectName)"
robocopy "$(TargetDir)" "%Dir2019%$(ProjectName)" /XF "*.pdb" /E
if %errorlevel% leq 1 exit 0 else exit %errorlevel%
goto exit
:2020
echo Copying results to 2020
if not exist "%Dir2020%$(ProjectName)" mkdir "%Dir2020%$(ProjectName)"
robocopy $(TargetDir) "%Dir2020%$(ProjectName)" /XF "*.pdb" /E
if %errorlevel% leq 1 exit 0 else exit %errorlevel%
goto exit
:exit
错误显示在 Visual Studio 中:
Severity Code Description Project File Line Suppression State
Error The command "echo Configuration: Debug2019
set "Dir2018=C:\Program Files\Rune\Altem 2018\Plugins\"
set "Dir2019=C:\Program Files\Rune\Altem 2019\Plugins\"
set "Dir2020=C:\Program Files\Rune\Altem 2020\Plugins\"
if Debug2019 == Debug2018 goto 2018
if Debug2019 == Debug2019 goto 2019
if Debug2019 == Debug2020 goto 2020
:2018
echo Copying results to 2018
if not exist "%Dir2018%SnoopTool" mkdir "%Dir2018%SnoopTool"
robocopy C:\Users\username\OneDrive\_WORK FILES\_REPOS\nw_issuecreator\nw_issuecreator\SnoopTool\bin\Debug2019\ "%Dir2018%SnoopTool" /XF "*.pdb" /E
if %errorlevel% leq 1 exit 0 else exit %errorlevel%
goto exit
:2019
echo Copying results to 2019
if not exist "%Dir2019%SnoopTool" mkdir "%Dir2019%SnoopTool"
robocopy "C:\Users\username\OneDrive\_WORK FILES\_REPOS\nw_issuecreator\nw_issuecreator\SnoopTool\bin\Debug2019\" "%Dir2019%SnoopTool" /XF "*.pdb" /E
if %errorlevel% leq 1 exit 0 else exit %errorlevel%
goto exit
:2020
echo Copying results to 2020
if not exist "%Dir2020%SnoopTool" mkdir "%Dir2020%SnoopTool"
robocopy C:\Users\username\OneDrive\_WORK FILES\_REPOS\nw_issuecreator\nw_issuecreator\SnoopTool\bin\Debug2019\ "%Dir2020%SnoopTool" /XF "*.pdb" /E
if %errorlevel% leq 1 exit 0 else exit %errorlevel%
goto exit
:exit" exited with code 16. SnoopTool
Invalid Parameter #3 : "C:\Program Files\Rune\Altem 2019\Plugins\SnoopTool"
if not exist %Dir2019%$(ProjectName) mkdir %Dir2019%$(ProjectName) 行成功执行。如果目录不存在,它会创建目录。但是,我认为问题在于下一行robocopy $(TargetDir) %Dir2019%$(ProjectName) /XF "*.pdb" /E。我没有更改应用程序中的任何内容,这发生在我的所有项目中。这可能是关于 robocopy 工作原理的更新吗?我不确定。这里完全不解。
编辑:
我更新了我的代码以遵循 Squashman 和 Mofi 的建议,但错误仍然存在。我继续删除大部分事件后代码以缩小错误范围:
echo Configuration: $(Configuration)
echo Copying results to 2019
robocopy $(TargetDir) "C:\Users\username\Documents\New folder (2)"
if %errorlevel% leq 1 exit 0 else exit %errorlevel%
:exit
即使有明确明确的路径,错误仍然弹出:
Invalid Parameter #3 : "C:\Users\oscarramirez\Documents\New folder (2)"
感谢您的帮助。如果不是很明显,我在代码方面是个业余爱好者。我绝对不是开发人员,所以如果我的错误是一个严重的新手错误,请保持温和。
【问题讨论】:
-
使用 Robocopy 不要以反斜杠结束源路径或目标路径,并且始终使用引号来保护文件路径中的空格和特殊字符。
robocopy "C:\some\source\path" "D:\to\destination\path" -
我强烈建议您不要为变量分配引号。变量赋值的最佳实践是
set "Dir2018=C:\Program Files\Rune\Altem 2018\Plugins\"。这使用引号来保护变量赋值,但不会使引号与变量的值分开。然后,当您需要使用变量时,请在其周围使用引号。if not exist "%Dir2018%IssueCreator" mkdir "%Dir2018%IssueCreator". -
该错误是因为您的源目录未正确引用。使用脚本的所有详细输出更新您的问题,包括所有 robocopy 消息。
标签: batch-file robocopy post-build-event