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:
Top