Can we execute a .bat file in post build event command line in visual studio?

86,441

Solution 1

Sure, here's an example:

call "$(SolutionDir)scripts\copyifnewer.bat" "$(SolutionDir)libs\RLPL.Services.CertificateValidator.Basic.dll" "$(TargetDir)RLPL.Services.CertificateValidator.Basic.dll"
call "$(SolutionDir)scripts\copyifnewer.bat" "$(SolutionDir)libs\RLPL.Services.CertificateValidator.Common.dll" "$(TargetDir)RLPL.Services.CertificateValidator.Common.dll"

Just be aware of two possible issues you might have:

  1. the enclosing double quotes (see how each part is surrounded by " sign)

  2. if you want to call 2 or more batch files make sure you use call command otherwise you'll have a trouble finding why the second bat is not doing its job

Solution 2

Yes, by adding a call to it in the post-build event editor.

If you go to the Properties page for your project, you should select the Build Events tab. You can type in the call to your batch file in the Post-build event command line text box.

If you want to refer to the batch file using the paths included in the project or solution, you can click on the Edit Post-Build... button. This will open the Post-build Event Command Line dialog box.

This dialog box has a Macros >> button that you can click. It will show you all the available Macros that you can use to refer to folders and files within your solution.

When you select one of those macros, you can use the Insert button to insert them into your script.

Solution 3

As well as calling a .bat file, you can enter batch commands (i.e., the normal commands available from the Windows console--cmd.exe) directly into the Pre-build/Post-build fields. This may be preferable as it means you do not have to maintain the batch file separately, as all your commands will be part of the project.

Share:
86,441
Admin
Author by

Admin

Updated on July 04, 2020

Comments

  • Admin
    Admin almost 4 years

    Can we execute a .bat file in post build event command line in visual studio?

  • AAT
    AAT over 14 years
    +1 but for completeness note divo's comment on Jon Skeet's response re using the macros to specify paths for project-specific batch files.
  • EvgeniyK
    EvgeniyK almost 12 years
    thank you for a note about "call", it solves my problem with multiply executing.
  • Martin Smith
    Martin Smith over 10 years
    The answer referred to above no longer exists. The comment trail was Q. "How do I give the batch file the path?" and the comment in response was A. "You can use either use a hard-coded path, or - better - use the variables available when you click on the "Macros" button. For instance, "$(ProjectDir)" (without the quotes) will be the directory where the Visual Studio project file resides"
  • Admin
    Admin about 9 years
    Even though SO is now asking me to not explain my gratitude: +1 for point 2)
  • Tomáš Zato
    Tomáš Zato over 8 years
    How to do it for entire solution rather than individual project?
  • Alex M
    Alex M about 8 years
    Might need to change the current directory, as by default it is run within the Bin folder. I needed to run inside the solution dir, so for me it was: call cd $(SolutionDir) as a first line in Post-build events.
  • jrypkahauer
    jrypkahauer about 5 years
    It's just as easy to commit the post-build script to SCM right along with your project. Actually it's easier to test, it's versioned right along with your files and it's not buried in project files. Personally I no real good rationale for (but many against) having anything but a one-line CALL in your post-build action property.
  • Pangamma
    Pangamma about 4 years
    How do you actually set this up? Does this go in the csproj file?