4

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!

Hoobajoob
  • 2,748
  • 3
  • 28
  • 33
  • The error you quote sounds like it's a specific command in the batch file that's not being executed. – pilotcam Oct 06 '12 at 23:54
  • I think the batch script is fine - I can run it from the command line without issue. It's when I try to run it by prepending the $(SolutionDir) path in front of the script name, together with the other arguments that I get this problem. Trying it on the command line, I get similar errors back, so it must be something obvious with the quotation marks that's screwing everything up. I just can't find the right combination! – Hoobajoob Oct 07 '12 at 00:04
  • Are you running the batch file from the same working directory as visual studio is using? You could a "cd" command to the batch file to see what directory it's being run from, then try to replicate from the command line. And/Or add something like "echo %0 x %1 x %2 x %3" to diagnose how VS is calling it. – pilotcam Oct 07 '12 at 00:10
  • On the command line i've been trying to specify the full path to the batch script, and the full path to $(SolutionDir), enclosing things in quotes, and it won't work even from the command line. If you're not in the same directory as the script, can't you just specify the full path to the script, enclosed in quotes, as the first element on the command line, followed by the arguments to the batch script also enclosed in quotes? – Hoobajoob Oct 07 '12 at 00:14
  • On the command line, I'm using this: "" "" Win32 Debug – Hoobajoob Oct 07 '12 at 00:16
  • That should work... maybe make sure your script changes to the solution directory before doing anything else. Try adding this to the first line of your batch file: "cd /d %1" – pilotcam Oct 07 '12 at 00:20
  • ok, you were right about the script content. The first line checked for an empty argument with this: "%1"=="". But, when the first argument is enclosed in quotes, those quotes become part of %1, so I should have been using "%~1"=="" instead. Thank you! – Hoobajoob Oct 07 '12 at 00:38
  • Why don't you try expanding those into a message box, or fill a text file with them? That might give you a better idea of where the error is. And if it's fine, why not use a variable as an intermediate "holder", and then call the variable, rather than have C++ (or whatever) expand the variables in the execution command. – James K Oct 07 '12 at 01:28
  • So, these scripts all need to operate with no user interaction, they are for automated build and smoke testing environment setup. The lesson I learned here is to add early echo statements inside the batch scripts so I can visually see as it's executing in VS2010 whether the source of the error is before or after entry into the batch script. Thank you both for your help and feedback. – Hoobajoob Oct 07 '12 at 01:44

1 Answers1

9

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.

Hoobajoob
  • 2,748
  • 3
  • 28
  • 33
  • Thanks stripping the quotes worked in my situation I removed enclosing quotes from --> call "$(ProjectDir)PostBuild.bat" $(ConfigurationName) – HostMyBus Jan 10 '20 at 21:03