Find a file function

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

Post Reply
User avatar
serge_girard
Posts: 1872
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Has thanked: 293 times
Been thanked: 67 times
Contact:

Find a file function

Post by serge_girard » Thu Nov 02, 2017 11:03 am

Hello,

Does anybody know if there is a function that returns drive+path to a specific file.
Example: I what to know where 'somefile.txt' is located on ALL or 1 drive at my computer. Such as :

Code: Select all

cFILE  := 'SOMEFILE.TXT'
aFILES := Find_File(cFILE)   // or Find_File(cFILE, 'C:')

/*
aFILES [1] could then contain 'C:\xxx\somefile.txt'
aFILES [2] could then contain 'F:\zzz\somefile.txt'
aFILES [3] could then contain 'H:\aaa\somefile.txt'
etc...
Thanks, Serge

User avatar
PeteWG
Posts: 107
Joined: Sun Mar 21, 2010 5:45 pm
Has thanked: 3 times
Been thanked: 27 times

Post by PeteWG » Thu Nov 02, 2017 12:51 pm

Maybe
hb_DirScan(<cPath>, [<cFileMask>], [<cAttr>]) ➜ aFiles)
is what you're looking for.

Code: Select all

PROCEDURE Main()
 local a := hb_DirScan( "C:\harbour", "*.txt" )
 aeval( a, {|e| qout( e[1] ) } )
 return
Keep in mind that searching an entire hard disk could be quite time consuming, so you have to warn user(s)
[ and probably beg for their patience.. 8-) ]

regards,
Pete

User avatar
serge_girard
Posts: 1872
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Has thanked: 293 times
Been thanked: 67 times
Contact:

Post by serge_girard » Thu Nov 02, 2017 2:11 pm

Thanks Pete !

Forgot all about hb_dirscan! Indeed user(s) must be warned, but this way is better than searching via Windows-explorer!

Serge

User avatar
andyglezl
Posts: 848
Joined: Fri Oct 26, 2012 7:58 pm
Location: Guadalajara Jalisco, MX
Has thanked: 7 times
Been thanked: 24 times
Contact:

Post by andyglezl » Sun Nov 05, 2017 3:42 pm

Bueno, yo trato de aprovechar lo que ya está hecho, si no cumple
con lo que necesito, entonces empiezo el desarrollo.
--------------------------------------------------------------------------------------
Well, I try to take advantage of what's already done, if it doesn't meet
With what I need, then I start developing.

Code: Select all

SHELLEXECUTE( 0, "find" , , , "C:\",5 ) )
Andrés González López
Desde Guadalajara, Jalisco. México.

User avatar
serge_girard
Posts: 1872
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Has thanked: 293 times
Been thanked: 67 times
Contact:

Post by serge_girard » Sun Nov 05, 2017 5:40 pm

Andres,

That what I just tried to avoid: Windows-Explorer's find.
When this is used, you might get what you want: some text in some files and some files that meet the filename. Then click on name (for example) and it restarts searching again. I just need to find filenames, for example: demo.exe and not files that contain demo.exe.

I made a little extract :

Code: Select all

#include "hmg.ch"
#INCLUDE "Directry.CH"
#INCLUDE "Fileio.CH"



FUNCTION MAIN()
/***************/
PUBLIC cCurrentSRT   := GetStartUpFolder() 
PUBLIC lSTOP         := .F.
PUBLIC aDrives	      := {}

AllDrives()


DEFINE WINDOW        Form_MAIN   ;
   AT                0,0	   ;
   WIDTH             500   	;  
   HEIGHT            700  	;
   ICON              "DEFAULT"	;
   TITLE             ' FIND a FILE  '   ;
   BACKCOLOR         SILVER;
   MAIN; 


   @ 10, 10 TEXTBOX  TB_FILE ;   
      FONTCOLOR      BLUE   ;
      BACKCOLOR      WHITE ;
      WIDTH          300	  ;  
      HEIGHT         25	 ; 
      MAXLENGTH      200;
      FONT           'Arial' SIZE 12		;
      VALUE          ''  ;
      ON ENTER       FIND_FILE()
 
   DEFINE BUTTON     Start_GO
      ROW            10
      COL            320 
      WIDTH          50
      HEIGHT         20
      ACTION         FIND_FILE()
      CAPTION        "GO"
      TRANSPARENT    .T.
      TOOLTIP        "GO"
   END BUTTON

   DEFINE BUTTON     Start_STOP
      ROW            10
      COL            380 
      WIDTH          50
      HEIGHT         20
      ACTION         Stop_Search()
      CAPTION        "STOP"
      TRANSPARENT    .T.
      TOOLTIP        "STOP"
   END BUTTON

   DEFINE GRID       Grid_Find_FILES
      ROW            40
      COL            10
      WIDTH          400
      HEIGHT         300
      HEADERS        {'Drive folder' } 
      WIDTHS         { 665             } 
      JUSTIFY        {GRID_JTFY_LEFT   }  
      ITEMS          {}
   END GRID      

   @ 360,10  CHECKBOX CH_SHOP_FIRST;
      CAPTION        'Stop when first file is found' ;
      WIDTH          200 ;
      HEIGHT         25 ;
      VALUE           .T.    ;  
      BACKCOLOR      SILVER	;
      FONT           'Arial' ;
      SIZE           8  

   nROW := 400
   FOR a = 1 TO LEN(aDrives)
      CDR := 'DR' + ALLTRIM(STR(a))
      @ nROW,10  CHECKBOX &CDR;
         CAPTION        'Drive ' + aDrives [a];
         WIDTH          200 ;
         HEIGHT         25 ;
         VALUE           .T.    ;  
         BACKCOLOR      SILVER	;
         FONT           'Arial' ;
         SIZE           8  
      nROW := nROW + 30
   NEXT

END WINDOW

CENTER WINDOW   Form_MAIN
ACTIVATE WINDOW Form_MAIN






FUNCTION FIND_FILE()
/*******************/
cFILE       := ALLTRIM(Form_MAIN.TB_FILE.Value)
lSHOP_FIRST := Form_MAIN.CH_SHOP_FIRST.Value
cDEBUG      := 'C:\TEST\DEBUG.TXT'

IF EMPTY(cFILE) 
   RETURN
ENDIF

SET PRINTER TO &cDEBUG 
SET PRINTER ON 
SET CONSOLE OFF

? 'cFILE', cFILE

//lALL_DRIVES := Form_MAIN.CH_ALL_DRIVES.Value

aSEL_DRIVES := {} 
FOR a = 1 TO LEN(aDrives)
   cDR := 'DR' + ALLTRIM(STR(a))
   IF GetProperty ( "Form_MAIN", cDR, "Value" )
      AADD(aSEL_DRIVES, aDrives [a])
   ENDIF
NEXT   
FOR a = 1 TO LEN(aSEL_DRIVES)
   ? aSEL_DRIVES [a]
NEXT 


DELETE ITEM ALL FROM Grid_Find_FILES OF Form_MAIN




//aFilter := {"*.wav","*.flac","*.mp3","*.wma"}
aFilter  := {cFILE}
FOR a = 1 to len(aSEL_DRIVES)
   ? 'DRIVE' , aSEL_DRIVES [a]
   nError := DIRCHANGE( ALLTRIM(aSEL_DRIVES [a]) + '\' ) 

   IF lSTOP       
      EXIT
   ENDIF

	aDir := MF_DirScan(aSEL_DRIVES [a], aFilter)

   DO EVENTS
	FOR b = 1 to len(aDir)
      ADD ITEM { aSEL_DRIVES [a] + '\' + aDIR [b][1] } TO Grid_Find_FILES OF Form_MAIN   
      IF lSHOP_FIRST
         lSTOP := .T.
         EXIT
      ENDIF
   NEXT
NEXT   
lSTOP    := .F.
nError   := DIRCHANGE( 'C:\' ) 
DISKCHANGE( cCurrentSRT ) // BACK TO DEFAULT
MSGINFO('Ready')
RETURN





FUNCTION Mf_DirScan(cPath, aFileMask, cAttr)
/********************************************/
RETURN DoMultiScan(hb_DirSepAdd(hb_defaultValue( cPath, "")),;
IF (HB_ISARRAY(aFileMask),aFileMask,MsgStop("First parameter should be an array")), hb_defaultValue(cAttr,""),hb_ps() )

 



STATIC FUNCTION DoMultiScan( cPath, aMask, cAttr, cPathSep )
/***********************************************************/
LOCAL aFile, lMatch, aResult := {}
? 'DoMultiScan' , cPath

FOR EACH aFile IN hb_Directory( cPath + hb_osFileMask(), cAttr + "D" )
   DO EVENTS

   IF lSTOP       
      RETURN {} // EMPTY ARRAY !!
   ENDIF


   lMatch:=.F.
	AEVAL(aMask, {|x| if(hb_FileMatch(aFile[ F_NAME ],x), lMatch:=.T.,)}) // flag remains .T. if a .F. is discovered in next element eval
   
   IF "D" $ aFile[ F_ATTR ]
      IF lMatch .AND. "D" $ cAttr
         AADD( aResult, aFile )
      ENDIF

		IF !( aFile[ F_NAME ] == "." .OR. aFile[ F_NAME ] == ".." .OR. aFile[ F_NAME ] == "" )
			AEVAL( DoMultiScan( cPath + aFile[ F_NAME ] + cPathSep, aMask, cAttr, cPathSep ), ;
			{| x | x[ F_NAME ] := aFile[ F_NAME ] + cPathSep + x[ F_NAME ], ;
			AADD( aResult, x ) } )
		ENDIF
	ELSEIF lMatch
		AADD( aResult, aFile )
	ENDIF
NEXT
RETURN aResult






FUNCTION AllDrives()
/******************/
LOCAL wI 

FOR wI := 3 TO 26
	IF DISKCHANGE( CHR(wI + 64) )
		AADD(aDrives, CHR(wI + 64) + ':')	 
	ENDIF
NEXT
DISKCHANGE( cCurrentSRT ) // BACK TO DEFAULT
RETURN 






FUNCTION Stop_Search()
/*********************/
lSTOP := .T.
RETURN
Serge
Attachments
demoff.rar
(1.22 MiB) Downloaded 21 times

Post Reply