Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help With VBScript to Copy files and folders

Status
Not open for further replies.

Shaynec

Technical User
Feb 28, 2012
2
GB
I am trying to write a script that will look at a source directory, make a list of all the files and folders below it and then compaire this with another directory and copy any missing files and create any missing folders.

So far all I can do is look at the source directory and see a folder below it, I cannot find a way to go further into sub folders. This is what I have so far:
Set objFSO = createobject("Scripting.FileSystemObject")
Set objLogFile = objFSO.OpenTextFile (strLogFile, ForAppending)
objLogFile.WriteLine "Customer configs Backup ran at " &Now

CompareDirs strSourceFolder, strDestinationFolder

Set objFSO = nothing

sub CompareDirs(byval strSourceDir,byval strDestinationDir)
Dim objFolder
Dim objSubFolder
Dim objFiles
Dim objSubFolders
Dim objFile

Set objFolder = objFSO.GetFolder(strSourceDir)
Set objFiles = objFolder.Files

For Each objFile in objFiles
objLogFile.WriteLine("File," &objFile.Name & "," & objFile.Path)
Next

Set objSubFolders = objFolder.SubFolders

For Each objFolder in objFolder.SubFolders
objLogFile.WriteLine("Folder," &objFolder.Name & "," &objFolder.Path)
Next


end sub

Can anyone help with a solution ?
 
Use recursion:
For Each objFolder in objFolder.SubFolders
objLogFile.WriteLine("Folder," &objFolder.Name & "," &objFolder.Path)
CompareDirs objFolder.Path, strDestinationDir & "\" & objFolder.Name
Next

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I cannot find a way to go further into sub folders
Recursion (call a routine from within itself")
Code:
set objFSO = CreateObject("Scripting.FileSystemObject")
sub [red]traverseDir(strDir)[/red]
   for each objSubFolder in objFSO.GetFolder(strDir).SubFolders
      msgbox objSubFolder.Path
      [red]traverseDir(objSubFolder.Path)[/red]
   next
end sub

I think the solution you are looking for is objFSO.CopyFolder (
- Geates
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top