Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"Search in files" dialog (regexp, replace)
#13
Function QuickFindFile_Index
Code:
Copy      Help
;/
function $databaseFile $folders

;Creates database for <tip>QuickFindFile_Find</tip>.
;Error if fails.

;databaseFile - database file. Ex: "$my qm$\x.db3"
;folders - list of folders. Will get all file and folder paths from these folders. Ex: "C:[]E:\Folder"


Sqlite db.Open(databaseFile)
db.Exec("DROP TABLE files"); err
db.Exec("BEGIN TRANSACTION")
db.Exec("CREATE TABLE files (path)")

str f
foreach f folders
,f+iif(f.end("\") "*" "\*")
,Dir d
,foreach(d f FE_Dir 0x6)
,,str sPath=d.FileName(1)
,,sPath.lcase ;;in Sqlite LIKE, Unicode chars case sensitive
,,sPath.SqlEscape
,,db.Exec(F"INSERT INTO files VALUES ('{sPath}')")

db.Exec("END TRANSACTION")

err+ end _error

;info: with sqlite 50% slower than with raw txt file

Function QuickFindFile_Find
Code:
Copy      Help
;/
function $databaseFile $filePattern ARRAY(str)&results

;Finds files in database created by <tip>QuickFindFile_Index</tip>.
;Error if fails.

;databaseFile - database file.
;filePattern - file pattern. Must match full path. Examples: "*.txt", "C:\*.txt", "C:\Folder\*", "*\file.txt".
;results - receives full paths of found files and folders.


Sqlite db.Open(databaseFile)

str s=filePattern
s.lcase
s.findreplace("`" "``")
s.findreplace("%" "`%")
s.findreplace("_" "`_")
s.findreplace("*" "%")
s.findreplace("?" "_")
s.SqlEscape
db.Exec(F"SELECT path FROM files WHERE path LIKE '{s}' ESCAPE '`'" results)

err+ end _error

;info: with sqlite 5 times slower than with raw txt file. Not faster if we get all and use matchw.

test

Macro Macro1479
Code:
Copy      Help
out

str dbFile="$my qm$\QuickFindFile_Index.db3"

int t1=timeGetTime

QuickFindFile_Index dbFile "$qm$[]$my qm$"
;QuickFindFile_Index dbFile "c:"

int t2=timeGetTime

ARRAY(str) a
QuickFindFile_Find dbFile "*.chm" a

int t3=timeGetTime
out "%i %i" t2-t1 t3-t2

for(_i 0 a.len) out a[0 _i]


_____________________________________________________________________________________

The same with txt database.
Indexing 33% faster.
Searching 5 times faster.
Speed tested with ~150000 files.

Function QuickFindFile_Index2
Code:
Copy      Help
;/
function $databaseFile $folders

;Creates database for <tip>QuickFindFile_Find2</tip>.
;Error if fails.

;databaseFile - database file. Ex: "$my qm$\x.txt"
;folders - list of folders. Will get all file and folder paths from these folders. Ex: "C:[]E:\Folder"


str s f
__HFile h.Create(databaseFile CREATE_ALWAYS GENERIC_WRITE)

foreach f folders
,f+iif(f.end("\") "*" "\*")
,Dir d
,foreach(d f FE_Dir 0x6)
,,s.addline(d.FileName(1))
,,if s.len>10000
,,,;g1
,,,if(!WriteFile(h s s.len &_i 0)) end _s.dllerror
,,,s.fix(0 1)
,if(s.len) goto g1

err+ end _error

Function QuickFindFile_Find2
Code:
Copy      Help
;/
function $databaseFile $filePattern ARRAY(str)&results

;Finds files in database created by <tip>QuickFindFile_Index2</tip>.
;Error if fails.

;databaseFile - database file.
;filePattern - file pattern. Must match full path. Examples: "*.txt", "C:\*.txt", "C:\Folder\*", "*\file.txt".
;results - receives full paths of found files and folders.


str s f
int na(65000) nr nrTotal

results=0

__HFile h.Create(databaseFile OPEN_EXISTING GENERIC_READ FILE_SHARE_READ)
rep
,if(!ReadFile(h s.all(na) na &nr 0)) end _s.dllerror
,if(!nr) break
,s.fix(nr)
,s.fix(findcr(s 10)+1)
,
,foreach f s
,,if(matchw(f filePattern 1)) results[]=f
,
,nrTotal+s.len
,SetFilePointer h nrTotal 0 0

err+ end _error

test

Macro Macro1481
Code:
Copy      Help
out

str dbFile="$my qm$\QuickFindFile_Index.txt"

int t1=timeGetTime

QuickFindFile_Index2 dbFile "$qm$[]$my qm$"
;QuickFindFile_Index2 dbFile "c:"

int t2=timeGetTime

ARRAY(str) a
QuickFindFile_Find2 dbFile "*.chm" a

int t3=timeGetTime

out "%i %i" t2-t1 t3-t2

out a


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)