Page 1 of 1

Buscar un archivo en un disco y sus directorios

PostPosted: Tue Mar 26, 2019 9:39 pm
by Enrrique Vertiz
Saludos estimados

No se si existe en FWH o en xHB alguna funcion que realice la busqueda de un archivo en una unidad y sus directorios y subdirectorios ?? si alguien tiene el dato o alguna funcion se lo agradecere ...

Re: Buscar un archivo en un disco y sus directorios

PostPosted: Tue Mar 26, 2019 10:16 pm
by cnavarro
Prueba con

Xbrowse( HB_DirScan( "C:\", "mifile.txt", "A" ) )

Re: Buscar un archivo en un disco y sus directorios

PostPosted: Wed Mar 27, 2019 4:06 am
by Enrrique Vertiz
Saludos, GRACIAS efectivamente es los que necesito, pero hay un problema, es de Harbour y Yo uso xHarbour, estuve buscando la equivalencia en xHb pero no la encuentro, existira ??
Gracias nuevamente.

Re: Buscar un archivo en un disco y sus directorios

PostPosted: Wed Mar 27, 2019 11:11 am
by cnavarro
No me habia dado cuenta del detalle de que sigues utilizando xHarbour
Aqui tienes el source
Code: Select all  Expand view

https://github.com/harbour/core/blob/master/src/rtl/dirscan.prg
 

Re: Buscar un archivo en un disco y sus directorios

PostPosted: Thu Mar 28, 2019 5:19 pm
by karinha

Re: Buscar un archivo en un disco y sus directorios

PostPosted: Thu Mar 28, 2019 5:38 pm
by karinha
Excelente, más solo funciona con Harbour.

Code: Select all  Expand view

#include "FiveWin.ch"
#include "directry.ch"
#include "fileio.ch"

PROC TestDScan()

   LOCAL aFFList := HB_DirScan( "C:\TEMP" )
   LOCAL x1Row

   /*
   FOR EACH x1Row IN aFFList

      ? PAD( x1Row[ 1 ], 23 ), ;                     // Name
        TRAN( x1Row[ 2 ], '999,999,999,999' ), ;     // Size
        x1Row[ 3 ], ;                                // Date
        x1Row[ 4 ], ;                                // Time
        x1Row[ 5 ]                                   // Attributes
   NEXT x1Row
   */


   Xbrowse( HB_DirScan( "C:\TEMP", "TESTE", "A" ) )

RETURN NIL // TestDScan()

STATIC FUNCTION hb_doScan( cPath, cMask, cAttr, cPathSep )

   LOCAL aFile
   LOCAL lMatch
   LOCAL aResult := {}

   FOR EACH aFile IN hb_vfDirectory( cPath + hb_osFileMask(), cAttr + "D" )

      lMatch := hb_FileMatch( aFile[ F_NAME ], cMask )

      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( hb_DoScan( cPath + aFile[ F_NAME ] + cPathSep, cMask, 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 hb_DirScan( cPath, cFileMask, cAttr )

RETURN hb_DoScan( hb_DirSepAdd( hb_defaultValue( cPath, "" ) ),    ;
       iif( HB_ISSTRING( cFileMask ), cFileMask, hb_osFileMask() ), ;
       hb_defaultValue( cAttr, "" ), ;
       hb_ps() )

FUNCTION hb_DirRemoveAll( cDir )

   LOCAL aFile, cPath, cFile, nAttr

   IF ! Empty( cDir ) .AND. hb_vfDirExists( cDir )
      cPath := hb_DirSepDel( cDir )
      IF hb_vfAttrGet( cPath, @nAttr ) .AND. ! hb_bitAnd( nAttr, HB_FA_READONLY ) == 0
         hb_vfAttrSet( cPath, hb_bitXor( nAttr, HB_FA_READONLY ) )
      ENDIF
      cPath := hb_DirSepAdd( cPath )
      FOR EACH aFile IN hb_vfDirectory( cPath + hb_osFileMask(), "HSDL" )
         IF "D" $ aFile[ F_ATTR ] .AND. ! "L" $ aFile[ F_ATTR ]
            IF !( aFile[ F_NAME ] == "." .OR. aFile[ F_NAME ] == ".." .OR. aFile[ F_NAME ] == "" )
               IF ! hb_DirRemoveAll( cPath + aFile[ F_NAME ] )
                  RETURN .F.
               ENDIF
            ENDIF
         ELSE
            cFile := cPath + aFile[ F_NAME ]
            IF "R" $ aFile[ F_ATTR ] .AND. hb_vfAttrGet( cFile, @nAttr )
               hb_vfAttrSet( cFile, hb_bitAnd( nAttr, hb_bitNot( HB_FA_READONLY ) ) )
            ENDIF
            IF ! hb_vfErase( cFile ) == 0
               RETURN .F.
            ENDIF
         ENDIF
      NEXT
      RETURN hb_vfDirRemove( cDir ) == 0
   ENDIF

RETURN .T.

FUNCTION hb_FileDelete( cFileMask, cAttr )

   LOCAL lAny := .F., aFile, cPath, cFile, cAttrMask, nAttr

   IF HB_ISSTRING( cFileMask ) .AND. ! Empty( cFileMask ) .AND. ;
      ! hb_vfDirExists( cFileMask )
      cPath := hb_FNameDir( cFileMask )
      cAttrMask := StrTran( hb_defaultValue( cAttr, "" ), "D" ) + "L"
      FOR EACH aFile IN hb_vfDirectory( cFileMask, cAttrMask )
         cFile := cPath + aFile[ F_NAME ]
         IF "R" $ aFile[ F_ATTR ] .AND. hb_vfAttrGet( cFile, @nAttr )
            hb_vfAttrSet( cFile, hb_bitAnd( nAttr, hb_bitNot( HB_FA_READONLY ) ) )
         ENDIF
         IF hb_vfErase( cFile ) == 0
            lAny := .T.
         ENDIF
      NEXT
   ENDIF

RETURN lAny
 


Saludos.

Re: Buscar un archivo en un disco y sus directorios

PostPosted: Thu Mar 28, 2019 10:32 pm
by Enrrique Vertiz
Saludos y gracias

Es correcto solo funciona en HB para xHB he tenido que usar : DirectoryRecurse() , pues trate de compilar el source pero daba muchos errores.