Reputation: 2049
this is my situation:
@echo off
Setlocal EnableDelayedExpansion
set file=C:\Users\test\Desktop\fscls.cfg
how can I rename %file% variable to get (with an echo command):
C:\Users\test\Desktop\TIMESTAMP_fscls.cfg
Upvotes: 0
Views: 1276
Reputation: 24466
The %time%
environment variable contains a timestamp.
:: Remove colons from %time%
set ts=%time::=%
:: Remove centiseconds from %ts%
set ts=%ts:~0,-3%
:: file=HHMMSS_file
set file=%ts%_%file%
If you need your timestamp to include the date, you can get it by scraping the %date%
environment variable in a similar manner. See this page on DOS String Manipulation for more info.
If your %file%
variable already has the path included and you're trying to insert the timestamp between the path and the filename, that's a little trickier. You'll either need to use a for /f
loop or to call
a subroutine for batch parameter substitution.
@echo off
setlocal
set file=C:\Users\test\Desktop\fscls.cfg
:: set ds=YYYYMMDDHHMMSS.etc (the result of wmic os get localdatetime)
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set ds=%%I
:: set ds=YYYYMMDD
set ds=%ds:~0,8%
:: Insert %ds% into %file%
call :insert "%file%" "%ds%_" file
echo new filename: %file%
:: end main
goto :EOF
:insert <path> <str_to_insert> <varname_to_set>
set "%~3=%~dp1%~2%~nx1"
See the last couple of pages of help for
and help call
for more info.
Upvotes: 1