Auto Archiving files using VBScript

I have a script that I am trying to enable doing multiple folders using an array but I can't get it to work. the script itself reads the files determines how many days since file was created and if more then then X number of days then [the script will append the creation date to the file name and moves it to an achieve folder.  The script works great but if I have 2 folders and want the files to be in two different locations, then I can't get it to work.

source1 = D:\temp\folder1 Destination1=\\backupserver\autoarchieve\folder1
source2 = D:\temp\folder2 Destionation2=\\backupserver\autoarchieve\folder2
source3= D:\temp\folder3 Destionation3=\\backupserver\autoarchieve\folder3

Here is the actual script:

Option Explicit 
Dim objFSO, objFile, objFolder 
Dim Created, File, NewName
Dim strfiletype, objfilelocation, pkFiles, cbmFiles
Set objFSO = CreateObject("Scripting.FileSystemObject") 

Const SourceDir = "D:\Testing\SpotsTest\" 
Const TargetDir = "\\backupserver\AutoArchive$\" 
Const AgeLimit = 20


'-------------- 
'Move old files 
'-------------- 
Set objFolder = objFSO.GetFolder(SourceDir) 
For Each File In objFolder.Files 
Set objFile = objFSO.GetFile(SourceDir & File.Name) 
Created = objFile.DateCreated 
If DateDiff ("D", Created, Now) > AgeLimit Then 
NewName = Year(Created) & "-" & Pad(Month(Created)) & "-" & Pad(Day(Created)) _ 
& "." & Replace(File.Name, " ", "_") 
If objFSO.FileExists(TargetDir & NewName) Then 
MsgBox "Cannot move """ & File.Name & """", 0, "File exists in """ & TargetDir & """" 
Else 
WScript.Echo ("Moving " & File.Name) 
objFile.Move TargetDir & NewName 
End If 
End If 
Next 
'--------------------------------------- 
'Pad days & months with "0" if necessary 
'--------------------------------------- 
Function Pad (n) 
If Len(n) < 2 Then 
Pad = "0" & n 
Else 
Pad = n 
End If 
End Function 

Open in new window

LVL 1
Barron1299Asked:
Who is Participating?
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.

Bill PrewCommented:
Make your existing logic a subroutine, and then call it several times as needed, for example:

Option Explicit 

Dim objFSO, objFile, objFolder 
Dim Created, File, NewName
Dim strfiletype, objfilelocation, pkFiles, cbmFiles

Set objFSO = CreateObject("Scripting.FileSystemObject") 

ProcessFolder "D:\temp\folder1", "\\backupserver\autoarchieve\folder1", 20
ProcessFolder "D:\temp\folder2", "\\backupserver\autoarchieve\folder2", 20
ProcessFolder "D:\temp\folder3", "\\backupserver\autoarchieve\folder3", 20

Sub ProcessFolder(SourceDir, TargetDir, AgeLimit)
    '-------------- 
    'Move old files 
    '-------------- 
    Set objFolder = objFSO.GetFolder(SourceDir) 
    For Each File In objFolder.Files 
        Set objFile = objFSO.GetFile(SourceDir & File.Name) 
        Created = objFile.DateCreated 
        If DateDiff ("D", Created, Now) > AgeLimit Then 
            NewName = Year(Created) & "-" & Pad(Month(Created)) & "-" & Pad(Day(Created)) _ 
                & "." & Replace(File.Name, " ", "_") 
            If objFSO.FileExists(TargetDir & NewName) Then 
                MsgBox "Cannot move """ & File.Name & """", 0, "File exists in """ & TargetDir & """" 
            Else 
                WScript.Echo ("Moving " & File.Name) 
                objFile.Move TargetDir & NewName 
            End If 
        End If 
    Next 
End Sub

'--------------------------------------- 
'Pad days & months with "0" if necessary 
'--------------------------------------- 
Function Pad (n) 
    If Len(n) < 2 Then 
        Pad = "0" & n 
    Else 
        Pad = n 
    End If 
End Function

Open in new window


»bp
0

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
Barron1299Author Commented:
wow that was easy :), thank you very much bill
0
Barron1299Author Commented:
Thanks as always!
0
Bill PrewCommented:
Welcome, glad that was useful.


»bp
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.