问题描述
我有一个输出文件的批处理脚本,并且我试图确保每次执行该脚本时,都不会覆盖现有文件,因此,我尝试在其上加上时间戳.
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
我该如何解决?
我正在使用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: =%
通过将%TIME%
和%DATE%
中的/
和:
替换为18-03-2013_13-37-43.26
,然后去除空白,产生了一个时间戳:18-03-2013_13-37-43.26
.空格确实是我最初提出的问题.
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批处理脚本中的文件名时间戳被截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!