21

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?

Raj More
  • 47,048
  • 33
  • 131
  • 198
user611620
  • 211
  • 1
  • 2
  • 4
  • 2
    Is this a follow-up to a previous question? If so, you need to make this question self-contained and self-sufficient. Currently, it all seems to make little sense. :) – bzlm Feb 13 '11 at 13:37
  • All the answers here try to workaround machine-unfriendly (not gonna argue about human-friendliness here) American 12-hour time format, notwithstanding locale-dependence in general. What about using locale-independent time source with 24-hour format. See http://stackoverflow.com/questions/203090/how-to-get-current-datetime-on-windows-command-line-in-a-suitable-format-for-us – Martin Prikryl Jul 21 '14 at 14:22

7 Answers7

17

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:

Echo Off
IF Not EXIST n:\dbfs\doekasp.txt GOTO DoNothing

copy n:\dbfs\doekasp.txt n:\history\doekasp.txt

Rem rename command is done twice (2) to allow for 1 or 2 digit hour,
Rem If before 10am (1digit) hour Rename starting at location (0) for (2) chars,
Rem will error out, as location (0) will have a space
Rem and space is invalid character for file name,
Rem so second remame will be used.
Rem
Rem if equal 10am or later (2 digit hour) then first remame will work and second will not
Rem as doekasp.txt will not be found (remamed)


ren n:\history\doekasp.txt doekasp-%date:~4,2%-%date:~7,2%-%date:~10,4%_@_%time:~0,2%h%time:~3,2%m%time:~6,2%s%.txt
ren n:\history\doekasp.txt doekasp-%date:~4,2%-%date:~7,2%-%date:~10,4%_@_%time:~1,1%h%time:~3,2%m%time:~6,2%s%.txt

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:

ren Logs.txt Logs-%date:~10,4%%date:~7,2%%date:~4,2%_%time:~0,2%%time:~3,2%.txt
ren Logs.txt Logs-%date:~10,4%%date:~7,2%%date:~4,2%_%time:~1,1%%time:~3,2%.txt

Output:

Logs-20121707_1019
Jan Gerlinger
  • 7,361
  • 1
  • 44
  • 52
silentpete
  • 350
  • 2
  • 7
  • 1
    Instead of relying on a failing rename, I've found out that you can get the timestamp, and then afterwards replace a possible space with 0: set logtime=%time:~0,2%%time:~3,2% set logtime=%logtime: =0% – Rassi Feb 25 '13 at 11:12
  • Always inadvisable to use hacks (maintainability and keeping things simple to follow and understand should always be the number 1 golden rule for all work). Most solutions below are cleaner than this (although you do use the best time format IMHO). – SeeMoreGain Sep 18 '14 at 01:46
12

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:

ren "somefile.txt" "somefile-%date:~10,4%%date:~7,2%%date:~4,2%-%time:~0,2%%time:~3,2%.txt"
SeeMoreGain
  • 1,263
  • 16
  • 36
  • 1
    It seems your file is showing up as YYYYDDMM-HHMM (date and month are reversed). – Sun Apr 23 '15 at 18:47
  • 2
    @sunk818 This would be a locale problem. %date uses the date format on your computer. All answers here are assuming a date format of DD/MM/YYYY rather than MM/DD/YYYY. If you want a locale agnostic way of doing this, the solution is a little more complex - you must use the results of command `wmic os get localdatetime`. An example of this can be seen in [link](http://stackoverflow.com/a/10945887/1793348) – SeeMoreGain May 01 '15 at 03:39
  • Thanks, that explains it. I am muddy and you are dummy. – Sun May 01 '15 at 10:43
7

following should be your right solution

ren somefile.txt  somefile_%time:~0,2%%time:~3,2%-%DATE:/=%.txt
animuson
  • 53,861
  • 28
  • 137
  • 147
Raj Gohil
  • 71
  • 1
  • 3
4

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:

    set HR=%time:~0,2%
    set HR=%Hr: =0% 
    set HR=%HR: =%
    rename c:\ops\logs\copyinvoices.log copyinvoices_results_%date:~10,4%-%date:~4,2%-%date:~7,2%_%HR%%time:~3,2%.log 

This gave me my format I needed: copyinvoices_results_2013-09-13_0845.log

Roger
  • 41
  • 1
  • This will fail if your computer doesn't have the time formatted as month/day/year, that is, if you aren't in USA. – neves Mar 02 '16 at 20:25
3

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

Jude Duran
  • 2,195
  • 2
  • 25
  • 41
-2
ls | xargs -I % mv % %_`date +%d%b%Y`

One line is enough. ls all files/dirs under current dir and append date to each file.

melpomene
  • 84,125
  • 8
  • 85
  • 148
Jim.Y
  • 31
  • 4
-2

I tried to do the same:

<fileName>.<ext> --> <fileName>_<date>_<time>.<ext> 

I found that :

rename 's/(\w+)(\.\w+)/$1'$(date +"%Y%m%d_%H%M%S)'$2/' *
Draken
  • 3,134
  • 13
  • 34
  • 54