当第一个小时数字为零时,向文件名添加时间戳
在 Windows 2008 机器上,如果以下行位于 09:59 之后和 00:00 之前,则可以正常工作。
REN "D:\myfile.out" myfile-%DATE:/=%-%TIME::=%%.out
但是,如果我在 10:00 之前执行此操作,则会出现语法错误。它似乎试图用空格而不是零来重命名该文件。这会导致错误。例如:
Myfile-120511- 95343.out -
我需要它保存而不会出现这样的错误:
Myfile-120511-095343.out
任何帮助将不胜感激。
On a Windows 2008 box the following line works fine if it is after 09:59 and before 00:00.
REN "D:\myfile.out" myfile-%DATE:/=%-%TIME::=%%.out
However if I execute this before 10:00 I get a syntax error. It appears to be trying to rename the file with a space rather than a zero. Which causes the error. For example:
Myfile-120511- 95343.out -
Where I need it to save without errors like this:
Myfile-120511-095343.out
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要及时编辑掉前导空格以及编辑冒号(:)
You need to edit out the leading space in time as well as editing the colon (:)
您必须考虑前导空格以及区域设置日期和时间格式。
我使用这段代码,(1)暂时将日期和时间格式字符串更改为众所周知的格式,(2)通过适当的空格处理来解析返回的%date%和%time%,以及(3)放置一个完整的LODDATETIME 变量中的时间戳。
要在代码中使用它,只需在bat的开头调用它并使用返回的LOGDATETIME变量
you must take into account the leading space and the locale date and time format.
I use this code, that (1) temporarily changes the date and time format strings to a well known format, (2) parses the returned %date% and %time% with an appropiate handling of spaces, and (3) places a complete timestamp in the LOGDATETIME variable.
to use it in your code, simply invoke it at the beginning of your bat and use the returned LOGDATETIME varialble