how to create MS-DOS batch for deleting file/folder
Creating batch for deleting file/folder sounds so easy. So what's the problem here?
Well, the problem is, I want to have a batch file to create folder once a day, with folder naming rule: yyyy-mm-dd (i.e.: 2004-09-01 for August 1, 2004) and to delete a 2-days-ago-folder (i.e.: today is 2004-09-01, I want to delete folder 2004-08-30).
What made me confuse at first is how to get the date with format date I want. The code below is the answer:
for /f "tokens=2-4 delims=/ " %%f in ('date /t') do (
set mm=%%f
set dd=%%g
set yyyy=%%h
)
The second step is how to get 2 days ago date. I searched the internet (google.com) and found
experts-exchange.com
In that page, there is solution for getting 4 days ago date. Thanks to them (TrueBlue and SteveGTR) who contributed for the question and the solution in that page, I managed to finish my batch file.
Here is the batch file:
@echo off
::Get todays date
call :mmddnt
:: remember to change the path you want
:: attempting to delete old directory
:: (..\dirs\%yyyy%-%mm%-%dd%)
del "..\dirs\%yyyy%-%mm%-%dd%\*.xls"
rmdir "..\dirs\%yyyy%-%mm%-%dd%"
echo attempting to make directory
mkdir "..\dirs\%yyyy0%-%mm0%-%dd0%"
goto :eof
:mmddnt
::begin SteveGTR source code
for /f "tokens=2-4 delims=/ " %%f in ('date /t') do (
set mm=%%f
set dd=%%g
set yyyy=%%h
)
set dd0=%dd%
set mm0=%mm%
set yyyy0=%yyyy%
REM Substract 2 days
set /A dd=1%dd% - 102
set /A mm=1%mm% - 100
if /I %dd% GTR 0 goto DONE
set /A mm=%mm% - 1
if /I %mm% GTR 0 goto ADJUSTDAY
set /A mm=12
set /A yyyy=%yyyy% - 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
if %mm%==12 goto SET31
:SET31
set /A dd=31 + %dd%
goto DONE
:SET30
set /A dd=30 + %dd%
goto DONE
:LEAPCHK
set /A tt=%yyyy% %% 4
if not %tt%==0 goto SET28
set /A tt=%yyyy% %% 100
if not %tt%==0 goto SET29
set /A tt=%yyyy% %% 400
if %tt%==0 goto SET29
:SET28
set /A dd=28 + %dd%
goto DONE
:SET29
set /A dd=29 + %dd%
:DONE
if /i %dd% LSS 10 set dd=0%dd%
if /i %mm% LSS 10 set mm=0%mm%
::end SteveGTR source code
I've been thinking lately, I always depends on google when I'm stuck with my source code. Sometimes I think that this is not good, I mean, it looks like I copy paste some part of other's source code for my works, and you can say that it's not my own works 100%. Or in rude sentence, you can say it's like stealing some part of other's source code. That's why usually, I search it from tutorial pages, because that's what the purpose of the pages anyway. Anyone can learn from tutorial pages, but not to copy 100% from the source code, plus I should write the author name if there is GPL license for the code, right guys ^_~
0 Comments:
Post a Comment
<< Home