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

Loop through folder and check for folder type

Status
Not open for further replies.

din2005

Programmer
Mar 22, 2005
162
GB
Hi all

is there way to code so to loop through each folder and check if
the folder has a format like...

strFolder = "dd2322" Like "[a-z][a-z]####"

and if not that format possible to delete that folder!

thanks
 
Hi there with the help on a previous thread i've worked it out!

Dim mypath, mydir, myfile, counter, i

counter = 1

' Display the names in C:\ that represent directories.
mypath = "C:\Database\" ' Set the path.

mydir = Dir(mypath, vbDirectory) ' Retrieve the first entry.

Do While mydir <> "" ' Start the loop.
counter = counter + 1 '' counts # of directories passed
strFolder = mydir Like "[a-z][a-z]####"
' Ignore the current directory and the encompassing directory.
If mydir <> "." And mydir <> ".." Then
' Use bitwise comparison to make sure MyName is a directory.
If (GetAttr(mypath & mydir) And vbDirectory) = vbDirectory Then

If strFolder = True Then

Debug.Print mydir ' Display entry only if it
Else
End If


myfile = Dir(mypath & mydir & "\", vbNormal)
Do While myfile <> ""
' Debug.Print "---- " & myfile
myfile = Dir
Loop
End If ' it represents a directory.
End If
mydir = Dir(mypath, vbDirectory) ' Retrieve the first entry.
For i = 1 To counter
mydir = Dir ' Get next entry.
Next
Loop
 
hi phv,

thankU 4 replying...
I thought i nailed this but guess i was wrong :(

The code below prints out the folders which match a certain criteria how do i change it so that it deletes the ones that don't match that criteria

Private Sub Command1_Click()
Set fsoDelete = CreateObject("Scripting.FileSystemObject")



Dim mypath, mydir, myfile, counter, i

counter = 1

' Display the names in C:\ that represent directories.
mypath = "C:\image\" ' Set the path.

mydir = Dir(mypath, vbDirectory) ' Retrieve the first entry.
strFolder = mydir Like "[a-z][a-z]####"
Do While mydir <> "" ' Start the loop.
strFolder = mydir Like "[a-z][a-z]####"
counter = counter + 1 '' counts # of directories passed

' Ignore the current directory and the encompassing directory.
If mydir <> "." And mydir <> ".." Then
' Use bitwise comparison to make sure MyName is a directory.
If (GetAttr(mypath & mydir) And vbDirectory) = vbDirectory Then


If strFolder = True Then

Debug.Print mydir ' Display entry only if it
Else

' Delete All folders that don't match aa####
fsoDelete.DeleteFolder mypath & mydir, True
Debug.Print mydir
End If



myfile = Dir(mypath & mydir & "\", vbNormal)
Do While myfile <> ""
' Debug.Print "---- " & myfile
myfile = Dir
Loop
End If ' it represents a directory.
End If
mydir = Dir(mypath, vbDirectory) ' Retrieve the first entry.
For i = 1 To counter
mydir = Dir ' Get next entry.
Next
Loop
 
Typed, not tested

Code:
Sub SDFJKG()
Dim fso As Object
Dim myFolder As Object
Dim AllFolders As Object
Dim curFolder As Object

Set fso = CreateObject("Scripting.FileSystemObject")
Set myFolder = fso.GetFolder("C:\image")
Set AllFolders = myFolder.Folders
For Each curFolder In AllFolders
    If curFolder.Name Like "[a-z][a-z]####" Then
        Debug.Print curFolder.Name
    Else
        fso.DeleteFolder curFolder.Name
    End If
Next
Set AllFolders = Nothing
Set myFolder = Nothing
Set fso = Nothing
End Sub
 
din2005

You should have kept that posting thread705-1204057 here, in case someone else was interesting also..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top