在批处理作业中创建文件名作为时间戳 [英] Creating a file name as a timestamp in a batch job
问题描述
我们有一个每天运行的批处理作业,并将文件复制到拾取文件夹.我还想复制该文件并将其放入文件名的存档文件夹中
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
在 Windows 批处理作业中执行此操作的最简单方法是什么?
What's the easiest way to do this in a Windows batch job?
我基本上是在寻找与此 Unix 命令等效的命令:
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
但它依赖于语言环境.我不确定 %DATE%
是否已本地化,或者取决于为 Windows 中的短日期指定的格式.
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.
这是从这个答案中提取当前日期的一种与语言环境无关的方法,但这取决于WMIC
和 FOR/F
:
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屋!