We help IT Professionals succeed at work.

Rename file with previous weekday's date as the file name

reidtenkley
reidtenkley asked
on
Medium Priority
1,638 Views
Last Modified: 2009-12-16
Experts,

having some trouble getting a concise answer on this:

I have a folder- C:/folder
with a file- sampledailyfile.txt

and another folder- C:/otherfolder

today's date is- 10/31/2006

I would like my batch (.bat) script to open "folder", take sampledailyfile.txt, rename it as 10302006.txt, and save it to "otherfolder"

if this occurs on a monday, the file name should use friday's date (e.g. on 10/30/2006 the file it finds should be named 10272006.txt

any ideas?

thanks~!!!
Comment
Watch Question

Awarded 2006
Top Expert 2006
Commented:
:: rename a folder to a date that is today or few days ago
@echo off
setlocal
set D=%date%
set y=%D:~10,4%
set m=%D:~4,2%
set dw=%D:~0,3%
set d=%D:~7,2%
set folder=c:\folder\
set file=sampledailyfile.txt
if not "%dw%"=="Mon" echo ren %folder%%file% %y%%m%%d%&goto :eof

-----
But to subtract a few days from the date, I've applied the complex code from http://www.experts-exchange.com/Q_21994705.html  "Setting the Date in a Batch file - Is there a better way?", to make this demo: http://www.sendspace.com/file/e2fbc6 ToDate.bat

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
Subtracting dates is really tricky in batch scripts.  You could adapt the one in DateMath.zip from here:
http://www.ss64.com/ntsyntax/index.html

Then to figure out the day of the week you could use something like this:

for /F "tokens=1" %%G in ('date /T') do (
 if [%%G]==[Mon] <command to subtract 3 days>) else (
 <command to subtract 1 day>
)
Top Expert 2007
Commented:
Try this:

@echo off

setlocal

call :GETDATEPARTS "%date%"

:SUBDAY

call :SUBTRACTDAYS 1

call :GETDAY %mm% %dd% %yy%

if "%day%"=="Sun" goto SUBDAY
if "%day%"=="Sat" goto SUBDAY

call :DISPLAYDATE

echo copy "c:\folder\sampledailyfile.txt" "c:\otherfolder\%mm%%dd%%yy%.txt"

goto :EOF

REM ***************************************************

:GETDATEPARTS

set dt=%~1
set tok=1-3

if "%dt:~0,1%" GTR "9" set tok=2-4

set yyyy=

for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
)

if not "%yyyy%"=="" set yy=%yyyy%

if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%

goto :EOF

REM ***************************************************

:GETDAY

if "%~3"=="" echo GETDAY requires 3 parameters: mm dd yy (4 digit)&goto :EOF

set wmm=%1
set wdd=%2
set wyyyy=%3

if 1%wmm% GTR 100 set /A wmm=1%wmm% - 100
if 2%wdd% GTR 100 set /A wdd=1%wdd% - 100

if %wmm% LSS 3 set /a wmm+=12&set /a wyyyy-=1

set /A dayNum=%dd% + (2 * %wmm%) + (3 * (%wmm% + 1) / 5) + %wyyyy% + (%wyyyy% / 4) - (%wyyyy% / 100) + (%wyyyy% / 400) + 1
set /A dayNum=%dayNum% %% 7

if %dayNum%==0 set day=Sun
if %dayNum%==1 set day=Mon
if %dayNum%==2 set day=Tue
if %dayNum%==3 set day=Wed
if %dayNum%==4 set day=Thu
if %dayNum%==5 set day=Fri
if %dayNum%==6 set day=Sat

goto :EOF

REM ***************************************************

:SUBTRACTDAYS

set dayCnt=%1

if "%dayCnt%"=="" set dayCnt=1

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 DONESUBTRACT

set /A mm=%mm% - 1

if /I %mm% GTR 0 goto ADJUSTDAY

set /A mm=12
set /A yy=%yy% - 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=%yy% %% 4

if not %tt%==0 goto SET28

set /A tt=%yy% %% 100

if not %tt%==0 goto SET29

set /A tt=%yy% %% 400

if %tt%==0 goto SET29

:SET28

set /A dd=28 + %dd%

goto CHKDAY

:SET29

set /A dd=29 + %dd%

goto CHKDAY

:DONESUBTRACT

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

goto :EOF

Good Luck,
Steve

Author

Commented:
Steve,

I can tell that yesterday's date is being calculated correctly, but it crashes when looking for "DISPLAYDATE" is this subroutine not defined? thank you so much for the help so far~!

Reid

Author

Commented:
Here is the exact message that I get:

The system cannot find the batch label specified – DISPLAYDATE
Copy “c:\folder\sampledailyfile.txt” “c:\otherfolder\11022006.txt”

thanks~!
Awarded 2006
Top Expert 2006

Commented:
reidtenkley, did you try the file from the sendspace link? Does exactly what you want...
Awarded 2006
Top Expert 2006

Commented:

Notes:
1) yyyy-mm-dd format used (easier sorting and less confusion over mm-dd vs. dd-mm)
2) "echo ren" commands for testing -> change to just "ren" in final
Top Expert 2007

Commented:
Remove this line. That was a debugging message and routine I pulled out before posting sorry :)

call :DISPLAYDATE

Author

Commented:
callrs and Steve,

thank you very much. I used some of each of your suggestions to create what I was after I will post it below

Author

Commented:
:: Complex & slower - before today
:: Adapted from http://www.experts-exchange.com/Q_21994705.html
::        Setting the Date in a Batch file - Is there a better way?

set D=%date%
set dw=%D:~0,3%

if not "%dw%"=="Mon" call :NOTMONDAY

if "%dw%"=="Mon" call :MONDAY

goto :EOF

:: ------------------------------------------
:MONDAY
call :GETDATEPARTS "%date%"
set today=%yy%%mm%%dd%
set folder=c:\folder\
set nwfolder=c:\folder\otherfolder\
set file=sampledailyfile.csv

move "c:\folder\sampledailyfile.csv" "c:\folder\otherfolder\sampledailyfile.csv"

call :SUBTRACTDAYS 3
set todayMinus3=%mm%%dd%%yy%
ren %nwfolder%%file% %todayMinus3%.csv&goto :EOF

:: ------------------------------------------
:NOTMONDAY
call :GETDATEPARTS "%date%"
set today=%yy%%mm%%dd%
set folder=c:\folder\
set nwfolder=c:\folder\otherfolder\
set file=sampledailyfile.csv

move "c:\folder\sampledailyfile.csv" "c:\folder\otherfolder\sampledailyfile.csv"

call :SUBTRACTDAYS 1
set todayMinus1=%mm%%dd%%yy%
ren %nwfolder%%file% %todayMinus1%.csv&goto :EOF

:: ------------------------------------------
:GETDATEPARTS
set dt=%~1
set tok=1-3
if "%dt:~0,1%" GTR "9" set tok=2-4
set yyyy=
for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set

%%y=%%b&set %%z=%%c
)

if not "%yyyy%"=="" set yy=%yyyy%
if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%

goto :EOF

:: ------------------------------------------
:DISPLAYDATE
echo Date: %yy%/%mm%/%dd%
goto :EOF

::------------------------------------------
:SUBTRACTDAYS
set dayCnt=%1
if "%dayCnt%"=="" set dayCnt=1
:: Substract your days here
set /A dd=1%dd% - 100 - %dayCnt%
set /A mm=1%mm% - 100

:CHKDAY
if /I %dd% GTR 0 goto DONESUBTRACT
set /A mm=%mm% - 1
if /I %mm% GTR 0 goto ADJUSTDAY
set /A mm=12
set /A yy=%yy% - 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=%yy% %% 4
if not %tt%==0 goto SET28
set /A tt=%yy% %% 100
if not %tt%==0 goto SET29
set /A tt=%yy% %% 400
if %tt%==0 goto SET29

:SET28
set /A dd=28 + %dd%
goto CHKDAY

:SET29
set /A dd=29 + %dd%
goto CHKDAY

:DONESUBTRACT
if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.