Solved

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

Posted on 2006-11-03
10
1,557 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~!!!
0
Comment
Question by:reidtenkley
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 30

Accepted Solution

by:
callrs earned 250 total points
ID: 17868849
:: 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
0
 
LVL 38

Expert Comment

by:Shift-3
ID: 17868851
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>
)
0
 
LVL 30

Assisted Solution

by:SteveGTR
SteveGTR earned 250 total points
ID: 17868880
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
0
 

Author Comment

by:reidtenkley
ID: 17869385
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
0
 

Author Comment

by:reidtenkley
ID: 17869416
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~!
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Expert Comment

by:callrs
ID: 17869429
reidtenkley, did you try the file from the sendspace link? Does exactly what you want...
0
 
LVL 30

Expert Comment

by:callrs
ID: 17869574

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
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 17869757
Remove this line. That was a debugging message and routine I pulled out before posting sorry :)

call :DISPLAYDATE
0
 

Author Comment

by:reidtenkley
ID: 17871005
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
0
 

Author Comment

by:reidtenkley
ID: 17871007
:: 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%
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now