I'm trying to execute a batch file during a pre-build event. I have a new project and have added foo.bat to it. The file contains the following line:
echo bar
When I set the pre-build event command line to foo.bat, I get the following error:
The command "foo.bat" exited with code 9009.
When I set the pre-build event command line to call foo.bat, I get the following error:
The command "call foo.bat" exited with code 1.
Everything I've read related to those codes generally indicates that there is a problem with the contents of the batch file (not likely in this case) or that the system cannot find the batch file.
The batch file works fine from a command prompt. Things I've tried already: Created the file using different tools, various encodings, placing exit 0 in the file, different build actions for the file, and copying the file to the output directory. All with no luck.
What am I missing? It has to be something simple.
Update: Yep, it was simple - the length of the path was too long. See answer below for details.
Thanks!
Right click the project --> select properties --> select build event Tab Then Clear the Pre-Build event comment lines. Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.
You can type pre- or post-build events for the Build Events Page, Project Designer (C#) directly in the edit box, or you can select pre- and post-build macros from a list of available macros. Pre-build events do not run if the project is up to date and no build is triggered. Contains the events to run either for pre-build or post-build.
The current working directory of the pre-build and post-build events is the output directory specified by the macro $ (OutDir), not the project directory. The following tags in "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets" specify this: Show activity on this post. Show activity on this post.
It's possible that you have another foo.bat somewhere in the PATH. Try to specify full path to your batch file like C:\Path o\foo.bat. When project is being built the current directory is the one with the .vcproj file. The command path should be specified relative to this directory, if it's not in the PATH.
The current working directory of the pre-build and post-build events is the output directory specified by the macro $(OutDir), not the project directory.
The following tags in "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets" specify this:
<Target
Name="PreBuildEvent"
Condition="'$(PreBuildEvent)'!=''"
DependsOnTargets="$(PreBuildEventDependsOn)">
<Exec WorkingDirectory="$(OutDir)" Command="$(PreBuildEvent)" />
</Target>
<Target
Name="PostBuildEvent"
Condition="'$(PostBuildEvent)' != '' and ('$(RunPostBuildEvent)' != 'OnOutputUpdated' or '$(_AssemblyTimestampBeforeCompile)' != '$(_AssemblyTimestampAfterCompile)')"
DependsOnTargets="$(PostBuildEventDependsOn)">
<Exec WorkingDirectory="$(OutDir)" Command="$(PostBuildEvent)" />
</Target>
It's possible that you have another foo.bat
somewhere in the PATH
. Try to specify full path to your batch file like C:\Path\to\foo.bat
.
When project is being built the current directory is the one with the .vcproj
file. The command path should be specified relative to this directory, if it's not in the PATH
.
One more thing to try to diagnose the problem would be to specify cmd
in the pre-build event command explicitly like this:
cmd /c C:\Path\to\foo.bat
or even
C:\windows\system32\cmd.exe /c C:\Path\to\foo.bat
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With