#16
  1. 'fie' on me, allege-dly
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2003
    Location
    in da kitchen ...
    Posts
    12,889
    Rep Power
    6444
    If you wanna tackle it in perl check out globbing, and the stat function
    --Ax
    without exception, there is no rule ...
    Handmade Irish Jewellery
    Targeted Advertising Cookie Optout (TACO) extension for Firefox
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones


    09 F9 11 02
    9D 74 E3 5B
    D8 41 56 C5
    63 56 88 C0
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    -- Jamie Zawinski
    Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
    BIT COINS ANYONE
  2. #17
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2008
    Posts
    1
    Rep Power
    0
    Delete Files Older Than Date Using Batch Files

    This problem has nagged at me for years. Here is a batch command to delete files on a Windows 2003 machine.

    Forfiles -p z:\backup -s -m *.* -d -3 -c "cmd /c del /q @path"
    This will delete all files in my backup directory older than 3 days. To test it first, use this:

    Forfiles -p z:\backup -s -m *.* -d -3 -c "cmd /C Echo 0x22@Path\@File0x22"
    This will print out each file that you will be deleting.

  4. #18
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Posts
    1
    Rep Power
    0

    Removing files


    This script works great for files in your current directory. To get this to work recursively, I had to use for /r instead for the following line:

    for %%i in (*.*) do (
    set FileName=%%i
    call :PROCESSFILE %%~ti
    )

    Great script. Thanks. Now I don't have to install perl to do this.


    Originally Posted by Dameon51
    Problem. I'm a dumb @$$. Xcopy will only copy the files, not move them, so you'll still have them. I found this little batch on the web for you, but I haven't had time to disect it to see if it works yet...
    Also, it seems you are unfamiliar with scripts, so this might be a little complicated for you, but you'll learn something at least! Plus DOS scripts don't really get more complicated than this, so if you figure this out, dos batch files will be a breeze of you.


    Code:
    :: --------DELOLD.BAT----------
    @echo off
    SET OLDERTHAN=%1
    IF NOT DEFINED OLDERTHAN GOTO SYNTAX
    
    for /f "tokens=2" %%i in ('date /t') do set thedate=%%i
    
    set mm=%thedate:~0,2%
    set dd=%thedate:~3,2%
    set yyyy=%thedate:~6,4%
    
    set /A dd=%dd% - %OLDERTHAN%
    set /A mm=%mm% + 0
    
    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
    
    goto ERROR
    
    :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%
    for %%i in (*.*) do (
    set FileName=%%i
    call :PROCESSFILE %%~ti
    )
    
    set mm=
    set yyyy=
    set dd=
    set thedate=
    goto EXIT
    
    :SYNTAX
    ECHO.
    ECHO USAGE:
    ECHO DELOLD X
    ECHO   Where X is the number of days previous to Today.
    ECHO.
    ECHO EX: "DELOLD 5" Deletes files older than 5 days.
    GOTO EXIT
    
    :PROCESSFILE
    set temp=%1
    set fyyyy=20%temp:~6%
    set fmm=%temp:~0,2%
    set fdd=%temp:~3,2%
    if /I %fyyyy% GTR 2069 set fyyyy=19%temp:~6%
    
    
    :: +*************************************+
    :: | This is where the files are deleted |
    :: | Change the ECHO command to DEL to   |
    :: | delete. ECHO is used for test.      |
    :: +*************************************+
    if /I %yyyy%/%mm%/%dd% GEQ %fyyyy%/%fmm%/%fdd% (
    ECHO %FileName%
    )
    
    set temp=
    set fyyyy=
    set fmm=
    set fdd=
    
    :EXIT
    
    :: ----------END-DELOLD.BAT-------------
    
    View Solution
  6. #19
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Posts
    1
    Rep Power
    0
    Originally Posted by Faarooq
    This script works great for files in your current directory. To get this to work recursively, I had to use for /r instead for the following line:

    for %%i in (*.*) do (
    set FileName=%%i
    call :PROCESSFILE %%~ti
    )

    Great script. Thanks. Now I don't have to install perl to do this.

    Hi guys,

    im just a noobie for btach file scripting and i need to know how this code works. I mean when I tried to create a batch file, would i just run this script to delete files older than X days? How do i run this one?

    Thanks!
  8. #20
  9. Providing fuel for space ships
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2004
    Location
    nr Edinburgh, Scotland
    Posts
    14,382
    Rep Power
    3848
    What part of the batch file 'code' do you not understand ? If you tell us what part then hopefully we can explain it a bit better for you.
    The No Ma'am commandments:

    1.) It is O.K. to call hooters 'knockers' and sometimes snack trays
    2.) It is wrong to be French
    3.) It is O.K. to put all bad people in a giant meat grinder
    4.) Lawyers, see rule 3
    5.) It is O.K. to drive a gas guzzler if it helps you get babes
    6.) Everyone should car pool but me
    7.) Bring back the word 'stewardesses'
    8.) Synchronized swimming is not a sport
    9.) Mud wrestling is a sport
  10. #21
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0
    I have spent the morning analysing this batch code. I'm not bad at DOS batch and this is pretty complex! If you have any questions I will try to answer.

    The first thing I notice is that your server must have the date format set as US (mm/dd/yy). If it is not then this script will not work and you will have to modify it. If your date is set to US format, but has the full year (mm/dd/yyyy) then you appear to need to modify the :PROCESSFILE section:

    set fyyyy=20%temp:~6%
    set fmm=%temp:~0,2%
    set fdd=%temp:~3,2%
    if /I %fyyyy% GTR 2069 set fyyyy=19%temp:~6%

    to be

    set fyyyy=%temp:~6%
    set fmm=%temp:~0,2%
    set fdd=%temp:~3,2%
    rem if /I %fyyyy% GTR 2069 set fyyyy=19%temp:~6%

    Obviously you can check how your date is formated by typing in date/t at a command prompt, mine comes back as:

    C:\>date/t
    Mon 03/23/2009

    I'm still looking at this so if I find anymore 'helpful' things I will post them here.

    Feel free to ask questions and I will try to answer.
  12. #22
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    updated version of vat


    i was looking for a way to use a batch file to clean up some log file directories on our servers. i am running a win 2k server and forfile did not seem to be supported. rather than fanagling with admin packs i figured i would try out the delold.bat posted here.

    it provided a great framework. thanks for posting it.

    i did make some revisions and figured i would post them.

    i've added a /r option to provide recursive deletion. also, i added a few additional parameters...
    %1 still holds the day count
    %2 is for a base directory so batch does not have to be in the same directory as the files (if you want this to default to pwd then you can take the methodology from how %3 is handled)
    %3 will provide a file filter and is an optional parameter

    also, i had noticed the original bat would only make one adjustment for month or year changes. so, if you wanted to do something like "delold 90" it wouldn't make all the necessary adjustments. i added an additional goto to fix that.

    like the original batch posted here the actual del command is not in line. there is an echo command which should tell you which files are being targeted by the batch.

    i have done some basic tests with this batch. i have not used it in production. try some testing and use at your own risk.

    i am not a batch guru. any corrections would be appreciated.



    Code:
    :: --------DELOLD.BAT----------
    @echo off
    
    
    ::	test to see if first arg exists
    SET ARGTEST=%1
    IF NOT DEFINED ARGTEST GOTO SYNTAX
    
    :: 	initialize variable to control whether or not a
    ::	recursive file search is done in the TARGETDIR
    ::
    SET RECURSIVE=FALSE
    
    
    ::	check to see if recursive switch was passed
    IF %1==/r (
    	SET RECURSIVE=TRUE
    	SHIFT
    )
    
    ::	files older than this date will be deleted
    SET OLDERTHAN=%1
    IF NOT DEFINED OLDERTHAN GOTO SYNTAX
    
    ::	the directory containing files to be deleted
    SET TARGETDIR=%~f2
    IF NOT DEFINED TARGETDIR GOTO SYNTAX
    
    ::	filter for files to delete
    SET FILELIST=%3
    IF NOT DEFINED FILELIST (
    	SET FILELIST=*.*
    )
    
    ::	make sure TARGETDIR ends with a '\' for consistency
    IF NOT %TARGETDIR:~-1%==\ SET TARGETDIR=%TARGETDIR%\
    
    
    ::	for testing
    :: echo %RECURSIVE%
    :: echo %OLDERTHAN%
    :: echo %TARGETDIR%
    :: echo %FILELIST%
    
    
    :: 	get current date--ignore the first token which provides day of week
    for /f "tokens=2" %%i in ('date /t') do set thedate=%%i
    
    :: 	get date substrings
    set mm=%thedate:~0,2%
    set dd=%thedate:~3,2%
    set yyyy=%thedate:~6,4%
    
    :: 	initialize day and month of olderthan date current year is assumed
    set /A dd=%dd% - %OLDERTHAN%
    set /A mm=%mm% + 0
    
    
    goto DAYTEST
    
    
    
    :: +--------------------------------------------------------------------+
    :: |	test day value to see if month and/or year need to be altered	|
    :: +--------------------------------------------------------------------+
    :DAYTEST
    
    :: 	if olderthan date falls in current month
    if /I %dd% GTR 0 goto DONE
    
    :: 	adjust olderthan date to previous month and move to loop to id month
    set /A mm=%mm% - 1
    if /I %mm% GTR 0 goto ADJUSTDAY
    
    :: 	changes an olderthan month of 0 to 12 and subtracts one from olderthan year
    :: 	before forwarding to loop to id month
    set /A mm=12
    set /A yyyy=%yyyy% - 1
    
    goto ADJUSTDAY
    
    
    
    :: +------------------------------------------------------------------------------------+
    :: |	id month and send to loop to adjust day value to account for month change	|
    :: +------------------------------------------------------------------------------------+
    :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
    
    ::	month value is out of range
    goto ERROR
    
    
    :: +****************************+
    :: |	31 days in month	|
    :: +****************************+
    :SET31
    set /A dd=31 + %dd%
    goto DAYTEST
    
    
    :: +****************************+
    :: |	30 days in month	|
    :: +****************************+
    :SET30
    set /A dd=30 + %dd%
    goto DAYTEST
    
    
    :: +****************************+
    :: |	check for leap year	|
    :: +****************************+
    :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
    goto SET28
    
    
    :: +****************************+
    :: |	28 days in month	|
    :: +****************************+
    :SET28
    set /A dd=28 + %dd%
    goto DAYTEST
    
    
    :: +****************************+
    :: |	29 days in month	|
    :: +****************************+
    :SET29
    set /A dd=29 + %dd%
    goto DAYTEST
    
    
    
    :: +************************************************************+
    :: |	target olderthan date has been correctly identified.	|
    :: |	finish formatting the date and find files to process.	|
    :: +************************************************************+
    :DONE
    
    ::	add leading zero if day or month value is less than 10
    if /i %dd% LSS 10 set dd=0%dd%
    if /i %mm% LSS 10 set mm=0%mm%
    
    
    ::	check for files in directory and process them.
    :: 	"for /r" provides recursive search.
    ::
    IF %RECURSIVE%==TRUE (
    	for /r %TARGETDIR% %%i in (%FILELIST%) do (
    	set FileName=%%i
    	call :PROCESSFILE %%~ti
    	)
    ) ELSE (
    	for %%i in (%TARGETDIR%%FILELIST%) do (
    	set FileName=%%i
    	call :PROCESSFILE %%~ti
    	)
    )
    
    goto FINISH
    
    
    
    :: +************************************************************+
    :: |	Parse file date and delete files which are older	|
    :: |	than OLDERTHAN date					|
    :: +************************************************************+
    :PROCESSFILE
    
    ::	parse passed date string into month, day and year values
    set temp=%1
    set fyyyy=20%temp:~6%
    set fmm=%temp:~0,2%
    set fdd=%temp:~3,2%
    if /I %fyyyy% GTR 2069 set fyyyy=19%temp:~6%
    
    :: -||||||||||||||||||||||||||||||||||||||||||||||||||||-
    :: +	This is where the files are deleted 	  	*
    :: +	Change the ECHO command to DEL %FileName% 	*
    :: +	to delete. ECHO is used for test.         	*
    :: -||||||||||||||||||||||||||||||||||||||||||||||||||||-
    if /I %yyyy%/%mm%/%dd% GEQ %fyyyy%/%fmm%/%fdd% (	
    	ECHO The file %FileName% will be deleted
    )
    
    ::	clear variables used
    set temp=
    set fyyyy=
    set fmm=
    set fdd=
    
    goto EXIT
    
    
    
    :: +************************************************************+
    :: |	loop to address syntax errors when calling batch	|
    :: +************************************************************+	
    :SYNTAX
    ECHO.
    ECHO USAGE:
    ECHO 	DELOLD /r X Y Z
    ECHO.
    ECHO	/r option provides a recursive file search
    ECHO.
    ECHO	X is the number of days previous to Today.
    ECHO	Y is the directory to search
    ECHO	Z is the a filter for files to search.  This argument is optional
    ECHO	  and defaults to *.* (all files)
    ECHO.
    ECHO 	Examples: 
    ECHO		"DELOLD 5 C:\temp" 		Deletes files older than 5 days
    ECHO						  in C:\temp
    ECHO		"DELOLD /r 5 C:\temp"		Deletes files older than 5 days
    ECHO						  in C:\temp and any of its 
    ECHO						  subfolders
    ECHO		"DELOLD 5 C:\temp *.txt"	Deletes text files older than 5 
    ECHO						  days in C:\temp
    ECHO.
    ECHO.
    GOTO FINISH
    
    
    
    :: +************************************************************+
    :: +	loop to address errors in batch logic.			|
    :: +	currently only accessed when month for olderthan	|
    :: +	date is out of the expected range.			|
    :: +************************************************************+
    :ERROR
    ECHO.
    ECHO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    EcHO !	An error has occurred.  Stopping batch.		!
    ECHO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    ECHO.
    GOTO FINISH
    
    
    
    :: +************************************+
    :: |	clear variables used and exit	|
    :: +************************************+
    :FINISH
    set mm=
    set yyyy=
    set dd=
    set thedate=
    set ARGTEST=
    set RECURSIVE=
    set OLDERTHAN=
    set TARGETDIR=
    set FILELIST=
    set FileName=
    
    goto EXIT
    
    
    
    :EXIT
    
    
    :: ----------END-DELOLD.BAT-------------
  14. #23
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Quick and easy (yet dirty) solution


    Hi All,

    I had this as a side project for months and never really got around to trying to work it out as the little research I did involved coding pages upon pages.
    Anywho, I finally managed to get some time to check into a solution and here is what I came up with. It may be crude but it does the job.
    Note: deleting is for 3 day old files not 14

    I have 2 solutions (1 automated and the other requiring user input):

    Requiring User Input:
    set /P dir=Please enter the Directory to be purged (e.g c:\Data):
    cd %dir%
    set /P date1=Please enter date 3 days ago in format mm-dd-yyyy:
    mkdir temp
    xcopy /D:%date1% *.* temp
    del *.* /Q
    cd temp
    move *.* ../
    cd ..
    rmdir /S /Q temp


    Automated Solution:
    c:
    cd \Data
    mkdir temp
    set /A dd=%date:~4,2%-3
    set mm=%date:~7,2%
    set yyyy=%date10,4%
    xcopy /D:%mm%-%dd%-%yyyy% *.* temp
    del *.* /Q
    cd temp
    move *.* ../
    cd ..
    rmdir /S /Q temp
    You will notice with this solution that I am only looking at the date of the day and not the month. This is because it is only 3 days worth of data so figure it can wait a few extra days before purging those files.
    If you wanted to be strict on your purging then you would have to add some "if" statements in there


    Again, this is a quick and dirty solution. It is not my whole code, I do have some error checking etc in there but thought I would only show the relevant parts.
    I'm not the best batch programmer but I do what I can, I'm a networker so my solutions will not be as polished at the developers on this forum.

    Hope this helps
    Daz
  16. #24
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Exclamation Bug in delold.bat above


    There is an error in the 'delold' batch process above.

    :: initialize day and month of older than date current year is assumed
    set /A dd=%dd% - %OLDERTHAN%
    set /A mm=%mm% + 0

    Will not work for some reason when the day or month is 08 or 09.

    You need to change the code:
    rem set /A dd=%dd% - %OLDERTHAN%
    set dd=%dd%
    set /a dd=100%dd%%%100 -%OLDERTHAN%
    rem set /A mm=%mm% + 0
    set mm=%mm%
    set /a mm=100%mm%%%100 +0
  18. #25
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0
    Originally Posted by tvr450
    Will not work for some reason when the day or month is 08 or 09.
    I actually noticed this when I was creating my solutions (above). Seems to work for every other day I have tried except for those 2. That is why I came up with the solution requiring user input to get around that issue
  20. #26
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0
    Originally Posted by tvr450
    There is an error in the 'delold' batch process above.

    :: initialize day and month of older than date current year is assumed
    set /A dd=%dd% - %OLDERTHAN%
    set /A mm=%mm% + 0

    Will not work for some reason when the day or month is 08 or 09.

    You need to change the code:
    rem set /A dd=%dd% - %OLDERTHAN%
    set dd=%dd%
    set /a dd=100%dd%%%100 -%OLDERTHAN%
    rem set /A mm=%mm% + 0
    set mm=%mm%
    set /a mm=100%mm%%%100 +0

    nice catch... yes windows considers those as octals and 08 and 09 are not valid octal numbers

    here is corrected code

    Code:
    :: --------DELOLD.BAT----------
    @echo off
    
    
    ::	test to see if first arg exists
    SET ARGTEST=%1
    IF NOT DEFINED ARGTEST GOTO SYNTAX
    
    :: 	initialize variable to control whether or not a
    ::	recursive file search is done in the TARGETDIR
    ::
    SET RECURSIVE=FALSE
    
    
    ::	check to see if recursive switch was passed
    IF %1==/r (
    	SET RECURSIVE=TRUE
    	SHIFT
    )
    
    ::	files older than this date will be deleted
    SET OLDERTHAN=%1
    IF NOT DEFINED OLDERTHAN GOTO SYNTAX
    
    ::	the directory containing files to be deleted
    SET TARGETDIR=%~f2
    IF NOT DEFINED TARGETDIR GOTO SYNTAX
    
    ::	filter for files to delete
    SET FILELIST=%3
    IF NOT DEFINED FILELIST (
    	SET FILELIST=*.*
    )
    
    ::	make sure TARGETDIR ends with a '\' for consistency
    IF NOT %TARGETDIR:~-1%==\ SET TARGETDIR=%TARGETDIR%\
    
    ::	for testing
    :: echo %RECURSIVE%
    :: echo %OLDERTHAN%
    :: echo %TARGETDIR%
    :: echo %FILELIST%
    
    :: 	get current date--ignore the first token which provides day of week
    for /f "tokens=2" %%i in ('date /t') do set thedate=%%i
    
    :: 	get date substrings
    set mm=%thedate:~0,2%
    set dd=%thedate:~3,2%
    set yyyy=%thedate:~6,4%
    
    :: 	initialize day and month of olderthan date current year is assumed
    set /A dd=1%dd% - 100 - %OLDERTHAN%
    set /A mm=1%mm% - 100
    
    
    goto DAYTEST
    
    
    
    :: +--------------------------------------------------------------------+
    :: |	test day value to see if month and/or year need to be altered	|
    :: +--------------------------------------------------------------------+
    :DAYTEST
    
    :: 	if olderthan date falls in current month
    if /I %dd% GTR 0 goto DONE
    
    :: 	adjust olderthan date to previous month and move to loop to id month
    set /A mm=%mm% - 1
    if /I %mm% GTR 0 goto ADJUSTDAY
    
    :: 	changes an olderthan month of 0 to 12 and subtracts one from olderthan year
    :: 	before forwarding to loop to id month
    set /A mm=12
    set /A yyyy=%yyyy% - 1
    
    goto ADJUSTDAY
    
    
    
    :: +------------------------------------------------------------------------------------+
    :: |	id month and send to loop to adjust day value to account for month change		|
    :: +------------------------------------------------------------------------------------+
    :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
    
    ::	month value is out of range
    goto ERROR
    
    
    :: +****************************+
    :: |	31 days in month		|
    :: +****************************+
    :SET31
    set /A dd=31 + %dd%
    goto DAYTEST
    
    
    :: +****************************+
    :: |	30 days in month		|
    :: +****************************+
    :SET30
    set /A dd=30 + %dd%
    goto DAYTEST
    
    
    :: +****************************+
    :: |	check for leap year		|
    :: +****************************+
    :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
    goto SET28
    
    
    :: +****************************+
    :: |	28 days in month		|
    :: +****************************+
    :SET28
    set /A dd=28 + %dd%
    goto DAYTEST
    
    
    :: +****************************+
    :: |	29 days in month		|
    :: +****************************+
    :SET29
    set /A dd=29 + %dd%
    goto DAYTEST
    
    
    
    :: +************************************************************+
    :: |	target olderthan date has been correctly identified.	|
    :: |	finish formatting the date and find files to process.	|
    :: +************************************************************+
    :DONE
    
    ::	add leading zero if day or month value is less than 10
    if /i %dd% LSS 10 set dd=0%dd%
    if /i %mm% LSS 10 set mm=0%mm%
    
    
    ::	check for files in directory and process them.
    :: 	"for /r" provides recursive search.
    ::
    IF %RECURSIVE%==TRUE (
    	for /r %TARGETDIR% %%i in (%FILELIST%) do (
    	set FileName=%%i
    	call :PROCESSFILE %%~ti
    	)
    ) ELSE (
    	for %%i in (%TARGETDIR%%FILELIST%) do (
    	set FileName=%%i
    	call :PROCESSFILE %%~ti
    	)
    )
    
    goto FINISH
    
    
    
    :: +************************************************************+
    :: |	Parse file date and delete files which are older		|
    :: |	than OLDERTHAN date										|
    :: +************************************************************+
    :PROCESSFILE
    
    ::	parse passed date string into month, day and year values
    set temp=%1
    set fyyyy=20%temp:~6%
    set fmm=%temp:~0,2%
    set fdd=%temp:~3,2%
    if /I %fyyyy% GTR 2069 set fyyyy=19%temp:~6%
    
    :: -||||||||||||||||||||||||||||||||||||||||||||||||||||-
    :: +	This is where the files are deleted 	  		*
    :: +	Change the ECHO command to DEL %FileName% 		*
    :: +	to delete. ECHO is used for test.         		*
    :: +	ECHO The file %FileName% will be deleted		*
    :: -||||||||||||||||||||||||||||||||||||||||||||||||||||-
    if /I %yyyy%/%mm%/%dd% GEQ %fyyyy%/%fmm%/%fdd% (	
    	ECHO The file %FileName% will be deleted
    )
    
    ::	clear variables used
    set temp=
    set fyyyy=
    set fmm=
    set fdd=
    
    goto EXIT
    
    
    
    :: +************************************************************+
    :: |	loop to address syntax errors when calling batch		|
    :: +************************************************************+	
    :SYNTAX
    ECHO.
    ECHO USAGE:
    ECHO 	DELOLD /r X Y Z
    ECHO.
    ECHO	/r option provides a recursive file search
    ECHO.
    ECHO	X is the number of days previous to Today.
    ECHO	Y is the directory to search
    ECHO	Z is the a filter for files to search.  This argument is optional
    ECHO	  and defaults to *.* (all files)
    ECHO.
    ECHO 	Examples: 
    ECHO		"DELOLD 5 C:\temp" 		Deletes files older than 5 days
    ECHO						  in C:\temp
    ECHO		"DELOLD /r 5 C:\temp"		Deletes files older than 5 days
    ECHO						  in C:\temp and any of its 
    ECHO						  subfolders
    ECHO		"DELOLD 5 C:\temp *.txt"	Deletes text files older than 5 
    ECHO						  days in C:\temp
    ECHO.
    ECHO.
    GOTO FINISH
    
    
    
    :: +************************************************************+
    :: +	loop to address errors in batch logic.					|
    :: +	currently only accessed when month for olderthan		|
    :: +	date is out of the expected range.						|
    :: +************************************************************+
    :ERROR
    ECHO.
    ECHO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    EcHO !	An error has occurred.  Stopping batch.		!
    ECHO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    ECHO.
    GOTO FINISH
    
    
    
    :: +************************************+
    :: |	clear variables used and exit	|
    :: +************************************+
    :FINISH
    set mm=
    set yyyy=
    set dd=
    set thedate=
    set ARGTEST=
    set RECURSIVE=
    set OLDERTHAN=
    set TARGETDIR=
    set FILELIST=
    set FileName=
    
    goto EXIT
    
    
    
    :EXIT
    
    
    :: ----------END-DELOLD.BAT-------------
    As usual, line 199 echoes the filename(s) to be deleted rather than deleting it/them. When you're ready change it to:
    DEL %FileName%
  22. #27
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    1
    Rep Power
    0
    Nice batch. I edited it to suit the Oz date format dd-mm-yyyy.

    As before its currently set to only list files to be deleted.

    Peter

    Code:
    :: --------DELOLD.BAT----------
    @echo off
    
    
    ::	test to see if first arg exists
    SET ARGTEST=%1
    IF NOT DEFINED ARGTEST GOTO SYNTAX
    
    :: 	initialize variable to control whether or not a
    ::	recursive file search is done in the TARGETDIR
    ::
    SET RECURSIVE=FALSE
    
    
    ::	check to see if recursive switch was passed
    IF %1==/r (
    	SET RECURSIVE=TRUE
    	SHIFT
    )
    
    ::	files older than this date will be deleted
    SET OLDERTHAN=%1
    IF NOT DEFINED OLDERTHAN GOTO SYNTAX
    
    ::	the directory containing files to be deleted
    SET TARGETDIR=%~f2
    IF NOT DEFINED TARGETDIR GOTO SYNTAX
    
    ::	filter for files to delete
    SET FILELIST=%3
    IF NOT DEFINED FILELIST (
    	SET FILELIST=*.*
    )
    
    ::	make sure TARGETDIR ends with a '\' for consistency
    IF NOT %TARGETDIR:~-1%==\ SET TARGETDIR=%TARGETDIR%\
    
    ::	for testing
    :: echo %RECURSIVE%
    :: echo %OLDERTHAN%
    :: echo %TARGETDIR%
    :: echo %FILELIST%
    
    :: 	get current date--ignore the first token which provides day of week
    for /f "tokens=2" %%i in ('date /t') do set thedate=%%i
    
    :: 	get date substrings
    set dd=%thedate:~0,2%
    set mm=%thedate:~3,2%
    set yyyy=%thedate:~6,4%
    
    :: 	initialize day and month of olderthan date current year is assumed
    set /A dd=1%dd% - 100 - %OLDERTHAN%
    set /A mm=1%mm% - 100
    
    goto DAYTEST
    
    
    
    :: +--------------------------------------------------------------------+
    :: |	test day value to see if month and/or year need to be altered	|
    :: +--------------------------------------------------------------------+
    :DAYTEST
    
    :: 	if olderthan date falls in current month
    if /I %dd% GTR 0 goto DONE
    
    :: 	adjust olderthan date to previous month and move to loop to id month
    set /A mm=%mm% - 1
    if /I %mm% GTR 0 goto ADJUSTDAY
    
    :: 	changes an olderthan month of 0 to 12 and subtracts one from olderthan year
    :: 	before forwarding to loop to id month
    set /A mm=12
    set /A yyyy=%yyyy% - 1
    
    goto ADJUSTDAY
    
    
    
    :: +------------------------------------------------------------------------------------+
    :: |	id month and send to loop to adjust day value to account for month change		|
    :: +------------------------------------------------------------------------------------+
    :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
    
    ::	month value is out of range
    goto ERROR
    
    
    :: +****************************+
    :: |	31 days in month		|
    :: +****************************+
    :SET31
    set /A dd=31 + %dd%
    goto DAYTEST
    
    
    :: +****************************+
    :: |	30 days in month		|
    :: +****************************+
    :SET30
    set /A dd=30 + %dd%
    goto DAYTEST
    
    
    :: +****************************+
    :: |	check for leap year		|
    :: +****************************+
    :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
    goto SET28
    
    
    :: +****************************+
    :: |	28 days in month		|
    :: +****************************+
    :SET28
    set /A dd=28 + %dd%
    goto DAYTEST
    
    
    :: +****************************+
    :: |	29 days in month		|
    :: +****************************+
    :SET29
    set /A dd=29 + %dd%
    goto DAYTEST
    
    
    
    :: +************************************************************+
    :: |	target olderthan date has been correctly identified.	|
    :: |	finish formatting the date and find files to process.	|
    :: +************************************************************+
    :DONE
    
    ::	add leading zero if day or month value is less than 10
    if /i %dd% LSS 10 set dd=0%dd%
    if /i %mm% LSS 10 set mm=0%mm%
    
    ::echo %dd%
    ::echo %mm%
    ::echo %yyyy%
    
    
    
    ::	check for files in directory and process them.
    :: 	"for /r" provides recursive search.
    ::
    IF %RECURSIVE%==TRUE (
    	for /r %TARGETDIR% %%i in (%FILELIST%) do (
    	set FileName=%%i
    	call :PROCESSFILE %%~ti
    	)
    ) ELSE (
    	for %%i in (%TARGETDIR%%FILELIST%) do (
    	set FileName=%%i
    	call :PROCESSFILE %%~ti
    	)
    )
    
    goto FINISH
    
    
    
    :: +************************************************************+
    :: |	Parse file date and delete files which are older		|
    :: |	than OLDERTHAN date										|
    :: +************************************************************+
    :PROCESSFILE
    
    ::	parse passed date string into month, day and year values
    
    set temp=%1
    set fyyyy=20%temp:~8%
    set fdd=%temp:~0,2%
    set fmm=%temp:~3,2%
    
    if /I %fyyyy% GTR 2069 set fyyyy=19%temp:~6%
    
    :: -||||||||||||||||||||||||||||||||||||||||||||||||||||-
    :: +	This is where the files are deleted 	  		*
    :: +	Change the ECHO command to DEL %FileName% 		*
    :: +	to delete. ECHO is used for test.         		*
    :: +	ECHO The file %FileName% will be deleted		*
    :: -||||||||||||||||||||||||||||||||||||||||||||||||||||-
    if /I %fdd%/%fmm%/%fyyyy% GEQ %dd%/%mm%/%yyyy% (	
    	ECHO The file %FileName% will be deleted
    )
    
    ::	clear variables used
    set temp=
    set fyyyy=
    set fmm=
    set fdd=
    
    goto EXIT
    
    
    
    :: +************************************************************+
    :: |	loop to address syntax errors when calling batch		|
    :: +************************************************************+	
    :SYNTAX
    ECHO.
    ECHO USAGE:
    ECHO 	DELOLD /r X Y Z
    ECHO.
    ECHO	/r option provides a recursive file search
    ECHO.
    ECHO	X is the number of days previous to Today.
    ECHO	Y is the directory to search
    ECHO	Z is the a filter for files to search.  This argument is optional
    ECHO	  and defaults to *.* (all files)
    ECHO.
    ECHO 	Examples: 
    ECHO		"DELOLD 5 C:\temp" 		Deletes files older than 5 days
    ECHO						  in C:\temp
    ECHO		"DELOLD /r 5 C:\temp"		Deletes files older than 5 days
    ECHO						  in C:\temp and any of its 
    ECHO						  subfolders
    ECHO		"DELOLD 5 C:\temp *.txt"	Deletes text files older than 5 
    ECHO						  days in C:\temp
    ECHO.
    ECHO.
    GOTO FINISH
    
    
    
    :: +************************************************************+
    :: +	loop to address errors in batch logic.					|
    :: +	currently only accessed when month for olderthan		|
    :: +	date is out of the expected range.						|
    :: +************************************************************+
    :ERROR
    ECHO.
    ECHO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    EcHO !	An error has occurred.  Stopping batch.		!
    ECHO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    ECHO.
    GOTO FINISH
    
    
    
    :: +************************************+
    :: |	clear variables used and exit	|
    :: +************************************+
    :FINISH
    set mm=
    set yyyy=
    set dd=
    set thedate=
    set ARGTEST=
    set RECURSIVE=
    set OLDERTHAN=
    set TARGETDIR=
    set FILELIST=
    set FileName=
    
    goto EXIT
    
    
    
    :EXIT
  24. #28
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0
    All of these are way to complicated, the first guy (Dameon51) with xcopy had the right idea, just needed an update, I have added loggin which makes it more complex than it needs to be but I like logs, I used this with another to cleanup the logs, Dameon51 is the one who gave me the idea.

    Code:
    set thedate=%date:~4,10%
    mkdir %temp%\folder-temp
    robocopy C:\folder\ %temp%\folder-temp /E /MOVE /MINAGE:14 /log+:c:\logs\%thedate:/=-%Folder-delete.log 
    rmdir /s /q %temp%\folder-temp
    Remove the log bit (and you don't need the set date variable line) and you have it down to three lines of code.
  26. #29
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Question Deleting files older than specific date...


    I would like to re-open this thread as it's kind of similar to what I'm trying to do at the moment. Scenario is similar I want to create a bat file responsible for deleting files older than specific date. I have created initial bat file, which works perfectly fine.

    net use z: \\oldfestini\install$ /user:administrator
    : please provide correct password
    start z:\callp_backup
    start z:\callp_temp
    xcopy Z:\CallP_Backup z:\CallP_TEMP /D:06-15-09
    del z:\callp_backup
    xcopy Z:\callp_temp Z:\callp_backup
    pause

    The problem is that I want to schedule this file as a task. In order to do so I would have to change the date every single time I want to re-run the bat file. I was wonder is there any way to get a current date from the system (possibly as a variable) and then use it as a reference point. So it would be something like: delete everything for "today - 2 days"
    BTW I'm not expert in DOS but if you give me a clue I should figure it out
  28. #30
  29. No Profile Picture
    Self-banned
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2004
    Location
    http://middle.nowhere.com
    Posts
    2,288
    Rep Power
    3606
    First, welcome to DevShed.

    Second, it's usually bad form to post a new question to an old thread, even if the topics are related. By asking your own question in the original poster's thread, it's like being at your friend's birthday party, and shouting, "Hey, it's my birthday, too!" even if no harm is intended. You can have a moderator split this topic out into its own thread--just send one of them a private message.

    Third, I found several answers to your question with a very quick Google search for "dos batch get yesterday's date" (which would give me an idea of how to get even further back dates). We're happy to help you, and glad you posted code, which shows you've already put in some effort, but always search first, or you may only receive remarks pointing you to Google.

    Finally, I would recommend moving away from DOS batch files. Look into Windows Scripting Host or something more robust like Perl. They should be able to do everything you're trying to do.

IMN logo majestic logo threadwatch logo seochat tools logo