ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Deleting old files in directory

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

  • Deleting old files in directory

    Hello,

    I am looking for a method or program that goes into a network directory and deletes old files that are 7 days prior to today's date. Any suggestions. I was looking at Java but am very new to Java language.

    Thanks,

    David C

  • #2
    Re: Deleting old files in directory

    Here is the code to delete files in DOS batch.

    Code:
    @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%
    if /I %yyyy%/%mm%/%dd% GEQ %fyyyy%/%fmm%/%fdd% (
    erase %FileName%
    )
    
    set temp=
    set fyyyy=
    set fmm=
    set fdd=
    
    :EXIT

    Comment


    • #3
      Re: Deleting old files in directory

      David,

      Are you meaning network drive as server2000 drive or IFS on the iseries drive.
      Im asking cause I can help you on the IFS but as for the server.....Well thats a horse of a different color.

      Jamie
      All my answers were extracted from the "Big Dummy's Guide to the As400"
      and I take no responsibility for any of them.

      www.code400.com

      Comment


      • #4
        Re: Deleting old files in directory

        This looks like a great batch utility and I truly need to use it but for some reason it deletes all the files in the folder instead of only the ones older then the number I select.

        Can you help further?

        Bonrea

        Comment


        • #5
          Re: Deleting old files in directory

          Do it in VBS it's less code....

          Save this to "afile.vbs" then use "cscript afile.vbs {path}"

          Code:
          Option Explicit
          Dim FSO
          Set FSO = CreateObject("Scripting.FileSystemObject")
          DoDir FSO.GetFolder(WScript.Arguments.Item(0))
          Sub DoDir(Folder)
             On Error Resume Next
             Dim File, SubFolder
             For Each File In Folder.Files
                If File.DateLastModified < DateAdd("d", -7, Now) Then
                   WScript.StdOut.WriteLine "Deleted - " & File.Path
                   File.Delete True
                Else
                   WScript.StdOut.WriteLine "Ignored - " & File.Path
                End If
             Next
             For Each SubFolder in Folder.SubFolders
                DoDir SubFolder
             Next
          End Sub

          Comment


          • #6
            Re: Deleting old files in directory

            Thanks but I really need to run this in a DOS command type batch file so I can run it in an auto mode with the scheduler on Win2003 server.

            Bonrea

            Comment


            • #7
              Re: Deleting old files in directory

              Then just call the script from a batch file....

              Code:
              @echo off
              echo Running delete job....
              call cscript afile.vbs
              echo Delete job complete.
              exit

              Comment

              Working...
              X