Solved

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

Posted on 2006-11-03
10
1,605 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

615 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