Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching the Hard Drive

Status
Not open for further replies.

cannonenter

Programmer
Mar 27, 2003
20
GB
Hope someone can help me.

I need to search the Hard Drive and Open all the folders within the C: drive. Then search for files listed within a database.

The database part i can do fine but do not know when to start on the hard drive side of things.

Hope you can help me.

Many Thanks

Stephen Rattray
 
Hi,

If you do a search on this forum for FileSystemObject that should get you pointed in the right direction.

Hope this helps

Harleyquinn
 
If you do a search on this forum for DIR that should get you an alternative (and equally viable) view

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
Hi there

The following code finds All the Folders fine within the Hard Drive. But how do i List all the file names held within the Folders and Sub Folders.

I have searched the site but all i can find is this code to list all the Folders.

Hope you can Help me

Many thanks

Stephen Rattray
 
The following code finds All the Folders fine within the Hard Drive

what following code???

But how do i List all the file names held within the Folders and Sub Folders.

Code:
File = Dir(*.*)
While File <> ""
  List.AddItem File
  File = Dir
Wend

and there is a variation to list folders instead...

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
sorry i didn't post the code in my last posting here it is.

Private Sub Command1_Click()
Dim strFolderName As Folder
Dim oFS As FileSystemObject
Dim Path As String, S As String
Path = "C:\"
S = Dir$(Path & "*", vbDirectory)

While Len(S)
If S <> "." And S <> ".." Then
If GetAttr(Path & S) And vbDirectory Then
List1.AddItem S
End If
End If


S = Dir$
Wend

End Sub


My problem is that i can get all the Folders but No file names within the foilders. Also how do i open the sub folders aswell?

Hope you can help me.
 
In pseudocode:

For Each Folder In Drive
ListSubFolder
ListFiles
Next Folder

You can use the Folders, SubFolders and Files collections exposed by the FileSystemObject. If you create a subroutine that calls itself it is very easy.

Andy
--
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
 
I actually just made a program to do this several weeks ago...

You need to use Recursive Functions (Functions that conditionally call them selfs)

Example In pseudocode:
Code:
Sub ScanFolders(Path)
  For Each Folder In Drive
    Get SubFolders for Path
    If SubFolders exist
      For Each Folder In SubFolders
        List Folder
        [b]Call ScanFolders Folder[/b]
      Next Folder
    End If
    Get Files
    If Files Exist
      For Each File in Files
        List File
      Next File
    End If
  Next Folder
End Sub

Here is the actual code I used...

Start a new project with 1 Form and add the following controls:
CommonDialog :: cd1
TextBox :: Text1 :: Output Text Box, Set Multiline, w/Scrollbars... BOTH
TextBox :: Text2 :: Extention Filter, comma seperated, extention ONLY (mp3,wma,etc...)
DirListBox :: Dir1 :: Folder to search from
CommandButton :: Command1 :: Caption (List Folders)
CommandButton :: Command2 :: Caption (Save Text)

Then Add this code to the Form's code area...
Code:
Private Sub Form_Load()
  Text1.Text = ""
  Text2.Text = ""
  Command1.Caption = "List Folders"
  Command1.Default = True
  Command2.Caption = "Save Text"
  cd1.Filter = "Text|*.txt|All|*.*"
  cd1.DefaultExt = "txt"
  cd1.CancelError = True
End Sub

Private Sub Command1_Click()
  Text1 = ""
  recurse_list Replace(Dir1 & "\", "\\", "\"), Text2
End Sub

Private Sub Command2_Click()
  Err.Clear
  On Error Resume Next
  cd1.ShowSave
  If Err.Number = 0 Then
    If Dir(cd1.FileName) <> "" Then
      If MsgBox("File Exists" & vbCrLf & "Overwrite", vbYesNo, "Overwrite") = vbYes Then
        Open cd1.FileName For Output As #1
          Print #1, Text1
        Close
      End If
    Else
      Open cd1.FileName For Output As #1
        Print #1, Text1
      Close
    End If
  End If
End Sub

Function recurse_list(Path As String, Optional ext As String, Optional tabs As String)
  On Error Resume Next
  Dim Folders() As String
  Dim tmpExt() As String
  Dim t1 As String, t2 As String
  Folders = getFolders(Path)
  For i = 0 To UBound(Folders)
    Text1 = Text1 & tabs & ">" & Folders(i) & vbCrLf
    recurse_list Path & Folders(i) & "\", ext, tabs & vbTab
  Next
  File = Dir(Path & "*")
  While File <> ""
    If ext <> "" Then
      tmpExt = Split(Replace(ext, ",", "|"), "|")
      For i = 0 To UBound(tmpExt)
        t1 = UCase(Right(File, Len(tmpExt(i))))
        t2 = UCase(tmpExt(i))
        If t1 = t2 Then Text1 = Text1 & tabs & File & vbCrLf
      Next
    Else
      Text1 = Text1 & tabs & File & vbCrLf
    End If
    File = Dir()
  Wend
End Function

Function getFolders(Path As String) As String()
    On Error Resume Next
    Dim S As String
    Dim Temp As String, Temp2() As String
    S = Dir$(Path & "*", vbDirectory)
    While Len(S)
        If S <> "." And S <> ".." Then
            If GetAttr(Path & S) And vbDirectory Then
                Temp = Temp & IIf(Temp <> "", "|", "") & S
            End If
        End If
        S = Dir$
    Wend
    getFolders = Split(Temp, "|")
End Function

That should get you started... feel free to modify it as you like...

Hope This Helps ;-)

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Hi,

this is my stab at a recursive procedure that gets each and every file in the folder you specify, including those in any subfolder within the specified folder. You can turn the subfolder search off by providing a boolean value False to the last (optional) argument. Don't forget to reference Microsoft Scripting runtime.

Code:
Option Explicit

Dim fso As New FileSystemObject


Private Sub GetFiles(strFolder As String, ByRef colFiles As Collection, Optional Subfolders As Boolean = True)
  
  On Error Resume Next
                
  Dim m_Folder  As Folder
  Dim m_File    As File
  
  For Each m_File In fso.GetFolder(strFolder).Files
    colFiles.Add m_File.Path
  Next
  
  If Subfolders Then
    For Each m_Folder In fso.GetFolder(strFolder).Subfolders
      GetFiles m_Folder.Path, colFiles, True
    Next
  End If
  
End Sub

When you pass it a folder that doesn't exist, it will simply return nothing in the collection. You need this to call the sub and print a list of all files that have been put in the collection to your immediate window:

Code:
  Dim colAllFiles As New Collection
  Dim i           As Integer
  
  GetFiles "c:\", colAllFiles
  
  For i = 1 To colAllFiles.Count
    Debug.Print colAllFiles(i)
  Next i

It shouldn't be too hard to implement a Mask property that checks each file before it's added to the collection. I don't have time right now to do this.

Be careful with the code above because as it is, it will list every file on your C-drive and may take a while to finish.

Good luck!

__________________
code is enduring
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top