cmd行重命名文件,日期和时间 [英] cmd line rename file with date and time

查看:932
本文介绍了cmd行重命名文件,日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

项目向前移动,我可以看到为什么创建.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 to somefile_1317_13022011.txt(2011年2月13日1317hrs)

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给出了一个体面的方式来做,但没有帮助理解它。我一直在寻找并发现一个论坛帖子使用以下命令:

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将会因为(空格)是无效的字符的原因。这让我看到了这个问题。此外,默认情况下,您是在24小时模式。

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行重命名文件,日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆