Monday, 15 April 2013

rename file with "previous weekday" -



rename file with "previous weekday" -

is possible batch file rename files in directory appending date of previous weekday end of filename?

for example, if ran on mon 2/25/12, add together "_022213" end of filenames in directory.

thanks...

here go.

@echo off setlocal enabledelayedexpansion echo wd = weekday^(date^(^), vbsunday^)>yesterday.vbs echo if wd ^< 3 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 /f "tokens=1-3" %%i in ('cscript /nologo yesterday.vbs') ( 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% %%i in (*) ( set base=%%~ni echo !base:~-7!| findstr "^_[0-9]*$" >nul && set base=!base:~0,-7! ren "%%i" "!base!_!yesterday!%%~xi" )

there's no native function in windows batch scripting allow perform math on dates. vbscript has nifty date math functions though. dateadd allow add together or subtract year, quarter, month, or several other intervals. "w" specifies work day. more info. update: apparently "w" doesn't mean seems should mean. script updated perform math manually.

you can borrow vbscript echoing vbscript .vbs file, capturing output of cscript /nologo vbsfile for loop. see how works?

then piece [basename]_[yesterday slashes stripped][extension].

batch-file

No comments:

Post a Comment