在Windows CMD批处理脚本文件名时间戳 [英] Filename timestamp in Windows CMD batch script

查看:3367
本文介绍了在Windows CMD批处理脚本文件名时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理脚本输出的文件,我想确保每个执行脚本的时候,没有现有的文件将被覆盖,所以我试图把一个时间戳就可以了。

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  AM,它给人这样的:

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,和的输出一个命令行窗口呼应%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


编辑:我已经使用了好以下时间戳,而现在,效果很好。


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.

推荐答案

请参阅堆栈 溢出问题的 如何获得在Windows当前日期时间命令行中,以合适的格式用于使用在一个文件名?

See Stack Overflow question How to get current datetime on Windows command line, in a suitable format for using in a filename?.

创建一个文件, 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%

运行 date.bat

C:\>date.bat
2012-06-14_12-47-PM

更新:

您也可以做到这一点用这样一行:

You can also do it with one line like this:

FOR / F令牌= 2-8 delims =:/%%一中(%DATE%%TIME%)并设置DateNtime = %%Ç - %% A- %% b _ %%ð - %% E - %%˚F%%摹

这篇关于在Windows CMD批处理脚本文件名时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆