user2335924
user2335924

Reputation: 55

Issue with %date% and %time% on the command line

I got this command to generate a log file that contains the date in the name.

echo %time%;stuff done >> C:\myfile_%date%.log

Output: The system cannot find the path specified.

If I do remove the system variable, all fine. I have attempted to add "" and `` and '', no go so far.

Some help would be welcome :) Thanks.

Upvotes: 1

Views: 171

Answers (1)

Luke Peterson
Luke Peterson

Reputation: 8871

You need to generate a date without spaces in it. %date% outputs Mon 16/06/2014 which when passed as a filename causes problems with the creation. If you substring each of the date elements and build a custom filename with the below:

echo %time%;stuff done >> C:\myfile_%date:~10%%date:~4,2%%date:~7,2%%time:~0,2%%time:~3,2%.log

your command will work. The filename I get as a result is:

myfile_201416062105.log

which as a backwards timestamp will also make them easier to sort and find after the generation.

Upvotes: 1

Related Questions