开发者

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 开发者_JS百科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!


I got this working and I figure a picture is worth a thousand words, so here is the full setup in a single screenshot.

Problem Executing Batch File in Pre-Build Event


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


It looks like my problem was the length of the path to the batch file. As this was a proof of concept I let VS create it in the default location:

C:\Documents and Settings\UserXXX\My Documents\Visual Studio 2010\Projects\SolutionXXX\ProjectXXX\foo.bat

As soon as I moved the solution to a location with a shorter path it worked fine. =P

Thanks for the suggestions!


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>


1) see error details by View => Output 2) try call "$(SolutionDir)\xx\xx.bat"


I solved the problem by creating the .bat file from windows explorer, and then in turn including it into my project, instead of creating in directly in VS 2010.


The problem I suspect is that there's a SPACE character in your folder path.

So this command will probably work for c:\development\projectABC\foo.bat but not for c:\development\project ABC\foo.bat

At least that was the problem for me, there could be other issues, but I suspect this is the most common. The solution: put quotation marks around you batch call:

"c:\development\project ABC\foo.bat"


Right click the project --> select properties --> select build event Tab Then Clear the Pre-Build event comment lines.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜