|
-
Sep 30th, 2005, 12:26 PM
#1
Thread Starter
Fanatic Member
Looping in folders
I had to copy a 30 Gb folder which contains folders and subfolders and a lot of files but the copy failed at about 22 Gb copied.
I would like to create a application that compare each files from the source folder with the destination folder and copy the files that changed and the one that don't exist.
The problem is that I don't know how to do the loop to get all files.
I know how to loop, but in the main loop, I'll have to add another loop, and another one in this one ...
I don't know how to proceed.
Can I have an example on how to list all files ?
-
Sep 30th, 2005, 12:47 PM
#2
Re: Looping in folders
You need to create a 'recursive' function
create a function that scans the folder pass into the function... if it finds another folder... call the SAME function again passing the new folder in.. it will loop untill it finishes the return to where it was...
example: (Not real code! LOL)
Public Function SearchFolders(Fldr as Folder)
dim FLE as file
for each FLE in FLDR
if FLE Type is Folder Then Call SearchFolder(FLE)
Debug.print FLE.Name
Next
End function
get it?
Here is a working example: (You need a ref to the MS Scipting runtime)
VB Code:
Public Function RecurseFolderList(FolderName As String) As Boolean
On Error Resume Next
Dim fso, f, fc, fj, f1
Set fso = CreateObject("Scripting.FileSystemObject")
If Err.Number > 0 Then
RecurseFolderList = False
Exit Function
End If
On Error GoTo 0
If fso.FolderExists(FolderName) Then
Set f = fso.GetFolder(FolderName)
Set fc = f.Subfolders
Set fj = f.Files
'For each subfolder in the Folder
For Each f1 In fc
'Do something with the Folder Name
Debug.Print f1
'Then recurse this function with the sub-folder to get any'
' sub-folders
RecurseFolderList (f1)
Next
'For each folder check for any files
For Each f1 In fj
Debug.Print f1
Next
Set f = Nothing
Set fc = Nothing
Set fj = Nothing
Set f1 = Nothing
Else
RecurseFolderList = False
End If
Set fso = Nothing
End Function
Call RecurseFolderList("C:\")
and watch your debug(immediate) window have fun!
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Sep 30th, 2005, 01:10 PM
#3
Thread Starter
Fanatic Member
Re: Looping in folders
Thanks, but how can I compare file's last modified date ?
-
Sep 30th, 2005, 01:28 PM
#4
Re: Looping in folders
Since he mentioned FSO, try to look up FSO if your interested in the FSO implementation.
DateLastModified Property
Description
Returns the date and time that the specified file or folder was last modified. Read-only.
Syntax
object.DateLastModified
The object is always a File or Folder object.
Remarks
The following code illustrates the use of the DateLastModified property with a file:
Sub ShowFileAccessInfo(filespec)
Dim fs, f, s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(filespec)
s = UCase(filespec) & vbCrLf
s = s & "Created: " & f.DateCreated & vbCrLf
s = s & "Last Accessed: " & f.DateLastAccessed & vbCrLf
s = s & "Last Modified: " & f.DateLastModified
MsgBox s, 0, "File Access Info"
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|