Solved

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

Posted on 2006-11-03
10
1,581 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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
 
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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

770 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