Page 1 of 1

Batch add Previous Date to Filename

Posted: 25 Sep 2012 13:58
by macrome
Hello,

I would like to create a copy of a file, and rename it to include a date. Specifically I need the date that corresponds with the previous Sunday.

Example: Today is Tuesday Sept 25th, the date I want to show would be Sunday Sept 16th.

I am using the following code to add todays date:

Code: Select all

@Echo Off
@For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do @(
Set Year=%%A
Set Month=%%B
Set Day=%%C
Set All=%%A_%%B_%%C
)
@ copy "O:\WKLY.txt" "O:\WKLY %All%.txt"


This gives me a file called WKLY 2012_09_25
I would like WKLY 2012_09_16

Any suggestions?

Thank you!

Re: Batch add Previous Date to Filename

Posted: 25 Sep 2012 14:06
by macrome
I found that this works to get the DOW:

wmic path win32_localtime get dayofweek

I'll see if there is a way to take advantage of it

Re: Batch add Previous Date to Filename

Posted: 25 Sep 2012 15:44
by macrome
This works so far, but I have not done extensive testing. Bulk of this code came from a script Wimmel posted here:
http://stackoverflow.com/questions/355425/date-arithmetic-in-dos-scripting

I've modified a bit to suit my needs. First I use wmic to get the day of the week, today being Tuesday it returns a value of 2.
Since I want to round down to last Sunday's date, I add 7 to that, giving me 9.

The value (9) is passed into Wimmels script, and is subtracted from today's date. Looks like he's got logic in there to account for the year, leap year, days in each month, etc.. so I think it's safe.

Then I add the adjusted date value to a filename.

So far so good. Hope this helps someone else.

Here's the full script:

Code: Select all

@echo off
SETLOCAL enabledelayedexpansion
SET /a count=0
FOR /F "skip=1" %%D IN ('wmic path win32_localtime get dayofweek') DO (
    if "!count!" GTR "0" GOTO next
    SET /a wkdy = %%D
    SET /a count+=1

)
:next

set /a wkdy+=7

set yyyy=

set $tok=1-3
for /f "tokens=1 delims=.:/-, " %%u in ('date /t') do set $d1=%%u
if "%$d1:~0,1%" GTR "9" set $tok=2-4
for /f "tokens=%$tok% delims=.:/-, " %%u in ('date /t') do (
 for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do (
    set %%x=%%u
    set %%y=%%v
    set %%z=%%w
    set $d1=
    set $tok=))

if "%yyyy%"=="" set yyyy=%yy%
if /I %yyyy% LSS 100 set /A yyyy=2000 + 1%yyyy% - 100

set CurDate=%mm%/%dd%/%yyyy%

set dayCnt=%1

if "%dayCnt%"=="" set dayCnt=%wkdy%

REM Substract your days here
set /A dd=1%dd% - 100 - %dayCnt%
set /A mm=1%mm% - 100

:CHKDAY

if /I %dd% GTR 0 goto DONE

set /A mm=%mm% - 1

if /I %mm% GTR 0 goto ADJUSTDAY

set /A mm=12
set /A yyyy=%yyyy% - 1

:ADJUSTDAY

if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
REM ** Month 12 falls through

:SET31

set /A dd=31 + %dd%

goto CHKDAY

:SET30

set /A dd=30 + %dd%

goto CHKDAY

:LEAPCHK

set /A tt=%yyyy% %% 4

if not %tt%==0 goto SET28

set /A tt=%yyyy% %% 100

if not %tt%==0 goto SET29

set /A tt=%yyyy% %% 400

if %tt%==0 goto SET29

:SET28

set /A dd=28 + %dd%

goto CHKDAY

:SET29

set /A dd=29 + %dd%

goto CHKDAY

:DONE

if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%

)
@ copy "O:\Automation\WKLY.txt" "O:\Weekly data\WKLY %yyyy%_%mm%_%dd%.txt"

exit


Put in a text file, rename from *.txt to *.bat and there you go (=

Adding some crap to help peeps searching:
BATCH FILE ROUND DATE
BATCH FILE SUBTRACT DATE
BATCH FILE DATE IN FILENAME FILE NAME
BATCH FILE PREVIOUS DATE