首页 > 解决方案 > 通过使用 nmake 文件附加系统时间来重命名 .exe 文件

问题描述

我想通过修改nmake文件来用系统时间/本地时间重命名.exe文件。有人可以推荐书籍或参考 nmake 命令吗?微软页面中提供的参考资料并未涵盖我正在寻找的内容......

标签: visual-c++nmake

解决方案


以下生成文件将起作用:

# Create the macro DATE_TIME from the environment variables %DATE% and %TIME%
!if [echo %DATE%_%TIME% > date_time.tmp] == 0
DATE_TIME = \
!include date_time.tmp       # get macro value from first line of temporary file
DATE_TIME = $(DATE_TIME::=+) # replace colons with "+"
DATE_TIME = $(DATE_TIME: =0) # replace spaces with "0"
!if [del date_time.tmp]      # clean up temporary file
!endif
!endif

foo.exe: bar

foo.exe:
    @echo Info: Building target $@ from $**, because $? is newer (or non-existent)
    @type nul > $@
    copy $@ "$*_$(DATE_TIME).exe"

bar:

输出是:

>nmake -nologo
Info: Building target foo.exe from bar, because bar is newer (or non-existent)
        copy foo.exe "foo_07-Feb-2019_21+08+48.11.exe"
        1 file(s) copied.

解释:环境变量 %DATE% 和 %TIME% 是不寻常的,因为每次查看它们时它们的值都会发生变化;似乎这会导致他们的接口nmake出现问题。作为一个小问题,%TIME% 包含冒号,它当然不能是文件名的一部分。另一个问题是,%DATE% 和 %TIME%的格式不是固定的:它们可能取决于用户选择的内容。

给定的解决方案在临时文件中捕获 %DATE% 和 %TIME% 的值,然后nmake使用 .将宏设置为该文件第一行的值!include。然后将冒号替换为“+”或您想要的任何内容。

有关相关问题,请参阅计算宏名称

根据您的第二个问题,即如何找到“nmake 命令的书籍或参考资料?”,请参阅我对通配符的回答,以获取 NMAKE 子目录中的文件


更新#1:我的原始答案有效,但在上午 10 点之前在我的机器上创建了一个带空格的文件名。因此,答案已更新以解决此问题。(也许更好的解决方法就是睡到早上 10 点!)


更新#2:另一种方法是不使用 nmake 宏,而只使用环境变量。这更短。(我最初被 nmake 打印输出误导了。)

但是需要在 中进行两次替换%TIME%,一个替换空格,另一个替换冒号,这增加了一些复杂性。需要调用 tocall来延迟变量扩展。

以下生成文件:

foo.exe: bar

foo.exe:
    @echo Info: Building target $@ from $**, because $? is newer (or non-existent)
    @type nul > $@
    call set XXX=%%DATE%%_%%TIME: =0%%& call copy $@ "$*_%%XXX::=+%%.exe"

bar:

给出:

>nmake -nologo
Info: Building target foo.exe from bar, because bar is newer (or non-existent)
        call set XXX=%DATE%_%TIME: =0%& call copy foo.exe "foo_%XXX::=+%.exe"
        1 file(s) copied.

和:

>dir /O-D
 Volume in drive C has no label.
 Volume Serial Number is xxxxxx

 Directory of C:xxxxxx

08-Feb-2019  08:19 AM    <DIR>          ..
08-Feb-2019  08:19 AM    <DIR>          .
08-Feb-2019  08:19 AM                 0 foo_08-Feb-2019_08+19+22.08.exe
08-Feb-2019  08:19 AM                 0 foo.exe
08-Feb-2019  08:18 AM               196 Makefile
               3 File(s)            196 bytes
               2 Dir(s)  12,517,236,736 bytes free

推荐阅读