Search for a File(s)

emidmode

New Member
Hello

I have a problem: I need to search for a file in a directory and it's subdirectories, I mean a search recursively until find all the instances of the file within the directory, but I don't have a good option in 4gl to do this, I used INPUT THROUGH "dir <my file.txt> /s/b" but is very slow to respond because the directory is big Please, help me; somebody know about a some dll's functions (or better being used) to search quickly in a directory to retrieve the location(s) of the file within the directory???
 
the file have to be into the PROPATH.

DEF VAR lc AS CHAR NO-UNDO.
lc = SEARCH ("file.p").
MESSAGE lc VIEW-AS ALERT-BOX.
 
if your file is not in the PROPATH,
and you have a linux OS:

/* recursiveSearch.p */
DEFINE VARIABLE lcDir AS CHARACTER NO-UNDO.
lcDir = "/tmp".
DEFINE VARIABLE lcOSCommand AS CHARACTER NO-UNDO.
DEFINE VARIABLE lc_result AS CHARACTER NO-UNDO.
DEFINE VARIABLE lc_filename AS CHARACTER NO-UNDO.

DEFINE STREAM lsCmd.
/**************************************************/

lcOSCommand = "find " + lcDir + " '!' -type d -iname '*.p'" +
" -or -iname '*.w'" +
" -or -iname '*.i';".
INPUT-OUTPUT STREAM lsCmd THROUGH
VALUE (lcOSCommand) NO-ECHO UNBUFFERED.
REPEAT:
IMPORT STREAM lsCmd UNFORMATTED lc_result.
MESSAGE lc_result VIEW-AS ALERT-BOX.
END.
INPUT-OUTPUT STREAM lsCmd CLOSE.
 
else, here is a example code for webspeed, user for a recursive compilation,
this code can be translated into a 4GL procedure --> recursive search.

<script language="SpeedScript">
def var cDir as char initial ".":u no-undo.
def var cFileList as char initial "*.htm *.p *.w":u no-undo.
def var cXRef as char no-undo.
def var cSave as char no-undo.
def var cMinSize as char no-undo.
def var cSubDir as char no-undo.
def var cErrors as char no-undo.
def var cOptions as char no-undo.

def var i as int no-undo.
def var j as int no-undo.
def var iNumFiles as int no-undo.
def var iFlags as int no-undo.


function compileDir returns logical (input pcDir as char, input plSubDir as logical):
def var cFile as char extent 5 no-undo.
def var cDir as char no-undo.
def var cDirList as char no-undo.
def var cCompileFile as char no-undo.
def var cRCodeFile as char no-undo.
def var cXRefFile as char no-undo.
def var f_type as char no-undo.
def var f_list as char no-undo.

def var i as int no-undo.

def var l_e4gl as logical no-undo.

input from os-dir(pcDir).
repeat:
import cFile.

if (index(cFile[3], "D":u) > 0 and cFile[1] <> ".":u and cFile[1] <> "..":u) then
do:
assign cDirList = cDirList + cFile[2] + ",":u.
end.
else
do i = 1 to iNumFiles:
if (cFile[1] matches entry(i, cFileList) and index(cFile[3], "F":u) > 0) then
do:
RUN webtools/util/_filetyp.p (cFile[2], OUTPUT f_type, OUTPUT f_list).
assign l_e4gl = (LOOKUP("HTML":u, f_list) > 0).

IF LOOKUP("include":U, f_list) = 0 THEN
do:
{&out} '<li>' html-encode(cFile[2]) skip.
put {&WEBSTREAM} control null.

if (l_e4gl) then
do:
assign cCompileFile = entry(1, cFile[2], ".":u) + ".tmp":u.

/* Convert HTML files to SpeedScript. */
RUN webutil/e4gl-gen.p (cFile[2],
INPUT-OUTPUT cOptions,
INPUT-OUTPUT cCompileFile).
end.
else
do:
assign cCompileFile = cFile[2].
end.

assign
cRCodeFile = entry(1, cCompileFile, ".":u) + ".r":u
cXRefFile = entry(1, cCompileFile, ".":u) + ".xref":u.

case iFlags:
when 1 then compile value(cCompileFile) save no-error.
when 3 then compile value(cCompileFile) save min-size no-error.
when 4 then compile value(cCompileFile) xref value(cXRefFile) no-error.
when 5 then compile value(cCompileFile) save xref value(cXRefFile) no-error.
when 7 then compile value(cCompileFile) save min-size xref value(cXRefFile) no-error.
otherwise compile value(cCompileFile) no-error.
end case.

if (compiler:error) then
do:
{&out} '<ul>' skip.
do j = 1 to error-status:num-messages:
{&out}
'<li><font color="red" size=-1>'
html-encode(error-status:get-message(j))
'</font></li>' skip.
end.
{&out} '</ul>' skip.
put {&WEBSTREAM} control null.
end.
else
DO:
{&out} ' <a href="' cFile[1] '">RUN</a>' skip.
RCODE-INFO:FILE-NAME = cCompileFile.
{&out} 'Tables: ' + RCODE-INFO:TABLE-LIST + '<br>' SKIP.
END.

if (l_e4gl) then
do:
os-delete value(cCompileFile).
end.

{&out} '</li>' skip.
end.

/* If cFileList entry matched, skip rest */
assign i = iNumFiles.
end. /* if (cFile[1] matches... */
end. /* do i = 1 to iNumFiles */
end. /* repeat */
input close.

if (cDirList <> "":u and plSubDir) then do:
do i = 1 to num-entries(cDirList):
assign cDir = entry(i, cDirList).
if (cDir <> "":u) then
compileDir(entry(i, cDirList), plSubDir).
end.
end.
end function.

if (REQUEST_METHOD = "POST":u and get-value("btnOk":u) <> "":u) then
do:
assign
cDir = get-value("dir":u)
cFileList = get-value("file":u)
cXRef = get-value("xref":u)
cSave = get-value("save":u)
cSubDir = get-value("subdir":u)
cMinSize = get-value("minsize":u).

if cSave <> "":u then
assign iFlags = iFlags + 1.
if cMinSize <> "":u then
assign iFlags = iFlags + 2.
if cXRef <> "":u then
assign iFlags = iFlags + 4.
end.

assign file-info:filename = cDir.
if (file-info:full-pathname <> ? and index(file-info:file-type, "D":u) > 0) then
assign cDir = file-info:full-pathname.
else
assign cErrors = cErrors + "Invalid directory<br>":u.

if (cFileList = "":u) then
assign cErrors = cErrors + "Invalid file name<br>":u.

assign
cFileList = replace(cFileList, " ":u, ",":u)
cFileList = replace(cFileList, ";":u, ",":u).
</script>

<html>
<head>
<title>Application compiler</title>
</head>

<body bgcolor="silver">

<script language="SpeedScript">
if (WEB-CONTEXT:GET-CONFIG-VALUE("srvrAppMode":U) BEGINS "Development") then
do:
</script>

<form name="Comp" method="post">
<table align="center">
<th colspan=2 align="center">
<font color="blue" size=+1>WebSpeed Application Compiler</font>
</th>
<tr>
<th align="right">Directory:</th>
<td align="left"><input type="text" name="dir" value="` cDir `" size=40></td>
</tr>
<tr>
<th align="right">File:</th>
<td align="left"><input type="text" name="file" value="` cFileList `"></td>
</tr>
<tr>
<th align="right">Generate XRef:</th>
<td align="left"><input type="checkbox" name="xref" value="checked" ` cXRef `></td>
</tr>
<tr>
<th align="right">Save R-Code:</th>
<td align="left"><input type="checkbox" name="save" value="checked" ` cSave `></td>
</tr>
<tr>
<th align="right">Minimise R-Code Size:</th>
<td align="left"><input type="checkbox" name="minsize" value="checked" ` cMinSize `></td>
</tr>
<tr>
<th align="right">Search Sub-directories:</th>
<td align="left"><input type="checkbox" name="subdir" value="checked" ` cSubDir `></td>
</tr>
<tr>
<td colspan=2 align="center">
<input type="submit" name="btnOk" value="Compile">&nbsp;<input type="reset" name="btnReset" value="Reset">
</td>
</tr>

<script language="SpeedScript">
if (cErrors <> "":u) then
do:
</script>

<tr>
<td colspan=2 align="center"><font color="red">` cErrors `</font></td>
</tr>

<script language="SpeedScript">
end.
</script>

</table>
</form>

<script language="SpeedScript">
if (REQUEST_METHOD = "POST":u and get-value("btnOk":u) <> "":u and cErrors = "":u) then
do:
{&out} '<ul>' skip.

assign
iNumFiles = num-entries(cFileList)
cFileList = replace(cFileList, '.':u, chr(10))
cFileList = replace(cFileList, '?':u, '.':u)
cFileList = replace(cFileList, chr(10), '~~.':u).

compileDir(cDir, (cSubDir <> "":u)).

{&out}
'</ul>' skip.
end.
end. /* If DEVELOPMENT MODE */
else
do:
</script>

<center><font color="red" size=+1>WebSpeed Application Compiler not available in Production mode</font></center>

<script language="SpeedScript">
end.
</script>

</body>
</html>
 
Back
Top