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.
目前我有这个:
set stamp=%DATE:/=-%_%TIME::=-%
但是如果时间是凌晨 1 点到 9 点,它会给出类似的信息:
But if the time is 1-9 AM, it gives something like:
13-06-2012_ instead of a full 13-06-2012_12-39-37.28
我该如何解决这个问题?
How can I fix this?
我使用的是 Windows 7,命令行窗口中 echo %date% %time%
的输出是(我的短日期"时钟格式设置为显示 3-字母月):
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):
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.
由于没有人喜欢阅读标题,我将明确说明这个问题是关于截断问题的.我找到了解决方案.
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: =%
它产生了一个时间戳,如:18-03-2013_13-37-43.26
,通过在 % 中替换
和 /
和 :
TIME%%DATE%
,然后去除空白.空格是我原来问题中的问题,真的.
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.
推荐答案
感谢 堆栈溢出问题的答案在批处理作业中创建文件名作为时间戳,我发现它是一个空格终止文件名.
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.
这篇关于Windows CMD 批处理脚本中的文件名时间戳被截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!