Home > Enterprise >  Escape @ Sign Inside ECHO Command In Batch Script (Called Within VS Post-Build Event)
Escape @ Sign Inside ECHO Command In Batch Script (Called Within VS Post-Build Event)

Time:10-03

I simply want to display the @ sign inside text that is output by an ECHO command in a CMD batch script that's invoked by a Visual Studio post-build event. How can I do that?

My one-line post-build event invokes my batch script like this:

if $(ConfigurationName) == Release call "$(ProjectDir)\..\MYBATCHSCRIPT.bat"

The file MYBATCHSCRIPT.bat simply states:

@ECHO OFF
ECHO Please create a separate batch file called C:\Temp\OTHERBATCH.bat and place this code inside it:
ECHO @ECHO OFF
ECHO ECHO Hi there!
call C:\Temp\OTHERBATCH.bat

I know there are escape characters (such as ^) that I can use to prefix special characters, but none seem to work for me so far! Help! This is my offending line in the script:

ECHO @ECHO OFF

The intended output for that line is:

@ECHO OFF

All I'm doing is I am providing a hint to the developer to write a separate, prerequisite, non-version-controlled two-line OTHERBATCH.bat batch file. He must write it for my build to work correctly. I am giving him an actual code sample in the build output display.

My code does output the intended result if I run it on a regular command line! It just fails with, "The syntax of the command is incorrect" when I call the batch script in a post-build event! All I just want to do is write "@ECHO OFF" on the screen for my post-build output!

CodePudding user response:

To achieve my desired result (outputting "@ECHO OFF" to the Visual Studio output window, I've had to switch gears due to an unforeseen constraint in how MSBuild processes batch script *.bat files.

I've since learned that the command interpreter used during a Visual Studio post-build event must not be as full-featured as the one behind the CMD prompt that I can launch from Windows--though both run *.bat files. In my limited observation thus far, it will behave differently as follows:

  • Using apostrophes instead of REM throws an error
  • Any lines such as these throw an error:
    • ECHO ECHO OFF
    • @ECHO @ECHO OFF
  • The PAUSE command won't wait for the user to hit a key

My solution for the above question was to create a separate text file with the information that I wanted to display on the output window and then use the TYPE command to display it from within my batch script, like so:

TYPE INFO.TXT

Inside INFO.TXT I could place any text that I want to display, including the @ sign and text like "@ECHO OFF", without issue.

  • Related