By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,034 Members | 1,012 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,034 IT Pros & Developers. It's quick & easy.

Help with Dir Function

P: n/a
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.
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Nov 21 '05 #2

P: n/a
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
?
"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:ei**************@TK2MSFTNGP10.phx.gbl...
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

Nov 21 '05 #3

P: n/a
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
"dibblm" <di****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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 ?
"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:ei**************@TK2MSFTNGP10.phx.gbl...
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


Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.