Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem Executing Batch File in Pre-Build Event

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!

like image 373
John Laffoon Avatar asked Jan 17 '11 15:01

John Laffoon


People also ask

How do I remove pre-build event from a project?

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.

How do I set up pre-build and post-build events?

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.

What is the current working directory of pre-build and post-build events?

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.

Why can't I find the path to my batch file?

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.


2 Answers

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>
like image 148
I-A-N Avatar answered Oct 22 '22 04:10

I-A-N


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
like image 36
detunized Avatar answered Oct 22 '22 02:10

detunized