Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 990
Hoobajoob Avatar asked Jan 15 '23 09:01

Hoobajoob


1 Answers

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.

like image 130
Hoobajoob Avatar answered May 18 '23 20:05

Hoobajoob