13

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!

John Laffoon
  • 2,885
  • 2
  • 26
  • 38

8 Answers8

12

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

enter image description here

HodlDwon
  • 1,131
  • 1
  • 13
  • 30
  • the CD to the batch file's directory is what fixes it here for me...when I directly call 'c:\mydirectory\mybatch.bat' it doesn't work, but a cd to 'c:\mydirectory' and then a call to 'mybatch.bat' does work...thanks! – Robert Petz Oct 22 '13 at 21:27
6

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
detunized
  • 15,059
  • 3
  • 48
  • 64
  • 1
    At first I tried `$(SolutionDir)Scripts\PostBuild.bat` originally, but then that gave error code 9009. When I used `cmd $(SolutionDir)Scripts\PostBuild.bat` it worked for me, thanks! My bat file test contained a single line `exit 0` no other white space. – HodlDwon Jun 13 '13 at 01:55
  • sorry, this actually didn't work, I have posted an answer below, the `cmd` just didn't show an error... but it also wasn't running the batch file – HodlDwon Jun 13 '13 at 02:39
3

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!

John Laffoon
  • 2,885
  • 2
  • 26
  • 38
  • This doesn't seem that long, but it does contain spaces. Maybe John moved it to a location that didn't have spaces and that was what fixed it. You can use a path with spaces if you put it in double quotes. – Tommy Herbert Sep 02 '20 at 06:45
2

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>
I-A-N
  • 159
  • 3
  • 3
1

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

Alexander
  • 11
  • 1
1

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.

Hallgeir Engen
  • 841
  • 9
  • 10
0

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"
SDK
  • 820
  • 1
  • 12
  • 24
-3

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

Bala Kumar
  • 637
  • 3
  • 11
  • 18
  • 2
    That does not *fix* the problem of trying specifically to execute the batch, does it? It just eliminates the cause. – Hexo Aug 13 '14 at 19:58