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

Searching drive for a string

Status
Not open for further replies.

gfrlaser1

Technical User
Jan 31, 2013
28
US
I've searched this forum with no success. What I am trying to do is locate a string of text in one file, in one folder of one drive on my network. The string is always unique. For Instance, I want to find the string "123456789123456" that is contained in any of the many .csv files in the folder Y:\myfiles. I want to have it report back the file name it was found in, ex:12345.csv. Can this be accomplished easily enough from within my form?
 
If it will always be in a file in a single folder and you know what you're searching for ahead of time, you could use ADIR() to get the list of files, then loop through the files list and use FILETOSTR() to read data from each file into a string, then finally use AT() to see if your string is a substring.
If there are subdirectories it will get a little more complex...


-Dave Summers-
[cheers]
Even more Fox stuff at:
 
VFP also has an embedded full project search, doing what you want to do with all source files of the loaded and active project. It's called code references and is based on filer.dll, a more general purpose file search DLL. You are allowed to redistribute this. See "...you can distribute filer.dll and it's companion files..."

I think that wasn't always so, as faq184-139 offers an alternative under the assumption you can' distribute filer. Also faq184-140 shows a solution without filer.

Anyway filer.dll needs a setup with COM registration but otherwse is a few lines, as a slight modification of the sample code in the help topic:

Code:
oMyFiler = CREATEOBJECT('Filer.FileUtil')
oMyFiler.SearchPath = HOME() && Point search path to home directory.
oMyFiler.FileExpression = '*.TXT' && Specify text file search. && might change to *.* or *.csv
oMyFiler.SearchText1 = "filer.dll" && also see the help topic about more search options
oMyFiler.Find(0) && again the help tells what 0 means here
FOR nFileCount = 1 TO oMyFiler.Files.Count
   ? oMyFiler.Files.Item(nFileCount).Name
ENDFOR
That by the way shows filer is found in the redist.txt file about all redistributable files.

That said you don't need to justify any question by assuring you searched the whole forum. It doesn't matter at all. But if you say so, it could get embarrassing.
But that's it, I'm sure nobody will blame you for not searching an already given answer first.

Welcome and I even believe you searched, just not the right terms or a too specific question. Maybe a little recommendations for that are welcome too? Simple search terms still lead the way. In the FAQ section use CTRL+F to search and highlight the titles of FAQs with "file" in them, that would have been one way. It's the central term for search within a file, also "search" highlights those FAQs.

You may also use Windows Search Index, if you didn't turn that off and if you add your main folder to it, it'll at least index all file types which have a IFilter, which includes txt and csv. I gave a sample in thread184-1452607

This sample finds more files containing "filer.dll", as it looks in the index of all files and still works in Win7 and 8:
Code:
Local loConn, loRS, loPath, loFile, lcFilename

loConn=CREATEOBJECT("adodb.connection")
loConn.ConnectionString="Provider=Search.CollatorDSO;Extended Properties='Application=Windows';"
loConn.Open()

loRs = loConn.Execute("SELECT System.ItemFolderPathDisplay, System.Filename FROM SystemIndex WHERE CONTAINS('filer.dll')")
loRS.Movefirst()
Do While Not loRS.EOF
   loPath = loRs.Fields.Item("System.ItemFolderPathDisplay")
   loFile = loRs.Fields.Item("System.Filename")

   lcFilename =  Addbs(loPath.Value)+loFile.Value
   ? lcFilename
   loRS.Movenext()
EndDo

Bye, Olaf.
 
thanks for the replies. This looks a bit over my head. I will give it a shot though.
 
This looks a bit over my head. I will give it a shot though.

I think we might have confused you by talking about the need to traverse directories and sub-directories. But you did say that all the files would be in a single folder, in which case the solution is simpler. The following code should give you a start:

Code:
lcStringToFind = "123456789123456"
lcPath = "y:\myfiles"
lnCount = ADIR(laFiles, ADDBS(lcPath) + "*.csv")
FOR lnI = 1 TO lnCount
  lcFile = FORCEPATH(laFiles(lnI, 1), lcPath)
	? "Now searching " + lcFile
  lcContents = FILETOSTR(lcFile)
  IF OCCURS(UPPER(lcStringToFind), UPPER(lcContents)) > 0
    ? "String found in " + lcFile
  ENDIF 
ENDFOR

Mike




__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Well, you don't need to do all the alternatives,

so I'd say the filer solution is the simplest besides rolling your own filer.

With a little more explanation:
Code:
*** Init
* 'Filer.FileUtil' is a class in filer.dll, which you can copy and use anywhere without copyyright infringement
oMyFiler = CREATEOBJECT('Filer.FileUtil') 


*** Configuration of Filer for a single search
* 1. Path, HOME() is just a special VFP path, put in your directory instead
oMyFiler.SearchPath = HOME() && Point search path to home directory. 

* Again change as you like, if you want to search and find any file. make this *.*
oMyFiler.FileExpression = '*.TXT' && Specify text file search. && might change to *.* or *.csv

* In my sample I search for filer.dll itself, your search text is what you like or a user enters:
oMyFiler.SearchText1 = "filer.dll" && also see the help topic about more search options

*** Search
* This starts filers searching
oMyFiler.Find(0) && again the help tells what 0 means here

*** Show Results
* And this shows all the search results in a loop
FOR nFileCount = 1 TO oMyFiler.Files.Count
   ? oMyFiler.Files.Item(nFileCount).Name
ENDFOR

Bye, Olaf.
 
yes! Thanks to you all. Mike your sample code was something I can understand adapt to my needs. Have a great 4th everyone
 
If it helps, I keep this simple line of code for FILER.DLL saved in my command history:

DO FORM (HOME(1) + 'Tools\Filer\Filer.scx')
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top