的Visual Studio 2012后生成事件 - 错误code 255错误、事件、Studio、Visual

2023-09-04 03:51:24 作者:空心

下面是我尝试我的应用程序的可执行文件复制到另一个文件夹改变它的名字:

Here is my attempt to copy my application executable to another folder changing it's name:

IF $(ConfigurationName) == Release (
    SET DESTINATION=$(ProjectDir)Output\Distribution

    IF NOT EXIST "%DESTINATION%" ( MD "%DESTINATION%" )

    XCOPY /Q /Y "$(TargetPath)" "%DESTINATION%"
    RENAME "%DESTINATION%\$(TargetFileName)" "$(TargetName).Plain$(TargetExt)"
)

我已经竭尽所能,使其工作,但它总是抛出错误code 255或1,这取决于。运行code与普通的批处理文件就像一个魅力!

I've tried everything to make it work, but it always throw error code 255 or 1, it depends. Running that code with a plain batch file works like a charm!

推荐答案

您需要启用的延迟扩展的,使用的 SETLOCAL EnableDelayedExpansion 命令。难道它在生成后事件的顶部。在此之后,您可以通过使用而不是%VARIABLE_NAME%访问您的变量,但!VARIABLE_NAME! (在变量名,不是你会在常规批处理文件中使用百分比符号的两边使用感叹号)。

You need to enable delayed expansion, using the SETLOCAL EnableDelayedExpansion command. Do it at the top of the post-build event. After that, you can access your variable by using not %VARIABLE_NAME%, but !VARIABLE_NAME! (use an exclamation symbol on either side of the variable name, not the percentage symbol which you would use in a regular batch file).

因此​​,例如

SETLOCAL EnableDelayedExpansion
IF $(ConfigurationName) == Release (
    SET DESTINATION=$(ProjectDir)Output\Distribution
    echo My destination dir is !DESTINATION!
)

这将输出类似于

My destination dir is D:\Work\Projects\PBExample\Output\Distribution.