简体   繁体   English

Windows CMD 批处理脚本中的文件名时间戳被截断

[英]Filename timestamp in Windows CMD batch script getting truncated

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.它产生了一个时间戳,如: 18-03-2013_13-37-43.26 ,通过替换/:%TIME%%DATE% ,然后剥离空白。 The whitespace was the problem in my original question, really.空格确实是我最初问题中的问题。

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.此代码的前四行将使用 WMIC 在 XP Pro 和更高版本中为您提供可靠的 YY DD MM YYYY HH Min Sec 变量。

@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

Output example:输出示例:

datestamp: "20200828"
timestamp: "085513"
fullstamp: "2020-08-28_08-55-13"
Press any key to continue . . .

See Stack Overflow question How to get current datetime on Windows command line, in a suitable format for using in a filename?请参阅 Stack Overflow 问题如何以适合在文件名中使用的格式在 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

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 c:\sys\tmp>时间戳.bat
20160404_144741 20160404_144741
c:\sys\tmp>timestamp.bat - c:\sys\tmp>timestamp.bat -
2016-04-04_14-45-25 2016-04-04_14-45-25
c:\sys\tmp>timestamp.bat: c:\sys\tmp>时间戳.bat:
2016:04:04_14:45:29 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%
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-standard time/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

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 quesion Creating a file name as a timestamp in a batch job的答案,我发现它是一个终止文件名的空格。

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

It wants the full time in DD-MM-YYYY_HH-MM-SS.TT where TT is the ticks.DD-MM-YYYY_HH-MM-SS.TT中的完整时间,其中 TT 是刻度。 The exception says it all.异常说明了一切。

In the past, I've used a.cmd script I found on the Internet.过去,我使用在 Internet 上找到的 a.cmd 脚本。 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:任何时候你在文件名中有日期(或其他任何地方,如果我可以这么大胆的话)我想你希望它们采用 ISO 8601 格式:

2015-02-19T14:54:51Z 2015-02-19T14:54:51Z

or something else that has YMDHM 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 上运行得很好/D/YYYY Windows 7 的原始安装。两者都提供相同的格式:

2015-02-09 04:43 2015-02-09 04:43

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 “创建输出”和“输出”同上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM