dibblm wrote:
Below is current code used. I can only list one directory then move to next.
I want to search one more directory further and can't seem to find how to
get one deeper. What I want to accomplish is to get to a specified
directory, Then list all the files of that directory. Move to the next and
do the same.
J:\
-user1
--Cookies
---List all files
move to next user
- user2
--Cookies
---List all files
move to next user
Code..........
Dim MyFile, MyPath, MyName, MySubdir, mysub
MyPath = "J:\" ' Set the path.
MySubdir = Dir("\" & "Cookies")
MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.
MyName = Dir(MyPath, vbDirectory)
Do While MyName <> "" ' Start the loop.
' Use bitwise comparison to make sure MyName is a directory.
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
' Display entry only if it's a directory.
Debug.WriteLine(MyName, MySubdir)
End If
MyName = Dir() ' Get next entry.
Here is the code I use to recursively copy one folder to another. It
should give you some ideas to get started...
PS: Using the old DIR command is not the recommended approach.
HTH,
Greg
' call the function like so...
RecursiveCopyFiles(Source, Dest, True)
' Recursively copy all files and subdirectories from the
' specified source to the specified destination.
Private Function RecursiveCopyFiles( _
ByVal sourceDir As String, _
ByVal destDir As String, _
ByVal bTop As Boolean) As Boolean
Dim aDirs() As String
Dim aFiles() As String
Dim ok As Boolean = True
Trace.WriteLine("Inspecting folder " & sourceDir)
Try
' Get a list of directories from the current parent.
aDirs = System.IO.Directory.GetDirectories(sourceDir)
For Each folderpath As String In aDirs
Dim sDir As String
' Get the path of the source directory.
sDir = System.IO.Path.GetFileName(folderpath)
' Create the new directory in the destination directory.
System.IO.Directory.CreateDirectory(System.IO.Path .Combine(destDir, sDir))
' Since we are in recursive mode, copy the children also
ok = RecursiveCopyFiles(folderpath,
System.IO.Path.Combine(destDir, sDir), False)
If ok Then
Try
Trace.WriteLine("Deleting " & destDir & sDir)
System.IO.Directory.Delete(destDir & sDir)
Catch ex As Exception
Trace.WriteLine("Error deleting " & destDir & sDir)
Trace.WriteLine(ex.Message)
ok = False
End Try
End If
Next
Catch ex As Exception
Trace.WriteLine("Error reading directory " & sourceDir)
End Try
' Get the files from the current parent.
aFiles = System.IO.Directory.GetFiles(sourceDir)
' Copy all files.
For Each filepath As String In aFiles
Dim sFile As String
' Get the full path of the source file.
sFile = System.IO.Path.GetFileName(filepath)
Try
' Copy the file.
Trace.WriteLine("Copying " & filepath)
System.IO.File.Copy(filepath,
System.IO.Path.Combine(destDir, sFile))
Try
' Delete the file.
Trace.WriteLine("Deleting " & filepath)
System.IO.File.Delete(filepath)
Catch ex As Exception
Trace.WriteLine("Error deleting " & filepath)
Trace.WriteLine(ex.Message)
ok = False
End Try
Catch ex As Exception
Trace.WriteLine("Error copying " & filepath)
Trace.WriteLine(ex.Message)
ok = False
End Try
Next
If Not bTop Then
Try
Trace.WriteLine("Deleting folder " & sourceDir)
System.IO.Directory.Delete(sourceDir)
Catch ex As Exception
Trace.WriteLine("Error deleting folder " & sourceDir)
Trace.WriteLine(ex.Message)
ok = False
End Try
End If
End Function