Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify VS macros in a batch file to run pre or post build

I can use the VS macros like $(ProjectDir) in my pre & post build events. But is there any way I can specify them in a batch file & run the batch file as my pre & post build event?

e.g.

Before

Post-Build event

copy $(ProjectDir)foo.txt $(ProjectDir)\out\foo.txt

After

Post-Build event

CopyFoo.cmd

where CopyFoo.cmd contains

copy $(ProjectDir)foo.txt $(ProjectDir)\out\foo.txt

I want to do this to make my build events list more user-friendly to edit/update. Editing a batch file is much easier than editing the build events box in VS.

like image 839
Nikhil Avatar asked Aug 09 '11 19:08

Nikhil


2 Answers

Not sure if you can access them or not (becuase $ has a different meaning inside batch file), but one way would be to pass them as command line arguments to the batch file. You can access them inside the batch file as %0 - %9.

Post-Build event

CopyFoo.cmd $(ProjectDir)

Batch file

copy %1foo.txt %1\out\foo.txt
like image 132
Mrchief Avatar answered Nov 13 '22 12:11

Mrchief


Compared to the most accepted result, I here provide with a more intuitive way, especially when you have many macro wanted to use.

Post-Build event

set ENV_ProjectDir=$(ProjectDir)
call CopyFoo.cmd

CopyFoo.cmd

copy %ENV_ProjectDir%\foo.txt %ENV_ProjectDir%\out\foo.txt

Side notes:

  • (VS2022) I tried to use "set ProjectDir=$(ProjectDir)", but it results in some strange build error for my C# project. Better avoid this naming
  • (VS2022) Pre-Build event and Post-Build event env var are seperated. It seems you cannot define in pre-build and use it in post-build
like image 1
Wad Avatar answered Nov 13 '22 11:11

Wad