Any better source code to search for file/folder
hi all,
I've just finished a small program to search some filetype on my harddisk. The code run very well but it seems to me that it 's a bit too.... lenghy. Is there anyway else to search file without having to use ADIR function?*//CODE
? SearchFile("D:","mp3") &&This will show the number of all mp3 files found on drive D:
*//Contain of file SearchFile.PRG
parameters cStartFolder, cExtention
private _nCount
store 0 to _nCount
dimension _sFound[1,1]
store '' to _sFound
do Search4Files with cStartFolder, cExtention
*//command to do something with a list of file stored in array _sFound
return _nCount
*//
procedure Search4Files(cFolder as string , cExtention as string)
local cUniqueName
private _afile, _afolder,i, j, cSubFolder
cUniqueName=sys(2015)
_afile=cUniqueName+"_F0"
_afolder=cUniqueName+"_D0"
dimension &_afile[1,5], &_afolder[1,5]
store "" to &_afile, &_afolder
if directory(cFolder) and !cFolder=="." and !cFolder==".."
if adir(&_afile,cFolder+"\*."+cExtention)>0 && found, add filename to array and text file
for i=1 to alen(&_afile,1)
do AddFile with cFolder+"\"+&_afile[i,1]
doevents
endfor
endif
if adir(&_afolder, cFolder+"\*.*","DHRS")>2
for j=3 to alen(&_afolder, 1)
doevents
if "D" $ &_afolder[j,5] and !&_afolder[j,1]=="." and !&_afolder[j,1]==".."
cSubFolder=cFolder+"\"+&_afolder[j,1]
if directory(cSubFolder)
wait window "Looking in '"+lower(cSubFolder) +"'..." nowait
*// write the folder's name to a text file on drive C:
=strtofile(lower(cSubFolder)+chr(13)+chr(10),"c:\Folders.txt",1)
do Search4Files with cSubFolder, cExtention
endif
endif
endfor
endif
endif
return
endproc
*//--
procedure AddFile (cFileToAdd as string) as Boolean
if type('cFileToAdd')='C' and !empty(cFileToAdd)
nAddRow=iif(empty(_sFound[1,1]),0,1)+alen(_sFound,1)
dimension _sFound[nAddRow,1]
_sFound[nAddData,1]=cFileToAdd
*// Append filename to a text file on drive C:
=strtofile(lower(cFileToAdd)+chr(13)+chr(10),"c:\FoundThem.txt",1)
_nCount=_nCount+1
return .t.
else
return .f.
endif
endfunc
[2388 byte] By [
kanguru] at [2008-2-26]
I prefer Filer.dll that ships with VFP. It can search multiple extensions at a time, supports things like contains 'text', doesn't have array limits based on VFP version etc. ie:
[code]
lnFiles = GetTree(Getdir(),'*.vc*',.T.)
Browse
Function GetTree
Lparameters tcStartDir,tcSkeleton,tlSubfolders, ;
tcSearchText1, tcSearchText2, tcSearchText3, ;
tlSearchAnd, tlWholeWords, tlWithCase
tcStartDir = Iif(Empty(m.tcStartDir),Sys(5)+Curdir(),m.tcStartDir)
tcSkeleton = Iif(Empty(m.tcSkeleton),'*.*',m.tcSkeleton)
tcSearchText1 = Iif(Empty(m.tcSearchText1), '', m.tcSearchText1)
tcSearchText2 = Iif(Empty(m.tcSearchText2), '', m.tcSearchText2)
tcSearchText3 = Iif(Empty(m.tcSearchText3), '', m.tcSearchText3)
Create Cursor filelist ;
(filepath c(50), filename c(20), ;
filesize i, fattr c(8), createtime T, lastacc T, lastwrite T)
oFiler = Createobject('filer.fileutil')
With oFiler
.SearchPath = m.tcStartDir
.SearchText1 = m.tcSearchText1
.SearchText2 = m.tcSearchText2
.SearchText3 = m.tcSearchText3
.SearchAnd = Iif(m.tlSearchAnd,1,0)
.IgnoreCase = Iif(m.tlWithCase,0,1)
.WholeWords = Iif(m.tlWholeWords,1,0)
.Subfolder = m.tlSubfolders && Check subfolders
.SortBy = 5 && Sort by LastWriteTime
.SortDirection = 1 && Descending 0-Ascending
.FileExpression = tcSkeleton && Search for skeleton
.Find(0)
For ix=1 To .Files.Count
With .Files(ix)
If !(Bittest(.Attr,4) And .Name = '.')
Insert Into filelist ;
(filepath, filename, filesize, fattr, createtime, lastacc, lastwrite) ;
values ;
(.Path, .Name, .Size, Attr2Char(.Attr), ;
Num2Time(.Datetime), Num2Time(.LastAccessTime), Num2Time(.LastWriteTime))
Endif
Endwith
Endfor
Return .Files.Count
Endwith
Function Num2Time
Lparameters tnFloat
Return Dtot({^1899/12/30}+Int(m.tnFloat))+86400*(m.tnFloat-Int(m.tnFloat))
Function Attr2Char
Lparameters tnAttr
Return ;
IIF(Bittest(m.tnAttr,0),'RO','RW')+;
IIF(Bittest(m.tnAttr,1),'H','_')+;
IIF(Bittest(m.tnAttr,2),'S','_')+;
IIF(Bittest(m.tnAttr,4),'D','_')+;
IIF(Bittest(m.tnAttr,5),'A','_')+;
IIF(Bittest(m.tnAttr,6),'E','_')+;
IIF(Bittest(m.tnAttr,7),'N','_')
[/code]
> Do you know anyway to use Unicode-UTF8 with VFP?
VFP cannot work with Unicode. Whenever you have a Unicode string, VFP converts it to ANSI using the current application codepage and locale. There are some parts in VFP that allow you to work with UTF-8 instead of ANSI, but these only affect access to COM objects.
If you want to manipulate Unicode strings, you have to use API functions. In the following sample, lower.txt contains the string in Unicode format. I created the file by copying your sample into Notepad and saving the textfile as a unicode file.
Declare LONG
_wcsupr in MSVCRT.dll String @
lcString = Substr(FileToStr("lower.txt"),3)
_wcsupr( @lcString )
StrToFile(lcString,"upper.txt",2)