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

北城以北 提交于 2019-12-07 16:05:13

问题


Anybody know how to do this?

I have a .bat script that I'd like to be able to use both from the command line as well as a post-build script in Visual Studio.

The script takes arguments, some of which can contain spaces.

The path to the .bat script can contain spaces, and is best expressed as a path relative to $(SolutionDir).

I've tried what seems like a hundred variations of command lines with different enclosing quote and escape character combinations including:

"$(SolutionDir)myScript.bat" "$(SolutionDir)\" "$(Platform)" "$(Configuration)"

call "$(SolutionDir)myScript.bat" "$(SolutionDir)" "$(Platform)" "$(Configuration)"

cmd /c ""$(SolutionDir)myScript.bat" "$(SolutionDir)" "$(Platform)" "$(Configuration)""

But they all generate errors, and I cannot find a combination that works, instead usually getting this error:

"[path-to-script] is not recognized as an internal
or external command, operable program or batch file"

What is the proper syntax to invoke this from a post-build step?

I'm having similar problems setting it up as an external tool as well, so any help with either is greatly appreciated!


回答1:


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.



来源:https://stackoverflow.com/questions/12764847/visual-studio-post-build-events-calling-batch-files-with-arguments-that-have-spa

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