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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

filesearch doesn't return folder names 1

Status
Not open for further replies.

p27br

Programmer
Aug 13, 2001
516
GB
hello

I need to search for all folders with a given name in a folder structure and delete them.
I tried to use the application.filesearch but it doesn't seem to return folders, only files.
example : folder to delete is 'TEST'

tree is :

f1
subf1
TEST
subf2
subsubf2
TEST

there would be two deletions needed
I know there is a filesytem object but I could'nt figure it out and I think I would need a recursive function to search all subfolders.

does anyone know a solution ?

thanks

Paul
 
I couldn't get the fso to cough up folders either. Here is a work-around using DOS:

Run this in a DOS window (or in a .bat file):
Code:
  C:\>dir TEST /ad /s > c:\dir.txt
then process the resulting text file to see where the TEST folders are located. For example, here is the dir.txt that is produced for your sample setup:
Code:
 Volume in drive C has no label
 Volume Serial Number is xxxx-xxxx

Directory of C:\f1\subf1

TEST           <DIR>        04-19-03  3:03p TEST
         0 file(s)              0 bytes

Directory of C:\f1\subf2\subsubf2

TEST           <DIR>        04-19-03  3:04p TEST
         0 file(s)              0 bytes

Total files listed:
         0 file(s)              0 bytes
         2 dir(s)        4,821.56 MB free
It's a bit of a kludge, but at least it works.
 
thanks, I'll try and use this

regards

paul
 
Paul & Zathras,

Try the FileDialog object, which I use in Access & Word VBA to choose a file or folder for various tasks. To choose a folder, you need to set the FileDialog to 'pick a folder'

Code:
Public Function fFolderPicker() As String
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
    Dim vrtSelectedItem As Variant, strFolder As String
        With fd
            .AllowMultiSelect = False
            .Title = &quot;Choose folder&quot;
                If .Show = -1 Then
                    For Each vrtSelectedItem In .SelectedItems
                    strFolder = vrtSelectedItem
                    Next vrtSelectedItem
                Else
                    Exit Function
                End If
        End With
    Set fd = Nothing
    fFolderPicker = strFolder
End Function

This will return the folder name chosen as the variable fFolderPicker as a complete path. Then you can do with it what you want with NAME..AS.. or Kill etc.

There are heaps more properties to this object you can set as well.....

Brad

 
Might have misunderstood, sounds like you want to search for folders without user input. I'm sure I've seen a solution here somewhere.
 
Don't really have time to play with this much but here is some code I used a while ago to list all folders and sub folders under a specified path:

Sub LoopFolders()
'Loops thru 4 levels of sub folders
'Application.ScreenUpdating = False

Dim fso As New FileSystemObject
Dim f As Folder, sf As Folder, path As String
'Initialize path.
path = &quot;w:\&quot;
'Get a reference to the Folder object.
Set f = fso.GetFolder(path)
'Iterate through subfolders.
r = 2
x = 0
On Error Resume Next
For Each sf In f.SubFolders
Range(&quot;A&quot; & r).Value = sf.path
r = r + 1
x = x + 1
Application.StatusBar = x & &quot; Folders Done&quot;
For Each ssf In sf.SubFolders
Range(&quot;A&quot; & r).Value = ssf.path
r = r + 1
For Each sssf In ssf.SubFolders
Range(&quot;A&quot; & r).Value = sssf.path
r = r + 1
For Each ssssf In sssf.SubFolders
Range(&quot;A&quot; & r).Value = ssssf.path
r = r + 1

Next
Next
Next
Next
MsgBox &quot;Done&quot;
End Sub

You should be able to bodge it to do what you want

Rgds
Geoff
&quot;Some cause happiness wherever they go; others whenever they go.&quot;
-Oscar Wilde
 
Here's a FileSystemObject example that should work with any depth (yep, it's recursive...). It's written for VB6 so you'll probably need to make some minor changes in the bit where it adds files to a ListBox:
[tt]
Public Sub RecurseFolder(strRoot As String, Optional strMatch As String = &quot;&quot;)
Dim mySubFolder As Folder
Dim myFolder As Folder

If fso.FolderExists(strRoot) Then
Set myFolder = fso.GetFolder(strRoot)
' Folder found
' ===========================================
If (strMatch <> &quot;&quot; And UCase(myFolder.Name) = UCase(strMatch)) Or strMatch = &quot;&quot; Then
Form1.List1.AddItem myFolder.Path
End If
' ===========================================
DoEvents ' Defer to the OS
For Each mySubFolder In myFolder.SubFolders
RecurseFolder mySubFolder.Path, strMatch
Next
End If

End Sub
 
thank you all for your suggestions !

I'll let you know if I have been succesfull
 
strongm

thanks your post was very helpfull
i'll give it a star
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top