问题描述
项目向前移动,我可以看到为什么创建.bat文件做事情会变得上瘾!
现在我可以定期保存somefile.txt,然后通过添加时间和日期来重命名somefile.txt以创建唯一的文件名。
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
例如,上面的代码刚刚重命名为 somefile.txt
到 somefile_1317_13022011.txt(2011年2月13日1317小时)
As an example, the code above has just renamed somefile.txt
to somefile_1317_13022011.txt (1317hrs on 13th February 2011)
我跑了
ren somefile.txt somefile_%time:~0,2%%time:~3,2%-%date:~-10,2%%date:~7,2%%date:~-4,4%.txt
昨天,它运行成功,直到午夜,然后它崩溃(语法错误),虽然它保存为12012011日期(2011年1月12日),而不是正确的日期12022011。
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?
推荐答案
Animuson提供了一个合适的方式,但没有帮助理解它。我一直在使用此命令查找并遇到: / p>
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
我总是以年月日YYYYMMDD命名,在这里你会看到他给出了一个原因为什么0,2将不工作和1,1将,因为(空格)是一个无效的字符。这打开了我的眼睛的问题。
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.
我最终以:
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
输出:
Logs-20121707_1019
这篇关于cmd行重命名文件的日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!