当第一个小时数字为零时,向文件名添加时间戳

发布于 2024-11-06 21:32:13 字数 359 浏览 2 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倥絔 2024-11-13 21:32:13

您需要及时编辑掉前导空格以及编辑冒号(:)

set FormatedTime=%Time: =0%

REN "D:\myfile.out" myfile-%DATE:/=%-%FormatedTime::=%.out

You need to edit out the leading space in time as well as editing the colon (:)

set FormatedTime=%Time: =0%

REN "D:\myfile.out" myfile-%DATE:/=%-%FormatedTime::=%.out
眼角的笑意。 2024-11-13 21:32:13

您必须考虑前导空格以及区域设置日期和时间格式。

我使用这段代码,(1)暂时将日期和时间格式字符串更改为众所周知的格式,(2)通过适当的空格处理来解析返回的%date%和%time%,以及(3)放置一个完整的LODDATETIME 变量中的时间戳。

:logdatetime
reg copy "HKCU\Control Panel\International" "HKCU\Control Panel\International-Temp" /f >nul
reg add "HKCU\Control Panel\International" /v sShortDate /d "yyyy-MM-dd" /f >nul
set LogDate=%date%
reg add "HKCU\Control Panel\International" /v sTimeFormat /d "HH:mm:ss" /f >nul
set LogTime=%Time%
reg copy "HKCU\Control Panel\International-Temp" "HKCU\Control Panel\International" /f >nul
set loghour=%logtime:~0,2%
if "%loghour:~0,1%" == " " set loghour=0%loghour:~1,1%
set logmin=%logtime:~3,2%
if "%logmin:~0,1%" == " " set logmin=0%logmin:~1,1%
set logsec=%logtime:~6,2%
if "%logsec:~0,1%" == " " set logsec=0%logsec:~1,1%
set logyear=%logdate:~0,4%
set logmonth=%logdate:~5,2%
if "%logmonth:~0,1%" == " " set logmonth=0%logmonth:~1,1%
set logday=%logdate:~8,2%
if "%logday:~0,1%" == " " set day=0%day:~1,1%
set logdatetime=%logyear%%logmonth%%logday%%loghour%%logmin%%logsec%
goto :eof

要在代码中使用它,只需在bat的开头调用它并使用返回的LOGDATETIME变量

CALL :logdatetime
...
REN "D:\myfile.out" myfile-%LOGDATETIME%.out

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.

:logdatetime
reg copy "HKCU\Control Panel\International" "HKCU\Control Panel\International-Temp" /f >nul
reg add "HKCU\Control Panel\International" /v sShortDate /d "yyyy-MM-dd" /f >nul
set LogDate=%date%
reg add "HKCU\Control Panel\International" /v sTimeFormat /d "HH:mm:ss" /f >nul
set LogTime=%Time%
reg copy "HKCU\Control Panel\International-Temp" "HKCU\Control Panel\International" /f >nul
set loghour=%logtime:~0,2%
if "%loghour:~0,1%" == " " set loghour=0%loghour:~1,1%
set logmin=%logtime:~3,2%
if "%logmin:~0,1%" == " " set logmin=0%logmin:~1,1%
set logsec=%logtime:~6,2%
if "%logsec:~0,1%" == " " set logsec=0%logsec:~1,1%
set logyear=%logdate:~0,4%
set logmonth=%logdate:~5,2%
if "%logmonth:~0,1%" == " " set logmonth=0%logmonth:~1,1%
set logday=%logdate:~8,2%
if "%logday:~0,1%" == " " set day=0%day:~1,1%
set logdatetime=%logyear%%logmonth%%logday%%loghour%%logmin%%logsec%
goto :eof

to use it in your code, simply invoke it at the beginning of your bat and use the returned LOGDATETIME varialble

CALL :logdatetime
...
REN "D:\myfile.out" myfile-%LOGDATETIME%.out
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文