Vbscript - Checking each subfolder for files
- by Kenny Bones
Ok, this is a script that's supposed to basically mirror two sets of folders.
I managed to get it to work, but it seems like it didn't check each subfolder.
So, I added these lines to the code:
Set colFolders = objFolder.Subfolders
Then I added this:
For each subFolder in colFolders
For each objFile in colFiles
Dim DateModified
DateModified = objFile.DateLastModified
ReplaceIfNewer objFile, DateModified, strSourceFolder, strDestFolder
Next
Next
This however does not seem to be working.
This is the full code below.
Dim strSourceFolder, strDestFolder
Dim fso, objFolder, colFiles, colfolders
strSourceFolder = "c:\users\vegsan\desktop\Source\"
strDestFolder = "c:\users\vegsan\desktop\Dest\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFolder = fso.GetFolder(strSourceFolder)
Set colFiles = objFolder.Files
Set colFolders = objFolder.Subfolders
For each subFolder in colFolders
For each objFile in colFiles
Dim DateModified
DateModified = objFile.DateLastModified
ReplaceIfNewer objFile, DateModified, strSourceFolder, strDestFolder
Next
Next
Sub ReplaceIfNewer (sourceFile, DateModified, SourceFolder, DestFolder)
Const OVERWRITE_EXISTING = True
Dim fso, objFolder, colFiles, sourceFileName, destFileName
Dim DestDateModified, objDestFile
Set fso = CreateObject("Scripting.FileSystemObject")
sourceFileName = fso.GetFileName(sourceFile)
destFileName = DestFolder & sourceFileName
if fso.FileExists(destFileName) Then
Set objDestFile = fso.GetFile(destFileName)
DestDateModified = objDestFile.DateLastModified
if DateModified <> DestDateModified Then
fso.CopyFile sourceFile, destFileName
End if
End if
if Not fso.FileExists(destFileName) Then
fso.CopyFile sourceFile, destFileName
End if
End Sub