批处理文件-将计算的日期添加到文件末尾 [英] Batch File - Add calculated date to end of file
问题描述
您好,感谢您的提前帮助!我需要一些与批处理脚本相关的专家帮助,我正在使用这些批处理脚本在 .xlsx
文件的末尾添加一个日期.
Hello and thanks for any help in advance! I'm in need of some expert help related to a batch script I'm using to add a date to the end of a .xlsx
file.
我拥有的当前脚本可以正常工作,并将当前日期添加到文件末尾,但是我需要将当前日期减去12天,再加上当前日期减去6天,添加到文件末尾.文件,格式如下:
The current script I have works fine and adds the current date to the end of the file, but what I need is to add the current date minus 12 days, along with the current date minus 6 days, to the end of the file in the following format:
Test 20160807-20160813.xlsx.
因此其文件名为 Test
,然后为 YYYYMMDD(负12)-YYYYMMDD(负6).xlsx
So its the file name Test
, then YYYYMMDD (minus 12) - YYYYMMDD (minus 6).xlsx
这是我当前正在使用的脚本,用于获取当前日期:
Here is the script I'm currently using that gets the current date:
setlocal enabledelayedexpansion
for %%F in ("C:\test*.xlsx") do ( set "MDate=%%~tF"
set "ParsedDate=!MDate:~6,4!!MDate:~0,2!!MDate:~3,2!" copy %%F%%~dpnF!ParsedDate!%%~xF.new )
for %%F in ("C:\test*.xlsx") do ( del %%F )
ren "C:\test*.new" *.
exit
输出为 Test20160815.xlsx
,但是我需要 Test 20160807-20160813.xlsx
.有人可以帮我解决我做错的事情吗?
The output is Test20160815.xlsx
, but I need Test 20160807-20160813.xlsx
. Can someone help me with what I'm doing wrong?
推荐答案
PowerShell中的简单示例:
Simple example in PowerShell:
$today = [DateTime]::Today
$date1 = $today.AddDays(-12)
$date2 = $today.AddDays(-6)
"Test {0:yyyyMMdd}-{1:yyyyMMdd}.xlsx" -f ($date1),($date2)
在PowerShell中,日期不是字符串.它们是一流的对象.在此示例中,为 $ today
分配了今天的日期( [DateTime]
对象,而不是字符串). $ date1
在今天的日期之前12天,而 $ date2
在今天的日期之前6天.代码的最后一行使用PowerShell的格式( -f
)运算符输出包含所需格式的日期的自定义字符串.
In PowerShell, dates are not strings. They are first-class objects. In this example, $today
gets assigned today's date (a [DateTime]
object, not a string). $date1
gets 12 days before today's date, and $date2
gets 6 days before today's date. The last line of code uses PowerShell's format (-f
) operator to output a customized string containing the dates in the format you want.
这篇关于批处理文件-将计算的日期添加到文件末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!