Deleting files and folders older than 12 months

Home Forums Scripting General Scripting Deleting files and folders older than 12 months

This topic contains 4 replies, has 2 voices, and was last updated by Avatar krokkie 8 years, 2 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • Avatar
    krokkie
    Member
    #157170

    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:

    cscript briefingbackup.vbs “FADSOUB” “FADS_OUB_”
    cscript briefingbackup.vbs “FADSPDFRTF” “FADS_PDF_RTF_”
    cscript briefingbackup.vbs FIPSINB FIPS_INB_
    cscript briefingbackup.vbs FIPSPDF FIPS_PDF_
    cscript briefingbackup.vbs “Fuel Orders” Fuel_Orders_
    cscript briefingbackup.vbs PFIBA320 PFIB_A320_
    cscript briefingbackup.vbs PFIBB737 PFIB_B737_
    cscript briefingbackup.vbs PFIBTASN PFIB_TASN_
    cscript briefingbackup.vbs PFIBTASW PFIB_TASW_
    cscript briefingbackup.vbs SQPlans SQ_Plans_
    cscript briefingbackup.vbs CIPlans CI_Plans_
    cscript briefingbackup.vbs TODC TODC_
    cscript briefingbackup.vbs WxTrakPDF WxTrak_PDF_
    cscript briefingbackup.vbs “WxTrakQix Log” WxTrak_Qix_Log_
    :cscript briefingbackup.vbs WxTrakSigWx WxTrak_SigWx_
    [/CODE]

    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 = “\aklns02wgroup06$flight DespatchDespatchersBriefingBackups”
    filelist = “D:Briefing_Backupsfilelist.lst”
    logfile = “D:Briefing_BackupsArchiveLog.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:WinzipWinzip32.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
    [/CODE]
    Any help with this would be much appreciated.[CODE]
    cscript briefingbackup.vbs “FADSOUB” “FADS_OUB_”
    cscript briefingbackup.vbs “FADSPDFRTF” “FADS_PDF_RTF_”
    cscript briefingbackup.vbs FIPSINB FIPS_INB_
    cscript briefingbackup.vbs FIPSPDF FIPS_PDF_
    cscript briefingbackup.vbs “Fuel Orders” Fuel_Orders_
    cscript briefingbackup.vbs PFIBA320 PFIB_A320_
    cscript briefingbackup.vbs PFIBB737 PFIB_B737_
    cscript briefingbackup.vbs PFIBTASN PFIB_TASN_
    cscript briefingbackup.vbs PFIBTASW PFIB_TASW_
    cscript briefingbackup.vbs SQPlans SQ_Plans_
    cscript briefingbackup.vbs CIPlans CI_Plans_
    cscript briefingbackup.vbs TODC TODC_
    cscript briefingbackup.vbs WxTrakPDF WxTrak_PDF_
    cscript briefingbackup.vbs “WxTrakQix Log” WxTrak_Qix_Log_
    :cscript briefingbackup.vbs WxTrakSigWx WxTrak_SigWx_
    [/CODE]

    This is the vbscript:

    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 = “\aklns02wgroup06$flight DespatchDespatchersBriefingBackups”
    filelist = “D:Briefing_Backupsfilelist.lst”
    logfile = “D:Briefing_BackupsArchiveLog.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:WinzipWinzip32.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
    [/CODE]
    Any help with this would be much appreciated.[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 = “\aklns02wgroup06$flight DespatchDespatchersBriefingBackups”
    filelist = “D:Briefing_Backupsfilelist.lst”
    logfile = “D:Briefing_BackupsArchiveLog.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:WinzipWinzip32.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
    [/CODE]
    Any help with this would be much appreciated.

    Avatar
    krokkie
    Member
    #339798

    Re: Deleting files and folders older than 12 months

    Dave

    i am not sure what you mean

    Avatar
    igor7
    Member
    #295079

    Re: Deleting files and folders older than 12 months

    Quote:
    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:

    Quote:
    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…

    Avatar
    krokkie
    Member
    #339799

    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

    Avatar
    krokkie
    Member
    #339800

    Re: Deleting files and folders older than 12 months

    Thanks Mate

    I’ve just tested it, and it works fine

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.