|
-
Oct 13th, 2005, 03:49 PM
#1
Thread Starter
Member
Searching File in Folders - Dir()
I am going through a directory and grabbing the folder names 4 deep and then grabbing the filename. I am having problems when it goes back up the go to the next folder I am getting an error message "Invalid procedure call or argument". I think it is because I have already called the Dir.
Thanks
Shannon
VB Code:
Private Function GetClient(iPath As String) As String
Dim MyDir
Dim MyClient As String
MyClient = Dir(iPath, vbDirectory)
Do While MyClient <> ""
If MyClient <> "." And MyClient <> ".." Then
If (GetAttr(iPath & MyClient) And vbDirectory) = vbDirectory Then
Debug.Print MyClient
Call GetCategory(iPath & MyClient & "\")
End If
End If
MyClient = Dir
Loop
End Function
Private Sub Form_Activate()
Call GetClient("\\192.168.75.120\staging\")
End Sub
Private Function GetCategory(MyClient As String) As String
Dim MyDir1
Dim MyCategory As String
MyCategory = Dir(MyClient, vbDirectory)
Do While MyCategory <> ""
If MyCategory <> "." And MyCategory <> ".." Then
If (GetAttr(MyClient & MyCategory) And vbDirectory) = vbDirectory Then
Debug.Print MyClient & MyCategory
Call GetSource(MyClient & MyCategory & "\")
End If
End If
MyCategory = Dir
Loop
End Function
Private Function GetSource(MyCategory As String) As String
Dim MyDir2
Dim MySource As String
MySource = Dir(MyCategory, vbDirectory)
Do While MySource <> ""
If MySource <> "." And MySource <> ".." Then
If (GetAttr(MyCategory & MySource) And vbDirectory) = vbDirectory Then
Debug.Print MyCategory & MySource
Call GetGUID(MyCategory & MySource & "\HOLDING\")
End If
End If
MySource = Dir <-- I am getting the error here after calling GetGUID
Loop
End Function
Private Function GetGUID(MySource As String) As String
Dim MyDir3
Dim MyGUID As String
MyGUID = Dir(MySource, vbDirectory)
Do While MyGUID <> ""
If MyGUID <> "." And MyGUID <> ".." Then
Debug.Print MyGUID
End If
MyGUID = Dir
Loop
End Function
-
Oct 13th, 2005, 03:55 PM
#2
Re: Searching File in Folders - Dir()
im fairly sure you can only have 1 "Dir" going at a time.. each time its called its "reset"
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Oct 13th, 2005, 04:39 PM
#3
Re: Searching File in Folders - Dir()
A hack/workaround is to create a function which will reset Dir to the original location.
VB Code:
Private Function ResetDir(Path As String, Folder As String) As Boolean
Dim strFolder As String
strFolder = Dir(Path, vbDirectory)
Do Until strFolder = Folder Or strFolder = ""
strFolder = Dir
Loop
End Function
Call ResetDir after each call to one of your Get procedures.
VB Code:
Private Function GetClient(iPath As String) As String
Dim MyDir
Dim MyClient As String
MyClient = Dir(iPath, vbDirectory)
Do While MyClient <> ""
If MyClient <> "." And MyClient <> ".." Then
If (GetAttr(iPath & MyClient) And vbDirectory) = vbDirectory Then
Debug.Print MyClient
Call GetCategory(iPath & MyClient & "\")
[B]ResetDir iPath, MyClient[/B]
End If
End If
MyClient = Dir
Loop
End Function
-
Oct 13th, 2005, 05:39 PM
#4
Hyperactive Member
Re: Searching File in Folders - Dir()
I suggest using the file system object.
Project->References include Microsoft Scripting Runtime.
VB Code:
Dim fsObj as New FileSystemObject
Dim myFolder as Folder
For Each myFolder In fsObj.GetFolder(YourFolder).SubFolders
Debug.Print myFolder.Name
Next
-
Oct 13th, 2005, 05:48 PM
#5
Hyperactive Member
Re: Searching File in Folders - Dir()
Here is the code to do what you need using filesystemobject and recursion
VB Code:
Private Sub Form_Activate()
PrintMyJunk 1, "c:\Program Files"
End Sub
Public Sub PrintMyJunk(Level As Integer, MyDir As String)
Dim fsObj As New FileSystemObject
Dim myFolder As Folder
For Each myFolder In fsObj.GetFolder(MyDir).SubFolders
Select Case Level
Case Is = 1
Debug.Print "Client = " & myFolder.Name
Case Is = 2
Debug.Print "Category = " & myFolder.Name
Case Is = 3
Debug.Print "Source = " & myFolder.Name
Case Is = 4
Debug.Print "GUID = " & myFolder.Name
End Select
If Level < 4 Then
PrintMyJunk Level + 1, myFolder.Path
End If
Next
End Sub
Last edited by umilmi81; Oct 13th, 2005 at 05:57 PM.
-
Oct 14th, 2005, 09:54 AM
#6
Thread Starter
Member
Re: Searching File in Folders - Dir()
I have converted it over to the filesystemobject. My question is how do I get all of the filename in the folder?
My code is below. Thanks everyone for your help.
Shannon
VB Code:
Public Sub GetFileName(Level As Integer, MyDir As String)
Dim fsObj As New FileSystemObject
Dim myClient As Folder
Dim myCategory As Folder
Dim mySource As Folder
Dim myStatus As Folder
Dim myFile As File
For Each myClient In fsObj.GetFolder(MyDir).SubFolders
For Each myCategory In fsObj.GetFolder(myClient).SubFolders
For Each mySource In fsObj.GetFolder(myCategory).SubFolders
For Each myStatus In fsObj.GetFolder(mySource).SubFolders
If myStatus.Name = "Holding" Then
'How do I get the filename
End If
Next
Next
Next
Next
End Sub
-
Oct 14th, 2005, 10:19 AM
#7
Re: Searching File in Folders - Dir()
Well, i worte this some time ago (or did i ask for help on it :S can't remember) and it lists the filenames of a given folder quite easily.
You can specify an extension if you want, OR, you could leave it blank retrieving the entire filename.
VB Code:
Private Sub ListFiles(strPath As String, Optional Extention As String)
Dim File As String
If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
If Trim$(Extention) = "" Then
Extention = "*.*"
ElseIf Left$(Extention, 2) <> "*." Then
Extention = "*." & Extention
End If
File = Dir$(strPath & Extention)
Do While Len(File)
Debug.Print File
File = Dir$
Loop
End Sub
-
Nov 2nd, 2005, 10:51 PM
#8
Hyperactive Member
Re: Searching File in Folders - Dir()
 Originally Posted by odamsr
I have converted it over to the filesystemobject. My question is how do I get all of the filename in the folder?
My code is below. Thanks everyone for your help.
Shannon
VB Code:
Public Sub GetFileName(Level As Integer, MyDir As String)
Dim fsObj As New FileSystemObject
Dim myClient As Folder
Dim myCategory As Folder
Dim mySource As Folder
Dim myStatus As Folder
Dim myFile As File
For Each myClient In fsObj.GetFolder(MyDir).SubFolders
For Each myCategory In fsObj.GetFolder(myClient).SubFolders
For Each mySource In fsObj.GetFolder(myCategory).SubFolders
For Each myStatus In fsObj.GetFolder(mySource).SubFolders
If myStatus.Name = "Holding" Then
For Each myFile in myStatus.Files
Debug.Print myFile.Name
Next
End If
Next
Next
Next
Next
End Sub
I modified your code to debug.print file names. Simple isnt' it?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|