Help with Dir Function

D

dibblm

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
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.
 
G

Greg Burns

dibblm said:
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


- user2
--Cookies
---List all files


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
 
D

dibblm

This doesn't really help me greg. You are going from point A to point B. and
I can do that by typing the whole path string in that I need.

If things would stay concise on teh net about doing things , this wouldnt
be so hard. Theres 100 ways to see around a box, which is the best way if
you know what I mean.

Ill play with your code for bit and see what I can work out of it. Can you
tell me this though. What do I have to do, to get the results to a text file
?
 
G

Greg Burns

Mike, what I was trying to demonstrate in my posting was the necessity of
using recursion. It is essential to what you are attempting to do.

Here is some code I threw together that drills down and only spits out files
that are in folders named "cookies". Not well tested. Recursion can make
your head spin, and I'm not sure I did this right. :^)

Don't have time today to show how to write the output to a file. Perhaps
somebody else can point you in the right direction.

BTW:

Not sure your end goal here. VB.NET maybe overkill for this. This might be
better suited written as a .vbs file. You can easily dump the output to an
Excel file. Later I'll try and post a quick example of that. (Of course,
if you going to use .vbs you be back to the old filesystem object model)

Greg

Option Strict On ' always use this! PLEASE!!
Imports System.IO

Module Module1

Const start As String = "J:\"

Sub Main()

RecurseFolder(start)

End Sub

Private Function RecurseFolder( _
ByVal startDir As String) As Boolean

Dim aDirs() As String
Dim aFiles() As String

'Debug.WriteLine("Looking in folder " & startDir)

Try
' Get a list of directories from the current parent.
aDirs = System.IO.Directory.GetDirectories(startDir)

For Each folderpath As String In aDirs
Dim sDir As String

' Get the path of the source directory.
sDir = System.IO.Path.GetFileName(folderpath)

' Since we are in recursive mode, copy the children also
RecurseFolder(folderpath)

Next
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try

If InStr(startDir.ToLower, "cookies") > 0 Then

Try
' Get the files from the current parent.
aFiles = System.IO.Directory.GetFiles(startDir)

' 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)

Debug.WriteLine("Found file " & filepath)

Next
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
End If

End Function
End Module
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top