Reputation: 11
I want to rename the .exe file with the system time/local time by making the modifications to the nmake file. Can some one suggest books or references to the nmake commmands? The reference provided in the microsoft page doesn't cover what I am looking for...
Upvotes: 1
Views: 190
Reputation: 9972
The following makefile will work:
# 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:
The output is:
>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.
Explanation: The environment variables %DATE% and %TIME% are unusual in that their values can change each time you look at them; it seems this causes their interface with nmake
to be problematic. And as a minor problem, %TIME% contains colons, which of course cannot be part of a filename. And as a further problem, the formats of %DATE% and %TIME% are not fixed: they can depend on what the user has selected.
The given solution captures the values of %DATE% and %TIME% in a temporary file, and then sets an nmake
macro to the value of the first line of that file using !include
. The colons are then replaced by "+", or by whatever you want.
For a related question, see Computed macro names.
And as per your second question, namely how do you find "books or references to the nmake commmands?", see my answer to Wildcard for files in sub-directory in NMAKE.
Update #1: My original answer works, but on my machine before 10 a.m. it created a filename with a space. So the answer has been updated to fix this. (Perhaps a better work-around just is to sleep in until 10 a.m. in the morning!)
Update #2: Another approach is to not use nmake macros, and just use environment variables. This is shorter. (I was initially mislead by the nmake printed output.)
But the need to do two substitutions in %TIME%
, one to replace spaces and another to replace colons, adds a bit of complication. The call to call
is needed to delay the variable expansion.
The following makefile:
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:
gives:
>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.
with:
>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
Upvotes: 1