Reputation: 273
This question might be simple to everyone but I am struggling to execute time stamp command when using a command line to create a file in c:\ drive (Let's say a txt file).
Syntax I am using:
c:\> echo >"C:\EmptyFile%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%.txt"
c:\> pause
and the output is "The system cannot find the path specified."
The furthers I have successfully created the file in my drive is using the command below:
c:\> echo. 2>"C:\EmptyFile%DATE:~-4%.txt"
The file name created in the C drive is named: EmptyFile2014.txt
It is seems that command line only takes maximum of two % signs, but I would like a full date, year-mm-dd.
Thanks guys
Upvotes: 0
Views: 7647
Reputation: 41242
The issue is not the number of percent signs but echo
by itself generates an error message. You can use echo(
or type nul
or even break
as I have shown below, which creates a zero bye file and is short to type.
A related tip is that using %date%
is unreliable because different computers can have different formats of the %date%
string.
The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher.
@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%" & set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
break > "C:\EmptyFile - %fullstamp%"
Pause
Upvotes: 2