Visual Studio Post-Build events calling batch files with arguments that have spaces

不羁的心 提交于 2019-12-05 19:23:32

With thanks to pilotcam and James K in the comments for pointing me in the right direction, the interoperation problem I was having was a relatively simple one in the batch scripts I was trying to invoke from VS2010.

When invoked from the command line, I was always passing in relative paths that did not require enclosing quotes. When invoked from VS2010, I was specifying full paths based on VS's expansion of $(SolutionDir) and other macros with enclosing quotes.

When the batch script processed those arguments, it was not written to account for the fact that the quote characters might be present as part of the arguments. So, the first thing it did was check for empty arguments like this:

if "%1"=="" goto usage

This very first line was what was choking when invoked from VS like so:

"$(SolutionDir)myScript.bat" "$(SolutionDir)"

...the if statement was choking because of the enclosed quotes that were getting cooked into the value of %1. The proper fix, which worked for both command line usage when there are no enclosing quotes around the argument as well as from VS2010 when there are, was to strip the quotes out of %1 like so:

if "%~1"=="" goto usage

...and the rest of it worked just fine after fixing this up. Probably batch scripting 101, so you can tell how often I write windows batch scripts.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!