• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Dos Scripting issue

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    Dos Scripting issue

    Afternoon, I have a grim task, I have to write a script that will purge file older than a define date on a window server, I am not able to install perl, cygwin or anything remotly useful

    I have been trawling the net & found this, however it is not working.... It trys to delete everything in the folder regarless of the date :-s

    It is in output mode at the moment as opposed to delete mode

    The input paratemeters are:

    USAGE:
    DELOLD /r X Y Z

    /r option provides a recursive file search

    X is the number of days previous to Today.
    Y is the directory to search
    Z is the a filter for files to search. This argument is optional
    and defaults to *.* (all files)

    Examples:
    "DELOLD 5 C:\temp" Deletes files older than 5 days
    in C:\temp
    "DELOLD /r 5 C:\temp" Deletes files older than 5 days
    in C:\temp and any of its
    subfolders
    "DELOLD 5 C:\temp *.txt" Deletes text files older than 5
    days in C:\temp
    Any ideas where ther error is??

    :: --------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:~3,2%
    set dd=%thedate:~0,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 |
    :: +--------------------------------------------------------------------+
    AYTEST

    :: 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. |
    :: +************************************************* ***********+
    ONE

    :: 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:~3,2%
    set fdd=%temp:~0,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-------------
    Last edited by aceboy; 19 April 2010, 15:16.

    #2
    Ay Carumba!

    Surely you can use WSH? That's got to be easier than "DOS" (it hasn't been DOS for a long time).

    If it was me I'd write a C++ console app to do what I want. Usually quicker than trying to find an existing solution.
    Will work inside IR35. Or for food.

    Comment


      #3
      Try the Forfiles command. If it's not on the server it can be found in the Resource Kit.

      Comment


        #4
        Code:
        forfiles /p "<directory>" /s /m *.* /d -30 /c "cmd /c echo @file >= "cleardown.txt" | del @file"
        Will delete files older than 30 days in <directory> and report it into cleardown.txt
        Still Invoicing

        Comment


          #5
          Dos scripting 2 sites worth using

          Rob van der Woude's Scripting Pages

          SS64.com Command line reference
          Always forgive your enemies; nothing annoys them so much.

          Comment


            #6
            If you do actually want to do this in DOS, the attrib command can be useful to set the archive bit on files older than a certain date. Then use DELETE to delete all files with archive bit set.

            Comment

            Working...
            X