Re: Search and Delete Files Based on Mask

From: Old Enough to Know Better (SpamSucks_at_NoSpam.com)
Date: 02/22/04


Date: Sun, 22 Feb 2004 17:59:14 GMT


"J French" <erewhon@nowhere.com> wrote in message
news:4037a5cd.31401336@news.btclick.com...
> On Sat, 21 Feb 2004 17:27:05 GMT, "Old Enough to Know Better"
> <SpamSucks@NoSpam.com> wrote:
>
> <snip>
>
> >You may also want to check out the FileSystemObject ( for VB add a
project
> >reference to the Microsoft scripting runtime)
>
> Are you joking ?
>

I make no guarentees on this code because I didn't spend a lot of time
verifing it but here
 is a MS knowledge base example of how to recursively search using wildcards
and FSO in VB6.
Maybe OP can spend a few minutes and paste it into a VB form and see if it's
what they want.
If you're not familiar with VB you can reference the KB article for complete
instructions.

HOW TO: Recursively Search Directories by Using FileSystemObject
http://support.microsoft.com/default.aspx?scid=kb;EN-US;185601

Option Explicit

Dim fso As New FileSystemObject
Dim fld As Folder

Private Sub Command1_Click()
   Dim nDirs As Long, nFiles As Long, lSize As Currency
   Dim sDir As String, sSrchString As String
   sDir = InputBox("Type the directory that you want to search for", _
                   "FileSystemObjects example", "C:\")
   sSrchString = InputBox("Type the file name that you want to search for",
_
                   "FileSystemObjects example", "vb.ini")
   MousePointer = vbHourglass
   Label1.Caption = "Searching " & vbCrLf & UCase(sDir) & "..."
   lSize = FindFile(sDir, sSrchString, nDirs, nFiles)
   MousePointer = vbDefault
   MsgBox Str(nFiles) & " files found in" & Str(nDirs) & _
          " directories", vbInformation
   MsgBox "Total Size = " & lSize & " bytes"
End Sub

Private Function FindFile(ByVal sFol As String, sFile As String, _
   nDirs As Long, nFiles As Long) As Currency
   Dim tFld As Folder, tFil As File, FileName As String

   On Error GoTo Catch
   Set fld = fso.GetFolder(sFol)
   FileName = Dir(fso.BuildPath(fld.Path, sFile), vbNormal Or _
                  vbHidden Or vbSystem Or vbReadOnly)
   While Len(FileName) <> 0
      FindFile = FindFile + FileLen(fso.BuildPath(fld.Path, _
      FileName))
      nFiles = nFiles + 1
      List1.AddItem fso.BuildPath(fld.Path, FileName) ' Load ListBox
      FileName = Dir() ' Get next file
      DoEvents
   Wend
   Label1 = "Searching " & vbCrLf & fld.Path & "..."
   nDirs = nDirs + 1
   If fld.SubFolders.Count > 0 Then
      For Each tFld In fld.SubFolders
         DoEvents
         FindFile = FindFile + FindFile(tFld.Path, sFile, nDirs, nFiles)
      Next
   End If
   Exit Function
Catch: FileName = ""
       Resume Next
End Function

Of course the sample code above doesn't actually delete the files it just
gives a listing, which you could then use for review before deleting. To
delete files use the FileSystemObject.DeleteFile method.

fso.DeleteFile ( filespec[, force] );
Arguments
filespec - Required. The name of the file to delete. The filespec can
contain wildcard characters in the last path component.
force - Optional. Boolean value that is true if files with the read-only
attribute set are to be deleted; false (default) if they are not.

Remarks
An error occurs if no matching files are found. The DeleteFile method stops
on the first error it encounters. No attempt is made to roll back or undo
any changes that were made before an error occurred.

Hope this helps



Relevant Pages

  • Re: My VB6 MS Word VBA code crashes Word - Word97
    ... The reason you can't go backwards (that is, compile with a reference to Word ... Dim oWord As Word.Application ' Note early bound is OK here! ... Set oDocuments = oApplication.Documents ' Note this is an early bound ... >>> Dim Firstname As String ...
    (microsoft.public.word.vba.general)
  • Re: String Reference Type
    ... Dim x As New MyInt ... The 'pointer' (lets call it a reference shall we) returned by the New ... Therefore its always allocated in the heap and a variable of string type is ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Send Word Data to Excel
    ... Did you create the Reference to Microsoft Scripting Runtime? ... Dim oWordApp As Word.Application ... Dim FileArrayAs String ... MsgBox "A folder was not selected" ...
    (microsoft.public.word.vba.general)
  • Re: recordset
    ... The problem is with the reference to the form inside the string. ... As you say, this works fine if you try it in the query window, because the ... > Dim rsf As DAO.Recordset ...
    (microsoft.public.access.formscoding)
  • Re: import longfilename.DBF
    ... This VBA function takes an ordinary filespec and returns the ... Function GetShortPath(ByVal FileSpec As String, ... Dim oFile As Object ...
    (microsoft.public.access.externaldata)

Loading