使用“上一个工作日"重命名文件; [英] rename file with "previous weekday"

查看:93
本文介绍了使用“上一个工作日"重命名文件;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

批处理文件是否可以通过将前一个工作日的日期附加到文件名的末尾来重命名目录中的所有文件?

Is it possible for a batch file to rename all files in a directory by appending the date of previous weekday to the end of the filename?

例如,如果我在2012年2月25日星期一运行它,它将在目录中文件名的末尾添加"_022213".

for example, if i ran it on Monday 2/25/12, it would add "_022213" to the end of the filenames in the directory.

谢谢...

推荐答案

在这里.

@echo off
setlocal enabledelayedexpansion
echo wd = Weekday^(Date^(^), vbSunday^)>yesterday.vbs
echo if wd ^< 3 then dif = -1 - wd else dif = -1 >>yesterday.vbs
echo d = dateadd^("d", dif, Date^(^)^)>>yesterday.vbs
echo wscript.echo DatePart^("yyyy",d^) ^& " " ^& DatePart^("m", d^) ^& " " ^& DatePart^("d", d^) >>yesterday.vbs
for /f "tokens=1-3" %%I in ('cscript /nologo yesterday.vbs') do (
    set year=%%I
    if %%J leq 9 (set month=0%%J) else set month=%%J
    if %%K leq 9 (set day=0%%K) else set day=%%K
)
del yesterday.vbs
set yesterday=%month%%day%%year:~-2%
for %%I in (*) do (
    set base=%%~nI
    echo !base:~-7!| findstr "^_[0-9]*$" >NUL && set base=!base:~0,-7!
    ren "%%I" "!base!_!yesterday!%%~xI"
)

Windows批处理脚本中没有本机函数可让您对日期进行数学运算. VBscript有一些漂亮的日期数学函数. DateAdd使您可以按年,季度,月或其他几个间隔进行添加或减去. "w"指定工作日. 更多信息. 更新:显然"w"并不意味着它应该看起来像什么.脚本已更新,可以手动执行数学运算.

There's no native function in Windows batch scripting that will let you perform math on dates. VBscript has some nifty date math functions though. DateAdd will let you add or subtract by the year, the quarter, the month, or several other intervals. "w" specifies work day. More info. Update: apparently "w" doesn't mean what it seems like it should mean. Script updated to perform the math manually.

您可以通过以下方式从vbscript借用:将vbscript回显到.vbs文件,然后使用for循环捕获cscript /nologo vbsfile的输出.看看它是如何工作的?

You can borrow from vbscript by echoing the vbscript to a .vbs file, then capturing the output of cscript /nologo vbsfile with a for loop. See how that works?

然后将它们拼凑成[basename]_[yesterday with slashes stripped][extension].

这篇关于使用“上一个工作日"重命名文件;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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