Click here for a screen shot of the GUI in this script.


AddExtender("wsrch34i.dll")

;Define temporary files

if !DirExist("C:\Temp") then DirMake("C:\Temp")
tempfile1="C:\Temp\$ETL$TEMP$FILE$1.TXT"
tempfile2="C:\Temp\$ETL$TEMP$FILE$2.TXT"

:begin
FileVer32Format=`WWWDLGED,5.0`

FileVer32Caption=`File Version Utility V3.41 - 4 December 1998`
FileVer32X=50
FileVer32Y=83
FileVer32Width=292
FileVer32Height=146
FileVer32NumControls=8

FileVer3201=`16,24,96,DEFAULT,STATICTEXT,DEFAULT,"Starting Drive and Directory:"`
FileVer3202=`26,52,86,DEFAULT,STATICTEXT,DEFAULT,"File Extension to look for:"`
FileVer3203=`10,80,102,DEFAULT,STATICTEXT,DEFAULT,"File and path for search result:"`
FileVer3204=`126,24,148,DEFAULT,EDITBOX,Drive,"C:\"`
FileVer3205=`126,52,64,DEFAULT,EDITBOX,Extension,"*.*"`
FileVer3206=`126,80,148,DEFAULT,EDITBOX,outfile,"C:\MOVE.TXT"`
FileVer3207=`36,122,126,DEFAULT,PUSHBUTTON,DEFAULT,"OK",1`
FileVer3208=`184,122,64,DEFAULT,PUSHBUTTON,DEFAULT,"CANCEL",0`

ButtonPushed=Dialog("FileVer32")

;remove any existing outfile
;create attempt makes sure any existing file is not read-only and directory exists, etc
new = FileOpen(outfile, "WRITE")
FileClose(new)

;Check Starting Drive and Directory format
If StrLen(drive)<3
Message("Entry Error","The Starting Drive and Directory format is incorrect%@CRLF%%@CRLF%Press OK to re-enter this parameter")
goto begin
endif

;Check Starting Drive and Directory exist
If !Direxist(drive)
Message("Entry Error","The Starting Drive and Directory cannot be found%@CRLF%%@CRLF%Press OK to re-enter this parameter")
goto begin
endif

;Check Extension for correct format
If StrLen(Extension)<1 ;ie has to be at least * for example
Message("Entry Error","The File Extension format is incorrect%@CRLF%%@CRLF%Press OK to re-enter this parameter")
goto begin
endif

fh=FileOpen(tempfile1,"WRITE")

Edcount=0

BoxOpen("Searching from %Drive%","")
If StrSub(Drive,StrLen(Drive),1)!= "\" Then Drive=StrCat(Drive,"\")

handle=srchInit(Drive,Extension,"",0,16)

:start
Edfile=srchNext(handle)
if Edfile=="" then goto finish
Edcount=Edcount+1
pos=StrIndex(Edfile,"\",0,@backscan)+1
filename=StrSub(Edfile,pos,-1)
ss=FileSize(Edfile)

:loop
;pv=FileVerInfo(Edfile,"","ProductVersion")
;if pv=="" then pv="None"
:nextp ;fv=FileVerInfo(Edfile,"","FileVersion")
;if fv=="" then fv="None"
ee=strfix(Edcount," ",6)
ss=strfix(ss," ",10)
;pv=strfix(pv," ",20)
;fv=strfix(fv," ",20)
filename=strfix(filename," ",20)
FileWrite(fh,Edfile)
BoxText(Edfile)
goto start
:finish
srchFree(handle)

FileClose(fh)
;Now check to make sure there is something to sort

If Edcount==0
BoxShut()
; Message("File Search complete","Nothing found matching entry parameters")
;cleanup
if FileExist(tempfile2) then FileDelete(tempfile2)
if FileExist(tempfile1) then FileDelete(tempfile1)
Choice=AskYesNo("FILE SEARCH COMPLETE","Nothing found matching entry parameters%@CRLF%%@CRLF%Do you want to run again?")
if Choice==@yes then goto begin
EXIT
Endif

Boxtitle("File Search complete")

Boxtext("Sorting Results - Please Wait")

old = FileOpen(tempfile1, "READ")
;Find the longest string in the file
longest=0
loopcount=0
while @TRUE ; Loop till break do us end
xetl = FileRead(old)
If xetl == "*EOF*" Then Break
loopcount=loopcount+1
length=strlen(xetl)
if length>longest then longest=length
endwhile
FileClose(old)

If loopcount==0 ;Nothing was found
BoxShut()
; Message("File Search complete","Nothing found matching entry parameters")
;cleanup
if FileExist(tempfile2) then FileDelete(tempfile2)
if FileExist(tempfile1) then FileDelete(tempfile1)
Choice=AskYesNo("FILE SEARCH COMPLETE","Nothing found matching entry parameters%@CRLF%%@CRLF%Do you want to run again?")
if Choice==@yes then goto begin
EXIT
Endif

;Now create a sort file with fixed length records
;by adding spaces to the end of shorter lines

;A check for disk space could go here
;Bytesrequired = loopcount*longest

;create sort file
old = FileOpen(tempfile1, "READ")
new = FileOpen(tempfile2, "WRITE")
while @TRUE ; Loop till break do us end
xetl = FileRead(old)
If xetl == "*EOF*" Then Break
xetl=strfix(xetl," ",longest)
FileWrite(new, xetl)
endwhile
FileClose(new)
FileClose(old)

;Now sort the entries in the file
RecSize=longest+2 ;as there is a CRLF at the end of each line
NameOffset=0 ;we are sorting on the filename, which is the first field
NameSize=20 ;the parameter: filename is fixed at 20 chars in the search routine

dbsize=FileSize(tempfile2)
db=BinaryAlloc(dbsize)
BinaryRead(db,tempfile2)
BinarySort(db,RecSize,NameOffset,NameSize,@STRING|@ASCENDING)

BinaryWrite(db,tempfile2)
BinaryFree(db)

;Finally, get rid of trailing spaces in the temp file
;and write to the filename chosen by the user

old = FileOpen(tempfile2, "READ")
new = FileOpen(outfile, "WRITE")
while @TRUE ; Loop till break do us end
xetl = FileRead(old)
If xetl == "*EOF*" Then Break
xetl=strtrim(xetl)
FileWrite(new, xetl)
endwhile
FileClose(new)
FileClose(old)

;cleanup temp files
FileDelete(tempfile2)
FileDelete(tempfile1)

;Message("Longest Line = %longest%","Space required = %Bytesrequired% %@crlf% Number of files = %loopcount%")

BoxShut()
;Message("Sort complete","Result in %outfile%")
Choice=AskYesNo("SORT COMPLETE","Result in:%@CRLF%%outfile%%@CRLF%%@CRLF%Do you want to run again?")
if Choice==@yes then goto begin

Exit
:cancel
display(2,"File Version Utility","Program Cancelled")
Exit