Execute batch commands before or after compilation using Pre-build or Post-build events
Have you ever wanted to execute a set of commands before or after the compilation, like copying the contents of the directory to the target compilation directory, or copy output of the compilation to a specified folder. This feature is integrated inside visual studio with the name; “Pre-build or Post-build event command line”. You access it from the project properties, in the build events tab.
![]()
If you want to do something before the compilation you enter the shell commands to pre-build, and post-build is the operations for after the compilation. We can use the predefined macros without hard copying the directory names. The edit window will guide us to find the macros.
Let’s say we want to copy the contents of the output directory to the root d:\. What we do is to edit the post-build event command with the command :
copy /D $(TargetPath) c:\
When we build it. We just get the error, “The command “copy D:\DEV\_Projects\sampleProj\bin\sample.dll d:\” exited with code 1.” Trying to execute the code from command line you get a similar error ‘The system cannot find the file specified.’
copy D:\\DEV\\_Projects\\sampleProj\\bin\\sample.dll d:" The system cannot find the file specified.
What we forget is the need for the quotations for the long directory and filenames.
copy /D \"$(TargetPath)\" "c:\"
As a result, we can do whatever bat file operations before or after the compilation.



thanks very much!
Hi,
The above does not work. It still throws the exception saying “Path cannot be found”!!
Hi,
I’ve fixed this by putting all commands in C:\postbld.bat. As it is short name, all runs Ok. Use cd inside the batch file to go to your project folder, do not try to put shell commands in .Net.
HTH,
Kiril.
Hi Dude, i am coming accross similar problem.
I have VS 2005 with .NET 2,3,3.5. on Win 2K3 Entp Edtn
In post build event, i am running below command
copy “$(ProjectDir)Tests\Microsoft.Practices.EnterpriseLibrary.Configuration.dll.config” “$(TargetDir)”
But it exits with following error:
Error 71 The command “copy ‘C:\_Src\LPO_ECM\HERMESDEV01_EcmBuild\Sources\Source\EnterpriseLibrary\Configuration\Tests\Microsoft.Practices.EnterpriseLibrary.Configuration.dll.config’ ‘C:\_Src\LPO_ECM\HERMESDEV01_EcmBuild\Sources\Source\EnterpriseLibrary\Configuration\bin\ReleaseFinal\’” exited with code 1. Configuration.prod
Even i replace above Macro-Directories with actual path, as below, but still i am getting error.
copy “C:\_Src\LPO_ECM\HERMESDEV01_EcmBuild\Sources\Source\EnterpriseLibrary\Configuration\Tests\Microsoft.Practices.EnterpriseLibrary.Configuration.dll.config” “C:\_Src\LPO_ECM\HERMESDEV01_EcmBuild\Sources\Source\EnterpriseLibrary\Configuration\bin\ReleaseFinal\”
Also i cheked that Source folder has necessary files & destination folder is writtable.
I am not able to understand what wrong here..!!!
In case anyone can throw some light on this issue,
please reply me : dal_hit@yahoo.com
Thanks
Dal Hit
Thanks a lot…