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

查看:117
本文介绍了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 重命名为 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 

昨天,它成功运行到午夜,然后它崩溃了(语法错误),尽管它保存为日期(2011 年 1 月 12 日)的 12012011 而不是正确的日期 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:dbfsdoekasp.txt GOTO DoNothing

copy n:dbfsdoekasp.txt n:historydoekasp.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:historydoekasp.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:historydoekasp.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天全站免登陆