473,326 Members | 2,095 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Help with Dir Function

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
3 4661
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Alan Bashy | last post by:
Please, guys, In need help with this. It is due in the next week. Please, help me to implement the functions in this programm especially the first three constructor. I need them guys. Please, help...
2
by: Jackson Yap | last post by:
can someone kind enough to help me look at the attached html and js file? Why is it that the javascript menu could not work at www.apchosting.net but could work at...
5
by: Craig Keightley | last post by:
Please help, i have attached my page which worksin IE but i cannnot get the drop down menu to fucntion in firefox. Any one have any ideas why? Many Thanks Craig ...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
1
by: Michael D. Reed | last post by:
I am using the help class to display a simple help file. I generated the help file using Word and saving it as a single page Web page (.mht extension). I show the help file with the following...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
4
by: Stef Mientki | last post by:
I'm making special versions of existing functions, and now I want the help-text of the newly created function to exists of 1. an extra line from my new function 2. all the help text from the old...
22
by: Amali | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of...
1
by: Bob | last post by:
Hi, Hope you can help me with this one. I'm at my wits end. I'm trying to create an intelligent edit-box like the excellent "Customer" one at the URL: ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.