reidtenkley
asked on
Rename file with previous weekday's date as the file name
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~!!!
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~!!!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
ASKER
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.t xt”
thanks~!
The system cannot find the batch label specified – DISPLAYDATE
Copy “c:\folder\sampledailyfile
thanks~!
reidtenkley, did you try the file from the sendspace link? Does exactly what you want...
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
Remove this line. That was a debugging message and routine I pulled out before posting sorry :)
call :DISPLAYDATE
call :DISPLAYDATE
ASKER
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
thank you very much. I used some of each of your suggestions to create what I was after I will post it below
ASKER
:: Complex & slower - before today
:: Adapted from https://www.experts-exchange.com/questions/21994705/Setting-the-Date-in-a-Batch-file-Is-there-a-better-way.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\otherfo lder\
set file=sampledailyfile.csv
move "c:\folder\sampledailyfile .csv" "c:\folder\otherfolder\sam pledailyfi le.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\otherfo lder\
set file=sampledailyfile.csv
move "c:\folder\sampledailyfile .csv" "c:\folder\otherfolder\sam pledailyfi le.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%
:: Adapted from https://www.experts-exchange.com/questions/21994705/Setting-the-Date-in-a-Batch-file-Is-there-a-better-way.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\otherfo
set file=sampledailyfile.csv
move "c:\folder\sampledailyfile
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\otherfo
set file=sampledailyfile.csv
move "c:\folder\sampledailyfile
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%
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>
)