Archiving throught VBS script

Hello Experts

I just made and test a functional vbs script

basically the script looks through a source folder and move to a created folder (with archiving bit enabled)

I just want to have your input on my script and want to know if it could be optimized
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I formatted the code for improved readability.
I precomputed the two date values, simplifying expressions, when possible.
I added a next iteration code skip in one of the loops.  Article reference on this technique below the code snippet.
strComputer = "."
sLogFolder = "D:\Folder"
dLogFolder = "Z:\Folder"
const months = 6
const years7 = 7

'strDate = "12/" & "31/" & DatePart("yyyy", DateAdd("yyyy",-0,Now))
strDate = "12/" & "31/" & Year(Now)
dtYr_7 = dateadd("yyyy", -years7 , CDate(strDate))
dtMonth_6 = dateadd("m", -months, Now)

Set objFSO = CreateObject("Scripting.FileSystemObject")

'For Each objItem in colItems
If objFSO.FolderExists(sLogFolder) Then
    CreateArchiveFolder dLogFolder
    EpurationDir sLogFolder
    EpurationDirArchive dLogFolder
End If
Set objFSO = Nothing

Function EpurationDir (strMyPath)
    ShowSubFolders objFSO.GetFolder(strMyPath)
End Function

Sub ShowSubFolders(Folder)
    Dim sourceFolder
    Dim DestinationSubFolder

    For Each Subfolder in Folder.SubFolders
        Set oFolder = objFSO.GetFolder(Subfolder.Path)
        CreateSubArchiveFolder dLogFolder & Mid(subfolder.path, Len(sLogFolder)+1) & "\"

        '***Supression des fichiers dans D:\OC\Log\APP           
        For Each file In oFolder.Files
            Do  '<<GROUP

                If file.DateLastModified < dtYr_7 then
                    'WScript.Echo path & "  deleted: " & file.path
                    Exit Do
                End If

                '***Deplacement des fichiers de D:\OC\Log\APP vers L:\Archive_Legales
                If file.DateLastModified < dtMonth_6 Then
                    sourceFolder = file.path
                    DestinationSubFolder = dLogFolder & Mid(file.path, Len(sLogFolder)+1)
                    objFSO.MoveFile sourceFolder, DestinationSubFolder
                    'WScript.Echo path & "  copy: " & file.path
                End If
            Loop While False    '>>GROUP

        ShowSubFolders Subfolder
    Set oFolder = Nothing
End Sub

'***Supression des fichiers dans Archive_Legales 
Function EpurationDirArchive(strArchivePathEpur)        
    If objFSO.FolderExists(strArchivePathEpur) Then
        ShowSubFoldersArchive objFSO.GetFolder(strArchivePathEpur)        
    End If
End Function

Sub ShowSubFoldersArchive(Folder)

    For Each Subfolder in Folder.SubFolders
        Set oFolder2 = objFSO.GetFolder(Subfolder.Path)

        ShowSubFolders Subfolder

        For Each file In oFolder2.Files
            If file.DateLastModified < dtYr_7 then
                'WScript.Echo file.Name & " last modified at " & file.DateLastModified
                'WScript.Echo path & "  deleted: " & file.path
                'WScript.Echo oFolder2
            End If
    Set oFolder2 = Nothing
End Sub

'***Creation d'un dossier d'archive  
Function CreateArchiveFolder(strArchivePath)

    If Not objFSO.FolderExists(strArchivePath) Then
    End If

    set objFolder = objFSO.GetFolder(strArchivePath)

    If Not objFolder.Attributes AND 2048 Then
        RC = compress(strArchivePath, msgRC)
        WScript.Echo "Attributes Value: " & objFolder.Attributes
        'WScript.Echo RC
        'WScript.Echo msgRC
    End If
End Function

Function compress(strFol, outMsgError)

    Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set objFile = objWMI.Get("Cim_Directory='" & strFol & "'")

    'set objFolder = objFSO.GetFolder(strArchivePath)
    strerr = objFile.Compress

    If strerr <> 0 Then
        compress = strerr
        outMsgError = "There was an error compressing the file: " & strerr
        compress = strerr
        outMsgError = "File compression successful: " & strerr
    End If
End Function

'***Creation des sous-dossier d'archive**** 
Function CreateSubArchiveFolder(strMySubPath)
    If Not objFSO.FolderExists(strMySubPath) Then
        wscript.echo strMySubPath
        WScript.Echo "existe deja"
        WScript.Echo strMySubPath
    End If    
End Function

Open in new window

Next Iteration functionality in VB classic and VBA:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
* I'm inclined to think that Robocopy might be able to do some of these copy/move functions quicker or more efficiently.
* I'm not sure what is going on with the code regarding the sub folders and sub archive folders
* I would suggest you add some statements to the script to time the performance of different parts of your code.

Did you mean to close this question?

What did you find when you added some timing statements?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.