Consultor Eletrônico



Kbase P107388: How to search source code files in a directory for the occurrence of a given string?
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   20/11/2008
Status: Verified

GOAL:

How to search source code files in a directory for the occurrence of a given string?

FACT(s) (Environment):

All Supported Operating Systems
Progress 9.x
OpenEdge 10.x

FIX:

The following code scans a given directory for Progress Source Code (.p, .w and .i) files and reports on the occurrence of a given string by listing the line number, the procedure name and the line of code itself:
DEFINE VARIABLE cDirectoryName AS CHARACTER NO-UNDO.
DEFINE VARIABLE cProcedureName AS CHARACTER NO-UNDO.
DEFINE VARIABLE cSourceLine AS CHARACTER NO-UNDO.
DEFINE VARIABLE cTargetString AS CHARACTER NO-UNDO.
DEFINE VARIABLE cOutputFileName AS CHARACTER NO-UNDO.
DEFINE VARIABLE iLineNumber AS INTEGER NO-UNDO.
DEFINE STREAM sDirectory.
DEFINE STREAM sProcedure.
DEFINE STREAM sOutputFile.
SESSION:DEBUG-ALERT = TRUE.
ASSIGN
cDirectoryName = "C:\WRK91D"
cTargetString = "DEFINE VARIABLE"
cOutputFileName = "C:\WRK91D\myfile.out".
INPUT STREAM sDirectory FROM OS-DIR( cDirectoryName).
OUTPUT STREAM sOutputFile TO VALUE(cOutputFileName).
PUT STREAM sOutputFile UNFORMATTED "Line#" AT 1 "Procedue" AT 7 "Line" AT 27 SKIP.
REPEAT:
/* Get the current directory's .p, .i and .w files only */
IMPORT STREAM sDirectory cProcedureName.
IF LENGTH(cProcedureName) < 3 THEN NEXT.
/* If the file is a Progress Procedure Source Code file then process it */
IF cProcedureName MATCHES "*~~.p" OR cProcedureName MATCHES "*~~.i" OR cProcedureName MATCHES "*~~.w" THEN DO:
INPUT STREAM sProcedure FROM VALUE(cProcedureName).
iLineNumber = 1.
REPEAT:
IMPORT STREAM sProcedure UNFORMATTED cSourceLine.
IF INDEX ( cSourceLine , cTargetString) > 0 THEN
PUT STREAM sOutputFile UNFORMATTED iLineNumber AT 1 cProcedureName AT 7 cSourceLine AT 27 SKIP.
iLineNumber = iLineNumber + 1.
END.
INPUT STREAM sProcedureG> CLOSE.
END.
END.
INPUT STREAM sDirectory CLOSE.
OUTPUT STREAM sOutputFile CLOSE..