Windows CMD 批处理脚本中的文件名时间戳被截断
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11037831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Filename timestamp in Windows CMD batch script getting truncated
提问by bryc
I have a batch script that outputs a file, and I'm trying to ensure that each time the script is executed, no existing files are overwritten, so I'm trying to put a timestamp on it.
我有一个输出文件的批处理脚本,我试图确保每次执行脚本时,没有现有文件被覆盖,所以我试图在它上面加上时间戳。
Currently I have this:
目前我有这个:
set stamp=%DATE:/=-%_%TIME::=-%
But if the time is 1-9 AM, it gives something like:
但如果时间是上午 1 点到 9 点,它会给出类似的信息:
13-06-2012_ instead of a full 13-06-2012_12-39-37.28
How can I fix this?
我怎样才能解决这个问题?
I'm using Windows 7, and the output of echo %date% %time%
in a command line window is (my clock format for 'short date' is set to display 3-letter months):
我使用的是 Windows 7,echo %date% %time%
命令行窗口中的输出是(我的“短日期”时钟格式设置为显示 3 个字母的月份):
03-Sep-12 9:06:21.54
Basically I want a solution that solves the issue regardless of what the clock format is set to.
基本上我想要一个解决问题的解决方案,无论时钟格式设置为什么。
Edit:Since no one likes to read past the title, I will explicitly state this question is about a truncation issue. And I found a solution.
编辑:由于没有人喜欢阅读标题,我将明确说明这个问题是关于截断问题的。我找到了解决方案。
I've been using the following timestamp for a good while now, works well.
我已经使用以下时间戳有一段时间了,效果很好。
set timestamp=%DATE:/=-%_%TIME::=-%
set timestamp=%timestamp: =%
It produced a timestamp like: 18-03-2013_13-37-43.26
, by replacing /
and :
in %TIME%
and %DATE%
, then stripping white space. The whitespace was the problem in my original question, really.
它产生了一个时间戳,如:18-03-2013_13-37-43.26
,通过替换/
and :
in %TIME%
and %DATE%
,然后剥离空白。空格是我原来问题中的问题,真的。
采纳答案by bryc
Thanks to an answer to Stack Overflow quesion Creating a file name as a timestamp in a batch job, I found that it was a space terminating the filename.
感谢Stack Overflow 问题在批处理作业中创建文件名作为时间戳的答案,我发现它是一个终止文件名的空格。
回答by foxidrive
The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher, using WMIC.
此代码的前四行将为您提供可靠的 YY DD MM YYYY HH Min Sec 在 XP Pro 及更高版本中使用 WMIC 的变量。
@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
pause
回答by Nicholas DiPiazza
See Stack Overflow question How to get current datetime on Windows command line, in a suitable format for using in a filename?.
请参阅堆栈溢出问题如何在 Windows 命令行上以适合在文件名中使用的格式获取当前日期时间?.
Create a file, date.bat
:
创建一个文件,date.bat
:
@echo off
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-3 delims=/:/ " %%a in ('time /t') do (set mytime=%%a-%%b-%%c)
set mytime=%mytime: =%
echo %mydate%_%mytime%
Run date.bat
:
运行date.bat
:
C:\>date.bat
2012-06-14_12-47-PM
UPDATE:
更新:
You can also do it with one line like this:
你也可以用这样的一行来完成:
for /f "tokens=2-8 delims=.:/ " %%a in ("%date% %time%") do set DateNtime=%%c-%%a-%%b_%%d-%%e-%%f.%%g
for /f "tokens=2-8 delims=.:/ " %%a in ("%date% %time%") do set DateNtime=%%c-%%a-%%b_%%d-%%e-%%f.%%g
回答by kapu
Here's a batch script I made to return a timestamp. An optional first argument may be provided to be used as a field delimiter. For example:
这是我为返回时间戳而制作的批处理脚本。可以提供可选的第一个参数以用作字段分隔符。例如:
c:\sys\tmp>timestamp.bat
20160404_144741
c:\sys\tmp>timestamp.bat -
2016-04-04_14-45-25
c:\sys\tmp>timestamp.bat :
2016:04:04_14:45:29
c:\sys\tmp>timestamp.bat
20160404_144741
c:\sys\tmp>timestamp.bat -
2016-04-04_14-45-25
c:\sys\tmp>timestamp.bat:
2016:04:04_14:45: 29
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
:: put your desired field delimiter here.
:: for example, setting DELIMITER to a hyphen will separate fields like so:
:: yyyy-MM-dd_hh-mm-ss
::
:: setting DELIMITER to nothing will output like so:
:: yyyyMMdd_hhmmss
::
SET DELIMITER=%1
SET DATESTRING=%date:~-4,4%%DELIMITER%%date:~-7,2%%DELIMITER%%date:~-10,2%
SET TIMESTRING=%TIME%
::TRIM OFF the LAST 3 characters of TIMESTRING, which is the decimal point and hundredths of a second
set TIMESTRING=%TIMESTRING:~0,-3%
:: Replace colons from TIMESTRING with DELIMITER
SET TIMESTRING=%TIMESTRING::=!DELIMITER!%
:: if there is a preceeding space substitute with a zero
echo %DATESTRING%_%TIMESTRING: =0%
回答by beloblotskiy
Windows batch log file name in 2018-02-08_14.32.06.34.log format:
2018-02-08_14.32.06.34.log 格式的 Windows 批处理日志文件名:
setlocal
set d=%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%
set t=%time::=.%
set t=%t: =%
set logfile="%d%_%t%.log"
ping localhost -n 11>%1/%logfile%
endlocal
回答by James K
for /f "tokens=2-8 delims=.:/ " %%a in ("%date% %time: =0%") do set DateNtime=%%c-%%a-%%b_%%d-%%e-%%f.%%g
echo %DateNtime%
Or, from the command line:
或者,从命令行:
for /f "tokens=2-8 delims=.:/ " %a in ("%date% %time: =0%") do echo %c-%a-%b_%d-%e-%f.%g
EDIT:As per bryce's non-standardtime/date specs. (03-Sep-12 9:06:21.54
)
编辑:根据布莱斯的非标准时间/日期规范。( 03-Sep-12 9:06:21.54
)
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1-7 delims=.:/- " %%a in ("%date% %time%") do (
if "%%b"=="Jan" set MM=01
if "%%b"=="Feb" set MM=02
if "%%b"=="Mar" set MM=03
if "%%b"=="Apr" set MM=04
if "%%b"=="May" set MM=05
if "%%b"=="Jun" set MM=06
if "%%b"=="Jul" set MM=07
if "%%b"=="Aug" set MM=08
if "%%b"=="Sep" set MM=09
if "%%b"=="Oct" set MM=10
if "%%b"=="Nov" set MM=11
if "%%b"=="Dec" set MM=12
set HH=0%%d
set HH=!HH:~-2!
echo 20%%c-!MM!-%%a_!HH!-%%e-%%f.%%g
)
endlocal
回答by Anubis
BATCH/CMD FILE like DateAndTime.cmd(not in CMD-Console)
BATCH/CMD 文件,如DateAndTime.cmd(不在 CMD-Console 中)
Code:
代码:
SETLOCAL EnableDelayedExpansion
(set d=%date:~8,2%-%date:~3,2%-%date:~0,2%) & (set t=%time::=.%) & (set t=!t: =0!) & (set STAMP=!d!__!t!)
Create output:
创建输出:
echo %stamp%
Output:
输出:
2020-02-25__08.43.38.90
Or also possible in for lines in CMD-Console and BATCH/CMD File
或者也可以用于 CMD-Console 和 BATCH/CMD 文件中的行
set d=%date:~6,4%-%date:~3,2%-%date:~0,2%
set t=%time::=.%
set t=%t: =0%
set stamp=%d%__%t%
"Create output" and "Output" same as above
“创建输出”和“输出”同上
回答by Mathieu K.
In the past, I've used a .cmd script I found on the Internet. I hate the way localization normally messes with dates. Anytime you have dates in filenames (or anywhere else, if I may be so bold) I figure you want them in ISO 8601 format:
过去,我使用了在 Internet 上找到的 .cmd 脚本。我讨厌本地化通常与日期混淆的方式。任何时候你在文件名中有日期(或其他任何地方,如果我可能这么大胆)我想你希望它们采用 ISO 8601 格式:
2015-02-19T14:54:51Z
2015-02-19T14:54:51Z
or something else that has Y M D H M in that order, such as
或其他具有 YMDHM 顺序的东西,例如
2015-02-19 14:54
2015-02-19 14:54
because it fixes the MDY / DMY ambiguity and because it's sortable as text.
因为它修复了 MDY / DMY 歧义,并且因为它可以作为文本进行排序。
I don't know where I got that .cmd script, but it may have been http://ss64.com/nt/syntax-getdate.html, which works beautifully on my YYYY-MM-DD Windows 8.1 and on a M/D/YYYY vanilla install of Windows 7. Both give the same format:
我不知道我从哪里得到了那个 .cmd 脚本,但它可能是http://ss64.com/nt/syntax-getdate.html,它在我的 YYYY-MM-DD Windows 8.1 和 M Windows 7 的 /D/YYYY vanilla 安装。两者都提供相同的格式:
2015-02-09 04:43
2015-02-09 04:43
回答by Cole Johnson
It wants the full time in DD-MM-YYYY_HH-MM-SS.TT
where TT is the ticks. The exception says it all.
它想要全职时间,DD-MM-YYYY_HH-MM-SS.TT
其中 TT 是滴答声。例外说明了一切。