Extract Lines

/*
Extract Lines
Extract specified lines from a text file.
 
 Build script : 
 
 c:\harbour\bin\hbmk2 ExtrcLines -lhbxpp -lxhb -lhbmisc -lhbct
 
 Run : ExtrcLines <cInFile> <cKeyWord>
Original file backed-up to a unique-name folder.
 
 In this sample lines includes <cKeyword> extracted and case in-sensitive comparation made. 
 
 For different conditions you can change above line 53 :
 
 IF ! ( UPPER( cKeyWord ) $ UPPER( c1Line ) )
 
*/
PROCEDURE Main( cInFile, cKeyWord )
LOCAL oFile, c1Line, cBackFOld, cBackUpFile
 LOCAL lFirstLine := .T.
 
 SET( _SET_EOF, .F. )
 
 IF !EMPTY( cInFile )
 
 SetMode( 25, 80 )
 CLS
 
 IF !EMPTY( cKeyWord )
cBackFOld := HL_UniqFFName( ".\BackUp" )
 MakeDir( cBackFOld ) 
 
 cBackUpFile := cBackFOld + "\" + cInFile
 
 COPY FILE (cInFile) TO (cBackUpFile)
 
 IF FILE( cBackUpFile )
 oFile := TFileRead():New( cBackUpFile )
 oFile:Open()
 
 IF oFile:Error()
 QOUT( oFile:ErrorMsg( "FileRead: " ) )
 ELSE
 SET ALTE TO ( cInFile ) 
 SET ALTE ON
 WHILE oFile:MoreToRead()
 c1Line := oFile:ReadLine()
 IF ! ( UPPER( cKeyWord ) $ UPPER( c1Line ) )
 IF lFirstLine
 QQOUT( c1Line )
 lFirstLine := .F.
 ELSE
 QOUT( c1Line )
 ENDIF 
 ENDIF 
 END WHILE
 oFile:Close()
 END IF
 ELSE
 ALERT( cInFile + " file not found." )
 ENDIF FILE( cInFile )
 SET ALTE OFF
 SET ALTE TO 
 ELSE
 ALERT( " Key word not specified." )
 ENDIF !EMPTY( cKeyWord )
 ELSE
 ALERT( " Input file not specified." )
 ENDIF !EMPTY( cInFile )
 
RETURN // ExtractLines.Main()
*.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._
FUNCTION HL_UniqFFName( ; // Unique File / Folder name
 cPrefix,; 
 cExtension ) 
 
 LOCAL cRVal,;
 nFFNamLen,; 
 nIterate := 0
IF HB_ISNIL( cExtension )
 cExtension := ''
 ENDIF
 
 IF !EMPTY( cExtension ) .AND. LEFT( cExtension, 1 ) # "."
 cExtension := "." + cExtension
 ENDIF
 
 cRVal := cPrefix + NTOC( VAL( TToS( HB_DATETIME() ) ), 36 ) 
 nFFNamLen := LEN( cRVal )
WHILE HB_DIREXISTS( cRVal + cExtension ) 
 cRVal := LEFT( cRVal, nFFNamLen ) + LTRIM( STR( ++nIterate, 0 ) )
 ENDDO
cRVal += cExtension
 
RETURN cRVal // HL_UniqFFName()
*.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._
Advertisements