Omtechguy
Omtechguy

Reputation: 3651

How to execute Post-build event without building the project?

I would like to execute the "Post-build" event without building the project. is there an option to do so somehow?

enter image description here

Upvotes: 4

Views: 1989

Answers (2)

Adam Plocher
Adam Plocher

Reputation: 14233

I would recommend putting the commands into a batch file and simply running THAT in your Post Build Events field.

You can add it to your solution and then from Solution Explorer, right click, "Open With", and add a new item called:

  • Name: Cmd
  • Command: C:\windows\system32\cmd.exe
  • Args: /c "$(ItemPath)"
  • Initial Dir: $(ItemDir)

This will allow you to run Batch files from Solution Explorer in the future. More details here.

Bat File Solution 1

Either pass in the path to run from, or rely on %SolutionPath% (which is an env var that is provided when you run a bat file from Visual Studio). So this method is doing some directory sniffing. (note, solution 2 below is probably better - simpler for sure)

@echo off
if not [%1]==[] (set "ProjectDir=%1")
if not [%SolutionPath%]==[] (set "ProjectDir=%SolutionPath%\ProjectSubDirectory)
if [%ProjectDir%]==[] (
   echo Error, no Solution Path env var or ProjectDir provided
   exit /b
   goto :eof
)

cd /d %ProjectDir%
ng build

Your post build events would then just run $(ProjectDir)\MyBatchFile.bat "$(ProjectDir)"

Bat File Solution 2

Run in directory relative to location of batch file (a simpler solution).

@echo off
pushd "%dp0"
ng build
popd

In this case your post build event would simply be: $(ProjectDir)\MyBatchFile.bat and it would run in that same directory. I would recommend this approach for simplicity.

Pushd and popd are similar to "CD" except it's a stack that you can pop out of. So popd will return you to your original directory. %dp0 is the directory of the currently executing bat file.

Upvotes: 2

Racil Hilan
Racil Hilan

Reputation: 25351

Launch the Developer Command Prompt:

  • Click on the start button.
  • Type Developer Command Prompt and press ENTER.

Then copy the lines from your Visual Studio:

  • Click on the Edit Post-Build button.
  • Replace the $(ProjectDir) with the actual path to your project.
  • Copy the lines.
  • Paste the lines in the command prompt and press ENTER.

Upvotes: 3

Related Questions