Question How do i know its a valid directory and how i can know the handle belongs to a temp table

METHOD PUBLIC VOID loadFilesFromDir(INPUT cipDir AS CHARACTER , INPUT iphbufhandle AS HANDLE ):

DEFINE VARIABLE cFile AS CHARACTER NO-UNDO.
DEFINE VARIABLE cFilePath AS CHARACTER NO-UNDO.
DEFINE VARIABLE cFormat AS CHARACTER NO-UNDO.

INPUT FROM OS-DIR (cipDir).

IF OS-ERROR = 0 THEN
DO:

REPEAT :
IMPORT cFile cFilePath.

IF cFile <> "." AND cFile <> ".." THEN
DO:
iphbufhandle:BUFFER-CREATE ().

iphbufhandle::ttFile =cFilePath.
END.
END.
END.
ELSE
MESSAGE "eror at os dir"
VIEW-AS ALERT-BOX.
INPUT CLOSE .

RETURN.

CATCH e AS Progress.Lang.Error:

UNDO, THROW NEW Progress.Lang.AppError(e:GetMessage(1),1).

END CATCH.



END METHOD.

It is a method in my class. Here i want to ensure several things
1. The input directory is valid or not
2. The handle I'm getting temp table handle or not ( i'm passing temp-table:default-buffer-handle from method calling )
3.i want to know which type of file i'm going to import
4.The temp table may have or may not have the ttFile as field how do i can handle it
I'm a beginner help me in a way i can understand the things.
 
FILE-INFO:FILE-NAME = cipDir .
IF FILE-INFO:FULL-PATHNAME <> ?
AND INDEX(FILE-INFO:FILE-TYPE, "D") > 0
THEN //The input directory is valid
Else // The input directory is NOT valid
 

Osborne

Active Member
For the other answers something along these lines - rough example only to show what you can check for:

Code:
DEFINE VARIABLE hTT AS HANDLE NO-UNDO.
DEFINE VARIABLE hTB AS HANDLE NO-UNDO.

CREATE TEMP-TABLE hTT.
hTT:ADD-NEW-FIELD("f1","character",0,?,?,"Field 1","Field 1").
hTT:ADD-NEW-FIELD("f2","integer",0,?,?,"Field 2","Field 2").
hTT:ADD-NEW-FIELD("f3","character",0,?,?,"Field 3","Field 3").
hTT:ADD-NEW-FIELD("ttFile","character",0,?,?,"Field 3","Field 3").
hTT:TEMP-TABLE-PREPARE("tt1").
hTB = hTT:DEFAULT-BUFFER-HANDLE.

MyClass:loadFilesFromDir(INPUT "C:\Temp", INPUT hTT).

METHOD PUBLIC VOID loadFilesFromDir(INPUT cipDir AS CHARACTER , INPUT iphbufhandle AS HANDLE):
   DEFINE VARIABLE hBuffer AS HANDLE NO-UNDO.
   DEFINE VARIABLE hField AS HANDLE NO-UNDO.
   DEFINE VARIABLE iCount AS INTEGER NO-UNDO.
   DEFINE VARIABLE lExists AS LOGICAL NO-UNDO.

   // 2 - Ensure handle passed is a temp-table
   IF iphbufhandle:TYPE <> "TEMP-TABLE" THEN
      RETURN.
   // Note: if you pass temp-table:default-buffer-handle instead the TYPE will be BUFFER.

   IF iphbufhandle:TYPE = "TEMP-TABLE" THEN
      hBuffer = iphbufhandle:DEFAULT-BUFFER-HANDLE.
   ELSE
      hBuffer = iphbufhandle.

   // 4 - Check if the field exists
   hField = hBuffer:BUFFER-FIELD("ttFile") NO-ERROR.
   IF VALID-HANDLE(hField) THEN
      lExists = TRUE.
   // or
   DO iCount = 1 TO hBuffer:NUM-FIELDS:
      hField = hBuffer:BUFFER-FIELD(iCount).
      IF hField:NAME = "ttFile" THEN DO:
         lExists = TRUE.
         LEAVE.
      END.
   END.

   IF NOT lExists THEN
      RETURN.

   // 3 - Type of file.
   MESSAGE hField:BUFFER-VALUE() VIEW-AS ALERT-BOX.
END METHOD.
 
For the other answers something along these lines - rough example only to show what you can check for:

Code:
DEFINE VARIABLE hTT AS HANDLE NO-UNDO.
DEFINE VARIABLE hTB AS HANDLE NO-UNDO.

CREATE TEMP-TABLE hTT.
hTT:ADD-NEW-FIELD("f1","character",0,?,?,"Field 1","Field 1").
hTT:ADD-NEW-FIELD("f2","integer",0,?,?,"Field 2","Field 2").
hTT:ADD-NEW-FIELD("f3","character",0,?,?,"Field 3","Field 3").
hTT:ADD-NEW-FIELD("ttFile","character",0,?,?,"Field 3","Field 3").
hTT:TEMP-TABLE-PREPARE("tt1").
hTB = hTT:DEFAULT-BUFFER-HANDLE.

MyClass:loadFilesFromDir(INPUT "C:\Temp", INPUT hTT).

METHOD PUBLIC VOID loadFilesFromDir(INPUT cipDir AS CHARACTER , INPUT iphbufhandle AS HANDLE):
   DEFINE VARIABLE hBuffer AS HANDLE NO-UNDO.
   DEFINE VARIABLE hField AS HANDLE NO-UNDO.
   DEFINE VARIABLE iCount AS INTEGER NO-UNDO.
   DEFINE VARIABLE lExists AS LOGICAL NO-UNDO.

   // 2 - Ensure handle passed is a temp-table
   IF iphbufhandle:TYPE <> "TEMP-TABLE" THEN
      RETURN.
   // Note: if you pass temp-table:default-buffer-handle instead the TYPE will be BUFFER.

   IF iphbufhandle:TYPE = "TEMP-TABLE" THEN
      hBuffer = iphbufhandle:DEFAULT-BUFFER-HANDLE.
   ELSE
      hBuffer = iphbufhandle.

   // 4 - Check if the field exists
   hField = hBuffer:BUFFER-FIELD("ttFile") NO-ERROR.
   IF VALID-HANDLE(hField) THEN
      lExists = TRUE.
   // or
   DO iCount = 1 TO hBuffer:NUM-FIELDS:
      hField = hBuffer:BUFFER-FIELD(iCount).
      IF hField:NAME = "ttFile" THEN DO:
         lExists = TRUE.
         LEAVE.
      END.
   END.

   IF NOT lExists THEN
      RETURN.

   // 3 - Type of file.
   MESSAGE hField:BUFFER-VALUE() VIEW-AS ALERT-BOX.
END METHOD.
Thank you @Osborne , got what I want. I will go and try thank you.
 

edujs

New Member
You can also use System.IO.Directory:Exists('C:\..') to check if the directory is valid
 
Last edited:

RaphaelFrei

New Member
I know this post is kinda old, but searching thru ways to list an directory and all sub directories I've created this function that fills a temp-table:

List the tt-dir after running the FillTempTableDir(INPUT cRootDir) and get a list with all subdirs. (including the root)

C#:
/* ******************************************************************** *
*                                                                       *
*                           Raphael Frei - 2024                         *
*                                                                       *
*                  Get List with all subdir from folder                 *
*                                                                       *
* ********************************************************************* */

/* ----- TEMP-TABLE ----- */
DEFINE TEMP-TABLE tt-dir
        FIELD tt-dir  AS CHARACTER
        FIELD tt-used AS LOGICAL.

/* ----- MAIN-BLOCK ----- */

DO:
    RUN FillTempTableDir(INPUT "J:\src").

    MESSAGE "Search complete."
        VIEW-AS ALERT-BOX INFORMATION BUTTONS OK.

    FOR EACH tt-dir NO-LOCK:
        MESSAGE tt-dir.tt-dir
            VIEW-AS ALERT-BOX INFORMATION BUTTONS OK.
    END.
END.

/* ----- PROCEDURES ----- */

PROCEDURE FillTempTableDir:
    DEFINE INPUT PARAMETER cFileRootDir AS CHARACTER NO-UNDO.
    
    FILE-INFO:FILE-NAME = cFileRootDir.

    IF FILE-INFO:FILE-TYPE <> "DRW" OR FILE-INFO:FULL-PATHNAME EQ ? THEN DO:
        MESSAGE "The informed directory does not exists - '" cFileRootDir "'."
            VIEW-AS ALERT-BOX INFORMATION BUTTONS OK.

        RETURN.
    END.

    DEFINE VARIABLE cCommand AS CHARACTER NO-UNDO.
    DEFINE VARIABLE cPath    AS CHARACTER NO-UNDO.

    FIND FIRST tt-dir WHERE tt-dir = cFileRootDir NO-LOCK NO-ERROR.
    
    IF NOT AVAIL tt-dir THEN DO:

        CREATE tt-dir.
        ASSIGN tt-dir.tt-dir  = cFileRootDir
               tt-dir.tt-used = NO.

    END.
    
    ASSIGN cCommand = "CMD.EXE /C DIR ~"" + cFileRootDir + "~" /A:D /B".

    INPUT THROUGH VALUE(cCommand) NO-ECHO.
    
    REPEAT:
        IMPORT UNFORMATTED cPath.
    
        RUN FillTempTableDir(INPUT STRING(cFileRootDir + "\" + cPath)).
    END.

END PROCEDURE.
 
Top