Results 1 to 4 of 4

Thread: Looping in folders

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    Granby, Qc, Canada
    Posts
    602

    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 ?

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    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:
    1. Public Function RecurseFolderList(FolderName As String)  As Boolean
    2.    
    3.     On Error Resume Next
    4.     Dim fso, f, fc, fj, f1
    5.    
    6.     Set fso = CreateObject("Scripting.FileSystemObject")
    7.    
    8.     If Err.Number > 0 Then
    9.         RecurseFolderList = False
    10.         Exit Function
    11.     End If
    12.    
    13.     On Error GoTo 0
    14.     If fso.FolderExists(FolderName) Then
    15.        
    16.         Set f = fso.GetFolder(FolderName)
    17.         Set fc = f.Subfolders
    18.         Set fj = f.Files
    19.         'For each subfolder in the Folder
    20.         For Each f1 In fc
    21.             'Do something with the Folder Name
    22.             Debug.Print f1
    23.             'Then recurse this function with the sub-folder to get any'
    24.             ' sub-folders
    25.             RecurseFolderList (f1)
    26.         Next
    27.        
    28.         'For each folder check for any files
    29.         For Each f1 In fj
    30.             Debug.Print f1
    31.         Next
    32.        
    33.         Set f = Nothing
    34.         Set fc = Nothing
    35.         Set fj = Nothing
    36.         Set f1 = Nothing
    37.        
    38.     Else
    39.         RecurseFolderList = False
    40.     End If
    41.    
    42.     Set fso = Nothing
    43.    
    44. 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"

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    Granby, Qc, Canada
    Posts
    602

    Re: Looping in folders

    Thanks, but how can I compare file's last modified date ?

  4. #4
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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
  •  



Click Here to Expand Forum to Full Width