Listing, Searching and Deleting Files

James used Ask the Experts™
I am not sure what to search for without tons of junk, so I will ask here....

I want to list all the files of one specific directory in a listbox or whatever and optionally be able to delete them individually or load them for viewing in notepad. Be nice to be able to search for files by keywords and list only those that are found.


Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Guy Hengel [angelIII / a3]Billing Engineer
Most Valuable Expert 2014
Top Expert 2009

There are tons of ways to do this:

to list the files:
* use the filelistbox directly
* use the DIR command
* use some API (should be fastest)
* use the filesystemobject

After that, you want to filter:
* key filename or by content?

Then, to individually manage them:
* to view in notepad: shell "notepad.exe yourpath\yourfile"
* to delete: kill "yourpath\yourfile"



Preferably filter by content.

Guy Hengel [angelIII / a3]Billing Engineer
Most Valuable Expert 2014
Top Expert 2009

That means, after the listing of the files, you need to open and read each of the files to find out if it contains the keyword.

* read a file with VB directly:
dim filenumber as long
dim strLine as string
filenumber = freefile
OPEN "yourfile" FOR INPUT AS #filenumber
WHILE NOT EOF(filenumber)
  LINE INPUT #filenumber, strLine
CLOSE #filenumber

* read a file with FileSystemObject
dim fs as FileSystemObject
dim tf as TextStream
dim strData as string

set fs = new FileSystemObject
set tf = fs.OpenTextFile("filename", ForReading)
strData = tf.ReadAll

* some API (again fastest, but also more code)

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

to improve speed open files as binary access read and read them to byte array:
  dim b() as byte, size as integer
  redim b(size-1)
  get #1,,b
and then convert to string for searching:
  dim s as string

I'd suggest starting with a FileListBox.  Even though it's older technology, it does the job quite nicely here.

You can add a command button "Delete File" (name=cmdDelFile) which will then take the currently selected file, confirm that the user wants to delete that file, then "kill" it:

private m_strPath as string
m_strPath = app.path ' or whatever

private sub cmdDelFile_Click()
  dim mbxAns as VbMsgBoxResult

  if file1.filename <> vbNullstring then
    mbxAns = msgbox("Are you sure you want to delete file " & file1.filename, vbYesNo or vbQuestion
    if mbxAns = vbYes then
      kill m_strPath & file1.filename
end sub

To allow the file to be opened, assuming that notepad can interpret it:

private sub cmdView_Click()
  Shell Environ("ComSpec") & " /c notepad " & File1.FileName
end sub

Filter by content?  I assume this will be in a textbox (name=txtFind) and the filename will be placed into a separate listbox (name=lstResult)

private sub cmdFind_Click()
   dim strFilename as string
   dim iFileNumber as integer
   dim strFileContents as string

   strFilename = dir$(m_strPath & "\*.*")
   while strFilename <> vbNullstring
      iFileNumber = freefile()
      open strFilename for as #iFileNumber
      strFileContents = input$(lof(iFileNumber), #iFileNumber)
      close #iFileNumber

      if instr(strFileContents, txtFind.Text) > 0 then
         lstResult.AddItem txtFind.Text
      strFilename = dir$
end sub

the line:

open strFilename for as #iFileNumber

should be:

open strFilename for binary as #iFileNumber

Note that you don't need to use Get to read the data into a byte array since it can be read directly into a string using Input$.


OK all this code suggestions gave me ideas that helped a lot. You all deserved points.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial