批处理命令的日期和时间,文件名 [英] Batch command date and time in file name

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

问题描述

我COM pressing使用 WinZip的在命令行上的文件。因为我们每天的基础上存档,我试图日期和时间添加到这些文件,使一个新的自动生成的每一次。

I am compressing files using WinZip on the command line. Since we archive on a daily basis, I am trying to add date and time to these files so that a new one is auto generated every time.

我使用以下方法来生成一个文件名。将其复制粘贴到您的命令行,你应该看到一个日期和时间成分的文件名。

I use the following to generate a file name. Copy paste it to your command line and you should see a filename with a Date and Time component.

echo Archive_%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.zip

输出

Archive_20111011_ 93609.zip

不过,我的问题是的 AM 的VS的 PM 的。该AM时间戳给我时间 9 (带有前导空格)与 10 自然占用两个空间。

However, my issue is AM vs PM. The AM time stamp gives me time 9 (with a leading blank space) vs. 10 naturally taking up the two spaces.

我想我的问题将延伸到第一九天,前9个月,等也是如此。

I guess my issue will extend to the first nine days, first 9 months, etc. as well.

我该如何解决这个问题,这样前导零包含,而不是领先的空格,所以我得到 Archive_20111011_093609.zip

How do I fix this so that leading zeroes are included instead of leading blank spaces so I get Archive_20111011_093609.zip?

推荐答案

另一种解决方案:

for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetime=%%I

它会给你(的区域设置独立的!):

It will give you (independent of locale settings!):

  20130802203023.304000+120 
( YYYYMMDDhhmmss.<fraction>+/-<timedifference to UTC>  )

从这里,很容易:

from here, it is easy:

set datetime=%datetime:~0,8%-%datetime:~8,6%
20130802-203023

修改 29Dec15洛根的要求为同一OUTPUTFORMAT一个文件的修改日期时间:

Edit 29Dec15 for Logan's request for the same outputformat for the "date-time modified" of a file:

for %%F in (test.txt) do set file=%%~fF
for /f "tokens=2 delims==" %%I in ('wmic datafile where name^="%file:\=\\%" get lastmodified /format:list') do set datetime=%%I
echo %datetime%

更复杂一些,因为它仅适用于完整路径, WMIC 预计反斜杠需要加倍和 = 来转义(第一个,第二个被包围引号的保护)

A bit more complicated, because it works only with full paths, wmic expects the backslashes to be doubled and the = has to be escaped (the first one. The second one is protected by surrounding quotes)

这篇关于批处理命令的日期和时间,文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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