批处理文件时间 [英] Batch File Time
本文介绍了批处理文件时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
重命名文件时如何在该批处理文件中添加时间?
How do I add the time to this batch file when I rename the file?
结果如下所示,但也需要时间:Daily Report_Wed 08222018.pdf
The results look like this but need time also: Daily Report_Wed 08222018.pdf
@echo off
Pushd c:\Temp
pdftk *.pdf cat output %fn%.pdf
ren %fn%.pdf %fn%.xxx
del *.pdf
ren %fn%.xxx "Daily Report".pdf
for /f "tokens=1-3 delims=/" %%a in ('echo %date%') do set today=%%a%%b%%c
for %%f in (*.pdf) do ren "%%f" "%%~nf_%today%%%~xf"
mkdir "Daily Reports for Review"
move *.pdf "Daily Reports for Review"
推荐答案
以下内容可以使您接近所需的内容(我修改了1 st for
循环):
Here's something that could get you close to what you want (I modified your 1st for
loop):
e:\Work\Dev\StackOverflow>echo Date: [%date%], Time: [%time%]
Date: [2018-08-22], Time: [22:51:36.23]
e:\Work\Dev\StackOverflow>for /f "tokens=1-6 delims=/-:., " %a in ('echo %date: =0%-%time: =0%') do (echo set now=%a%b%c%d%e%f)
e:\Work\Dev\StackOverflow>(echo set now=20180822225136 )
set now=20180822225136
注释:
- 由于我直接位于 cmd 窗口中,并且您位于 batch 文件中,因此必须将参数(
%)加倍>-> %%a
,%b
->%%b
,...,就像您已经在代码段中一样).有关更多详细信息,请检查 [SS64]:双%%符号为什么在批处理文件中需要它们? - 您可能已经注意到(很可能是由于"区域设置"引起的),我的日期格式与您的日期格式有所不同(这就是为什么我添加了连字符(
-
)的原因在delims
列表中;项目的顺序也被颠倒了),因此您在不同的计算机上可能会出现稍微不同的行为(在非英语语言环境中情况会变得更糟,如@Stephan注意到的那样),所以这不是一个可靠的解决方案(猜猜这是批处理处理的通用方法)
- Since I am directly in cmd window, and you're in a batch file you have to double the percent sign (
%
) for parameters (%a
->%%a
,%b
->%%b
, ... like you already have in your snippet). For more details, check [SS64]: Double %% symbols why are they needed in a batch file? - As you probably noticed (most likely due to "Regional settings"), my date format differs than yours (that's why I added the hyphen (
-
) in thedelims
list; also the items ordering is reversed), so you might get slightly different behaviors on different computers (things will get even worse on non English locales, as @Stephan noticed), so it's not a reliable solution (I guess this is batch processing generic)
这篇关于批处理文件时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文