Announcement

Collapse
No announcement yet.

Deleting files and folders older than 12 months

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

  • Deleting files and folders older than 12 months

    I have a .bat file that runs a vb script to archive files every month, but I also need either the .bat file or the vb script to delete any folders older than 12 months

    This is the bat file:
    Code:
    cscript briefingbackup.vbs "FADS\OUB" "FADS_OUB_"
    cscript briefingbackup.vbs "FADS\PDFRTF" "FADS_PDF_RTF_"
    cscript briefingbackup.vbs FIPS\INB FIPS_INB_
    cscript briefingbackup.vbs FIPS\PDF FIPS_PDF_
    cscript briefingbackup.vbs "Fuel Orders" Fuel_Orders_
    cscript briefingbackup.vbs PFIB\A320 PFIB_A320_
    cscript briefingbackup.vbs PFIB\B737 PFIB_B737_
    cscript briefingbackup.vbs PFIB\TASN PFIB_TASN_
    cscript briefingbackup.vbs PFIB\TASW PFIB_TASW_
    cscript briefingbackup.vbs SQPlans SQ_Plans_
    cscript briefingbackup.vbs CIPlans CI_Plans_
    cscript briefingbackup.vbs TODC TODC_
    cscript briefingbackup.vbs WxTrak\PDF WxTrak_PDF_
    cscript briefingbackup.vbs "WxTrak\Qix Log" WxTrak_Qix_Log_
    :cscript briefingbackup.vbs WxTrak\SigWx WxTrak_SigWx_
    This is the vbscript:
    Code:
     
    Dim source_folder, destination_folder, archive_file, filelist, tt
    d = Date : pre = DateSerial(year(d),month(d),0)
    
    fstDayCurrentMonth = DateSerial(year(d),month(d),1)
    lstDayPrePrevMonth = DateSerial(year(pre),month(pre),0)
    '**********************************************************************
    ' THESE ARE THE CONSTANT VALUES
    '**********************************************************************
       source_parent      = "D:\Briefing_Backups"
       destination_parent = "\\aklns02w\group06$\flight Despatch\Despatchers\BriefingBackups"
       filelist           = "D:\Briefing_Backups\filelist.lst"
       logfile = "D:\Briefing_Backups\ArchiveLog.txt"  ' (made it 'evergrowing' in this sample)
    '**********************************************************************
    ' GET THE ARGUMENTS OBJECT
    '**********************************************************************
       Set ArgObj = WScript.Arguments
       var1 = ArgObj(0) : var2 = ArgObj(1)
    '- # -                               - # -
    dStamp = Monthname(Month(pre)) & "_" & Year(pre)
    source_folder = source_parent & "\" & var1
    destination_folder = destination_parent & "\" & dStamp
    archive_file = var2 & "_" & dStamp & ".zip"
    tt=0
    BackupFiles lstDayPrePrevMonth, fstDayCurrentMonth
    wsh.echo "Total days in backup: " & _
         1+DateDiff("d", lstDayPrePrevMonth, fstDayCurrentMonth) _
         & vbNewLine & "Number of Files backed up:", iCnt
    wscript.quit
    
    Sub BackupFiles (d_A,d_B)
       Dim FSO, oSourceFolder, oFile, iCnt
       Dim dLM, dA, dB, runcmd, appWinZip
       
       appWinZip = "D:\Winzip\Winzip32.exe"
       dA = Year(d_A)*1e4 + Month(d_A)*1e2 + Day(d_A)
       dB = Year(d_B)*1e4 + Month(d_B)*1e2 + Day(d_B)
       iCnt = 0
       Set FSO = CreateObject("Scripting.FileSystemObject")
       If FSO.FolderExists(destination_folder) = False _
         Then FSO.CreateFolder(destination_folder)
       Set oSourceFolder = FSO.GetFolder(source_folder)
    
       ' Create the list of files to be zipped
       With FSO.CreateTextFile(filelist,true)
         For Each oFile in oSourceFolder.Files
           dLM = oFile.DateLastModified
           dLM = Year(dLM)*1e4 + Month(dLM)*1e2 + Day(dLM)
           If (dLM >= dA) AND (dLM =< dB) Then
               .writeline source_folder & "\" & oFile.name
               iCnt = iCnt + 1
           End If
         Next  
         .close
       End With
      If iCnt > 0 Then 
       
         
      ' populate archive according the filelist
       runcmd = "cmd /c title Backup to ZIP & COLOR 72 &"
       runcmd = runcmd & " (echo.* %date% %time%: " _
               & archive_file & " -Started- & echo.processing " _
               & iCnt & " files. & Start """"/w"
       runcmd = runcmd & " """ & appWinZip & """ -min -a """ _
               & destination_folder & "\" & archive_file _
               & """ @""" & filelist & """"
       runcmd = runcmd & " & echo. %date% %time%: " _
               & archive_file & " -Done- & echo.___"
       runcmd = runcmd & " )>>""" & logfile & """ 2>&1"
       CreateObject("WScript.Shell").Run runcmd, 0, True
       tt=tt + iCnt
       rem FSO.deletefile filelist
      End If
    End Sub
    Any help with this would be much appreciated.
    Last edited by Rems; 16th December 2011, 16:24.

  • #2
    Re: Deleting files and folders older than 12 months

    Dave

    i am not sure what you mean

    Comment


    • #3
      Re: Deleting files and folders older than 12 months

      I also need either the .bat file or the vb script to delete any folders older than 12 months
      May be something like this will suits your needs:

      Dim i, fso, f, f1, sf, BasePath, CalcResult, fNameArray()
      BasePath = "Your path here"
      Set fso = CreateObject("Scripting.FileSystemObject")
      Set f = fso.GetFolder(BasePath)
      Set sf = f.SubFolders
      For Each f1 in sf
      CalcResult = DateDiff("d",f1.DateCreated,Now)
      if CalcResult > 365 then 'desired number of days (365 = 1 year)
      ReDim preserve fNameArray(i)
      fNameArray(i) = f1.Name
      i = i + 1
      end if
      Next

      For Each fName in fNameArray
      FSO.DeleteFolder(BasePath & "\" & fName)
      Next
      Save it as "DelFolder.vbs" and try...

      Comment


      • #4
        Re: Deleting files and folders older than 12 months

        Thx Igor7, only saw your response today, was on leave for a month

        I will give it a go

        Comment


        • #5
          Re: Deleting files and folders older than 12 months

          Thanks Mate

          I've just tested it, and it works fine

          Comment

          Working...
          X