简体   繁体   English

在预构建事件中执行批处理文件时出现问题

[英]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. 我有一个新项目,并添加了foo.bat 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: 当我将pre-build事件命令行设置为foo.bat时 ,我收到以下错误:

The command "foo.bat" exited with code 9009. 命令“foo.bat”退出代码9009。

When I set the pre-build event command line to call foo.bat , I get the following error: 当我将pre-build event命令行设置为调用foo.bat时 ,我收到以下错误:

The command "call foo.bat" exited with code 1. 命令“call foo.bat”退出代码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. 我已经尝试过的事情:使用不同的工具,各种编码创建文件,在文件中放置出口0,对文件进行不同的构建操作,以及将文件复制到输出目录。 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. 我得到了这个工作,我认为一张图片胜过千言万语,所以这里是一个完整的设置在一个屏幕截图。

在此输入图像描述

It's possible that you have another foo.bat somewhere in the PATH . 你可能在PATH某个地方有另一个foo.bat Try to specify full path to your batch file like C:\\Path\\to\\foo.bat . 尝试指定批处理文件的完整路径,如C:\\Path\\to\\foo.bat

When project is being built the current directory is the one with the .vcproj file. 在构建项目时,当前目录是具有.vcproj文件的目录。 The command path should be specified relative to this directory, if it's not in the PATH . 如果命令路径不在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 ,如下所示:

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: 由于这是概念证明,我让VS在默认位置创建它:

C:\\Documents and Settings\\UserXXX\\My Documents\\Visual Studio 2010\\Projects\\SolutionXXX\\ProjectXXX\\foo.bat 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 = 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. 预构建和后构建事件的当前工作目录是宏$(OutDir)指定的输出目录,而不是项目目录。

The following tags in "C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\Microsoft.Common.targets" specify this: “C:\\ Windows \\ Microsoft.NET \\ Framework \\ v4.0.30319 \\ Microsoft.Common.targets”中的以下标记指定:

<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)通过View => Output 2查看错误详情。尝试调用“$(SolutionDir)\\ xx \\ xx.bat”

我通过从Windows资源管理器创建.bat文件,然后将其包含到我的项目中,而不是直接在VS 2010中创建,解决了这个问题。

The problem I suspect is that there's a SPACE character in your folder path. 我怀疑的问题是你的文件夹路径中有一个SPACE字符。

So this command will probably work for c:\\development\\projectABC\\foo.bat but not for c:\\development\\project ABC\\foo.bat 所以这个命令可能适用于c:\\development\\projectABC\\foo.bat但不适用于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"

右键单击项目 - >选择属性 - >选择构建事件选项卡然后清除预构建事件注释行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM