Project moving forwards, I can see why creating .bat files to do things can become addictive! I can now save somefile.txt at regular intervals, I then rename somefile.txt by adding the time and date to create a unique file name
ren somefile.txt somefile_%time:~0,2%%time:~3,2%-%date:~-10,2%%date:~3,2%%date:~-4,4%.txt
As an example, the code above has just renamed somefile.txt
to somefile_1317_13022011.txt (1317hrs on 13th February 2011)
I ran
ren somefile.txt somefile_%time:~0,2%%time:~3,2%-%date:~-10,2%%date:~7,2%%date:~-4,4%.txt
yesterday, it ran successfully until midnight, and then it crashed (syntax error) although it was saving as 12012011 for the date (12th Jan 2011) instead of the correct date of 12022011.
Will the current version ran past midnight? Am I confusing myself with UK vs US date format?
following should be your right solution
I took the above but had to add one more piece because it was putting a space after the hour which gave a syntax error with the rename command. I used:
This gave me my format I needed: copyinvoices_results_2013-09-13_0845.log
Animuson gives a decent way to do it, but no help on understanding it. I kept looking and came across a forum thread with this commands:
I always name year first YYYYMMDD, but wanted to add time. Here you will see that he has given a reason why 0,2 will not work and 1,1 will, because (space) is an invalid character. This opened my eyes to the issue. Also, by default you're in 24hr mode.
I ended up with:
Output:
I tried to do the same:
I found that :
problem in
%time:~0,2%
can't set to 24 hrs format, ended with space(1-9), instead of 0(1-9)go around with:
set
HR=%time:~0,2%
set
HR=%Hr: =0% (replace space with 0 if any <has a space in between : =0>)
then replace
%time:~0,2%
with%HR%
good luck
Digging up the old thread because all solutions have missed the simplest fix...
It is failing because the substitution of the time variable results in a space in the filename, meaning it treats the last part of the filename as a parameter into the command.
The simplest solution is to just surround the desired filename in quotes
"filename"
.Then you can have any date pattern you want (with the exception of those illegal characters such as
/
,\
,...)I would suggest reverse date order YYYYMMDD-HHMM: