vbscript order files in folder by datecreated

Latest post 06-06-2008 4:43 PM by Pat. 0 replies.
  • 06-06-2008 4:43 PM

    • Pat
    • Top 10 Contributor
      Male
    • Joined on 04-27-2008
    • Tempe, AZ
    • Posts 45

    vbscript order files in folder by datecreated

    Here is a script that will delete the oldest files in a folder until the folder is below the MaxFolderSize

    Const ZFPFolderPath = "\\C\Temp\"
    Const MaxFolderSize = 15
    Const ConvertToGigs = 1073741824
    Const adVarChar = 200
    Const MaxCharacters = 255
    Const adFldIsNullable = 32
    Const adDouble = 5 

    Set DataList = CreateObject("ADOR.Recordset")
    DataList.Fields.Append "FileName", adVarChar, MaxCharacters
    DataList.Fields.Append "FileDate", adVarChar, MaxCharacters
    DataList.Open

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(ZFPFolderPath)


    if objFolder.Size/ConvertToGigs > MaxFolderSize Then

        Set colFiles = objFolder.Files

        For Each objFile in colFiles
            DataList.AddNew
            DataList("FileName") = objFile.Path

            i = hour(objFile.DateCreated)
            if i < 10 then
                DataList("FileDate") = year(objFile.DateCreated) & "/" & month(objFile.DateCreated) & "/" & day(objFile.DateCreated) & " 0" & i & ":" & minute(objFile.DateCreated) & ":" & second(objFile.DateCreated)
            else
                DataList("FileDate") = year(objFile.DateCreated) & "/" & month(objFile.DateCreated) & "/" & day(objFile.DateCreated) & " " & i & ":" & minute(objFile.DateCreated) & ":" & second(objFile.DateCreated)
            End If

            DataList.Update
        Next

        DataList.Sort = "FileDate ASC"

        DataList.MoveFirst

        Do Until objFolder.Size/ConvertToGigs < MaxFolderSize        
            objFSO.DeleteFile(DataList.Fields.Item("FileName"))
            DataList.MoveNext
        Loop

    End If

    Patrick McNamara, BS-IS/CS, MBA, MAED
    ASP.NET Web Application Developer
    Asteryx, LLC.
    http://asteryx.com
    pat@asteryx.com

Page 1 of 1 (1 items) | RSS
Forums to discuss Microsoft ASP.Net Development and SQL
Powered by Community Server (Non-Commercial Edition), by Telligent Systems