cmd line rename file with date and time

后端 未结 7 1540
余生分开走
余生分开走 2020-12-14 11:03

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

相关标签:
7条回答
  • 2020-12-14 11:15

    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
    
    0 讨论(0)
  • 2020-12-14 11:22

    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

    0 讨论(0)
  • 2020-12-14 11:28

    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/' *
    
    0 讨论(0)
  • 2020-12-14 11:32

    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"
    
    0 讨论(0)
  • 2020-12-14 11:32

    following should be your right solution

    ren somefile.txt  somefile_%time:~0,2%%time:~3,2%-%DATE:/=%.txt
    
    0 讨论(0)
  • 2020-12-14 11:32

    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

    0 讨论(0)
提交回复
热议问题