创建一个文件名作为批处理作业时间戳 [英] Creating a file name as a timestamp in a batch job

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

问题描述

我们有运行每天将文件复制到一个文件夹皮卡批处理作业。我也想采取文件的副本,并将其与文件名拖放到存档文件夹

  YYYY-MM-dd.log

什么是在Windows批处理作业要做到这一点最简单的方法?

基本上,我找这Unix命令的等效:

  CP source.log`日期+%F`.log


解决方案

  CP source.log%DATE:〜-4% - %DATE:〜4.2% - %DATE: 〜7.2%.LOG

但它的环境相关。我不知道,如果%DATE%是局部的,或者依赖于在Windows短日期指定的格式。

下面是一个语言环境无关的方式来提取这个答案当前日期,但是它取决于 WMIC FOR / F

  FOR / F %% A IN('WMIC OS GET LocalDateTime ^ |。FINDSTR \\')DO @SET B = %%一
CP source.log%B:〜0,4% - %B:〜4.2% - %B:〜6,2%.LOG

We have a batch job that runs every day and copies a file to a pickup folder. I want to also take a copy of that file and drop it into an archive folder with the filename

 yyyy-MM-dd.log

What's the easiest way to do this in a Windows batch job?

I'm basically looking for an equivalent of this Unix command:

cp source.log `date +%F`.log

解决方案

CP source.log %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%.log

But it's locale dependent. I'm not sure if %DATE% is localized, or depends on the format specified for the short date in Windows.

Here is a locale-independent way to extract the current date from this answer, but it depends on WMIC and FOR /F:

FOR /F %%A IN ('WMIC OS GET LocalDateTime ^| FINDSTR \.') DO @SET B=%%A
CP source.log %B:~0,4%-%B:~4,2%-%B:~6,2%.log

这篇关于创建一个文件名作为批处理作业时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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