Cleaning up code help..

Home Forums Scripting General Scripting Cleaning up code help..

Viewing 1 post (of 1 total)
  • Author
    Posts
  • Avatar
    Lemons
    Member
    #128228

    Hey guys.. I’m new to VBScript and I’m just after a bit of help cleaning up a script, I have lots of duplicate code.. Any help or pointers would be much appreciated!

    Option Explicit

    On Error Resume Next

    Dim oFSO
    Dim sBaseDirectory
    Dim sDirectoryPath, sDirectoryPath1, sDirectoryPath2, ArchPath
    Dim oFolder, oFolder1, oFolder2, objFolder, objFolder1, objFolder2, objFolder3
    Dim oFileCollection, oFileCollection1, oFileCollection2
    Dim oFile, oFile1, oFile2
    Dim fExceptions, fDebug, fInfo, fOrderAudit

    Set oFSO = CreateObject(“Scripting.FileSystemObject”)
    sBaseDirectory = “D:_awcgateway”

    Call CheckArchFolders
    Call DelBuffer
    Call DelArchDebug
    Call DelArchExceptions
    Call MovingLogFiles

    Dim iDaysOld

    iDaysOld = 14

    Sub CheckArchFolders

    ArchPath = sBaseDirectory & “Archive”
    fExceptions = ArchPath & “Exceptions”
    fDebug = ArchPath & “Debug”
    fInfo = ArchPath & “Info”
    fOrderAudit = ArchPath & “OrderAudit”

    If oFSO.FolderExists(fExceptions) Then
    Set objFolder = oFSO.GetFolder(fExceptions)
    Else
    Set objFolder = oFSO.CreateFolder(fExceptions)
    End If

    If oFSO.FolderExists(fDebug) Then
    Set objFolder1 = oFSO.GetFolder(fDebug)
    Else
    Set objFolder1 = oFSO.CreateFolder(fDebug)
    End If

    If oFSO.FolderExists(fInfo) Then
    Set objFolder2 = oFSO.GetFolder(fInfo)
    Else
    Set objFolder2 = oFSO.CreateFolder(fInfo)
    End If

    If oFSO.FolderExists(fOrderAudit) Then
    Set objFolder3 = oFSO.GetFolder(fOrderAudit)
    Else
    Set objFolder3 = oFSO.CreateFolder(fOrderAudit)
    End If

    End Sub

    Sub DelBuffer

    sDirectoryPath = sBaseDirectory & “buffers”
    set oFolder = oFSO.GetFolder(sDirectoryPath)
    Set oFileCollection = oFolder.Files

    For each oFile in oFileCollection
    If oFile.DateLastModified < (Date() - iDaysOld) Then oFile.Delete(True) End If Next End Sub Sub DelArchDebug sDirectoryPath1 = sBaseDirectory & "archivedebug" set oFolder1 = oFSO.GetFolder(sDirectoryPath1) set oFileCollection1 = oFolder1.Files For each oFile1 in oFileCollection1 If oFile1.DateLastModified < (Date() - iDaysOld) Then oFile1.Delete(True) End If Next End Sub Sub DelArchExceptions sDirectoryPath2 = sBaseDirectory & "archiveexceptions" set oFolder2 = oFSO.GetFolder(sDirectoryPath2) set oFileCollection2 = oFolder2.Files For each oFile2 in oFileCollection2 If oFile2.DateLastModified < (Date() - iDaysOld) Then oFile2.Delete(True) End If Next End Sub Sub MovingFiles oFSO.MoveFile sBaseDirectory & "logsExceptions-*.*", sBaseDirectory & "ArchiveExceptions" oFSO.MoveFile sBaseDirectory & "logsDebug-*.*",sBaseDirectory & "ArchiveDebug" oFSO.MoveFile sBaseDirectory & "logsInfo-*.*",sBaseDirectory & "ArchiveInfo" oFSO.MoveFile sBaseDirectory & "logsOrderAudit-*.*",sBaseDirectory & "ArchiveOrderAudit" End Sub 'Cleanup! Set oFSO = Nothing Set oFolder = Nothing Set oFolder1 = Nothing Set oFolder2 = Nothing Set oFileCollection = Nothing Set oFileCollection1 = Nothing Set oFileCollection2 = Nothing Set oFile = Nothing Set oFile1 = Nothing Set oFile2 = Nothing Set sDirectoryPath = Nothing Set sDirectoryPath1 = Nothing Set sDirectoryPath2 = Nothing[/CODE][CODE]Option Explicit On Error Resume Next Dim oFSO
    Dim sBaseDirectory
    Dim sDirectoryPath, sDirectoryPath1, sDirectoryPath2, ArchPath
    Dim oFolder, oFolder1, oFolder2, objFolder, objFolder1, objFolder2, objFolder3
    Dim oFileCollection, oFileCollection1, oFileCollection2
    Dim oFile, oFile1, oFile2
    Dim fExceptions, fDebug, fInfo, fOrderAudit

    Set oFSO = CreateObject(“Scripting.FileSystemObject”)
    sBaseDirectory = “D:_awcgateway”

    Call CheckArchFolders
    Call DelBuffer
    Call DelArchDebug
    Call DelArchExceptions
    Call MovingLogFiles

    Dim iDaysOld

    iDaysOld = 14

    Sub CheckArchFolders

    ArchPath = sBaseDirectory & “Archive”
    fExceptions = ArchPath & “Exceptions”
    fDebug = ArchPath & “Debug”
    fInfo = ArchPath & “Info”
    fOrderAudit = ArchPath & “OrderAudit”

    If oFSO.FolderExists(fExceptions) Then
    Set objFolder = oFSO.GetFolder(fExceptions)
    Else
    Set objFolder = oFSO.CreateFolder(fExceptions)
    End If

    If oFSO.FolderExists(fDebug) Then
    Set objFolder1 = oFSO.GetFolder(fDebug)
    Else
    Set objFolder1 = oFSO.CreateFolder(fDebug)
    End If

    If oFSO.FolderExists(fInfo) Then
    Set objFolder2 = oFSO.GetFolder(fInfo)
    Else
    Set objFolder2 = oFSO.CreateFolder(fInfo)
    End If

    If oFSO.FolderExists(fOrderAudit) Then
    Set objFolder3 = oFSO.GetFolder(fOrderAudit)
    Else
    Set objFolder3 = oFSO.CreateFolder(fOrderAudit)
    End If

    End Sub

    Sub DelBuffer

    sDirectoryPath = sBaseDirectory & “buffers”
    set oFolder = oFSO.GetFolder(sDirectoryPath)
    Set oFileCollection = oFolder.Files

    For each oFile in oFileCollection
    If oFile.DateLastModified < (Date() - iDaysOld) Then
    oFile.Delete(True)
    End If
    Next

    End Sub

    Sub DelArchDebug

    sDirectoryPath1 = sBaseDirectory & “archivedebug”
    set oFolder1 = oFSO.GetFolder(sDirectoryPath1)
    set oFileCollection1 = oFolder1.Files

    For each oFile1 in oFileCollection1
    If oFile1.DateLastModified < (Date() - iDaysOld) Then
    oFile1.Delete(True)
    End If
    Next

    End Sub

    Sub DelArchExceptions

    sDirectoryPath2 = sBaseDirectory & “archiveexceptions”
    set oFolder2 = oFSO.GetFolder(sDirectoryPath2)
    set oFileCollection2 = oFolder2.Files

    For each oFile2 in oFileCollection2
    If oFile2.DateLastModified < (Date() - iDaysOld) Then
    oFile2.Delete(True)
    End If
    Next

    End Sub

    Sub MovingFiles

    oFSO.MoveFile sBaseDirectory & “logsExceptions-*.*”, sBaseDirectory & “ArchiveExceptions”
    oFSO.MoveFile sBaseDirectory & “logsDebug-*.*”,sBaseDirectory & “ArchiveDebug”
    oFSO.MoveFile sBaseDirectory & “logsInfo-*.*”,sBaseDirectory & “ArchiveInfo”
    oFSO.MoveFile sBaseDirectory & “logsOrderAudit-*.*”,sBaseDirectory & “ArchiveOrderAudit”

    End Sub

    ‘Cleanup!

    Set oFSO = Nothing
    Set oFolder = Nothing
    Set oFolder1 = Nothing
    Set oFolder2 = Nothing
    Set oFileCollection = Nothing
    Set oFileCollection1 = Nothing
    Set oFileCollection2 = Nothing
    Set oFile = Nothing
    Set oFile1 = Nothing
    Set oFile2 = Nothing
    Set sDirectoryPath = Nothing
    Set sDirectoryPath1 = Nothing
    Set sDirectoryPath2 = Nothing[/CODE]

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.