Link to home
Start Free TrialLog in
Avatar of James
James

asked on

Listing, Searching and Deleting Files

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.

Thanks!


 
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

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"

CHeers
Avatar of James
James

ASKER

Preferably filter by content.

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
WEND
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)

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

ASKER CERTIFIED SOLUTION
Avatar of rspahitz
rspahitz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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$.
Avatar of James

ASKER

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