SysGetFileInfos, get file infos

List

This page is part of the IEC 61131-3 Programming Manual. Go to the index.

This function block from run in task Back, returns information about individual files or directories or about files or directories contained in a directory. Passing in PathNane the complete file or directory name of the path and activating Init, if the file or directory is present, the information is returned.

List all the files in a folder

By defining in PathName the name of a folder followed by the filter “/*.*”, activating  Init the file list is initialized and the first file found is returned. At every command Next a search for a new file is performed, if the output file is found Found it is activated for a loop and its name is returned to Name. Finished the list of all files present on command Next the output is no longer activated Found e Name is listed.

It is also possible to perform a filter on the files to be returned. For example, using “/*.htm” only files with the “htm” extension are returned. The exit IsDir it is activated if the name of the file returned is that of a subdirectory

Replaces SysDirListing

Replaces the function block SysDirListing, changing the type of input and output parameters from string to string pointer. The previous statement:

VAR
    DList : SysDirListing; (* Get file infos *)
END_VAR

DList.PathName:='C:/';
DList();
IF (DList.Found) THEN i:=SysVfprintf(Fp, ADR('%s'), VR_TYPE#STRING_TYPE, ADR(DList.Name)); END_IF;

Becomes:

VAR
    DList : SysGetFileInfos; (* Get file infos *)
END_VAR

DList.PathName:=ADR('C:/');
DList();
IF (DList.Found) THEN i:=SysVfprintf(Fp, ADR('%s'), VR_TYPE#STRING_TYPE, DList.Name); END_IF;
information circle

Function lock

CODESYS: Not available

LogicLab: eLLabXUnified12Lib

Description

Init (BOOL) Upon activation, the index of the files in the indicated directory is initialized and the name of the first file found is returned.
Next (BOOL) Upon activation, the name of the file pointed to by the index in the directory indicated is returned. The index is incremented by pointing to the next file.
PathName (@STRING) File name complete with path.
To detect the data of a specific file, define its path and name, for example C:\MyDir\MyFile.log.
To detect data from a directory, define its path, for example C:\MyDir\MyFile.log.
To list files in a directory, define the path with the example filter C:\MyDir\*.log.
Found (BOOL) Activates for a loop if on command Init o Next a file was found.
Fault (BOOL) Activates for a loop if execution error.
IsDir (BOOL) Active if the returned file name belongs to a subdirectory.
Size (UDINT) File size.
FTime (UDINT) Last modification date of the file in Epoch time (UTC).
Name (@STRING) File name including any extension

SysGetFileInfos FB image

Examples

How to use the examples.

ST_SysGetFileInfos: By connecting in telnet to the indicated port, the elend of the files and directories found in the path set is returned.

ST_DeleteOldestFiles: Activating the variable from debug Delete, the files in the defined directory are checked and the oldest ones are deleted to keep the number of total files equal to the value of Threshold.

Other examples are found in the function Sysremove.

LogicLab (Ptp116, ST_SysGetFileInfos)
PROGRAM ST_SysGetFileInfos
VAR
    i : UDINT; (* Auxiliary variable *)
    CaseNr : USINT; (* Program case *)
    FFound : USINT; (* Files found *)
    Fp : eFILEP; (* File pointer array *)
    DFound : USINT; (* Directories found *)
    LDTime : LDATETIMESTRUCT; (* Date/Time struct *)
    TCPServer : SysTCPServer; (* TCP server *)
    DList : SysGetFileInfos; (* Get file infos *)
END_VAR

// *****************************************************************************
// PROGRAM "ST_SysGetFileInfos"
// *****************************************************************************
// When a telnet client connect to port 1200 it's executed the listing of
// a directory.
// -----------------------------------------------------------------------------

    // -------------------------------------------------------------------------
    // INITIALIZATION
    // -------------------------------------------------------------------------
    // Program initializations.

    IF (SysFirstLoop) THEN

        // TCPServer initialization.

        TCPServer.FilesArr:=ADR(Fp); //Files array
        TCPServer.LocalAdd:=ADR('0.0.0.0'); //Local address
        TCPServer.LocalPort:=1200; //Local port 
        TCPServer.MaxConn:=1; //Accepted connections
        TCPServer.FlushTm:=50; //Flush time (mS)
        TCPServer.LifeTm:=30; //Life time (S)
        TCPServer.RxSize:=128; //Rx buffer size
        TCPServer.TxSize:=128; //Tx buffer size

        // Directory listing initialization.

        DList.PathName:=ADR('/tmp/*.*'); //Directory to list    
        DList.PathName:=ADR('C:/*.*'); //Directory to list    
    END_IF;

    // -------------------------------------------------------------------------
    // TCP SERVER MANAGEMENT
    // -------------------------------------------------------------------------
    // Manage the TCP server and directory listing.

    DList(); //Directory listing
    TCPServer(); //TCPServer management
    TCPServer.Enable:=TRUE; //TCPServer enable

    // Check if a client has been connected.

    IF NOT(SysFIsOpen(Fp)) THEN CaseNr:=0; RETURN; END_IF;

    // -------------------------------------------------------------------------
    // PROGRAM CASES
    // -------------------------------------------------------------------------
    // Program case management.

    CASE (CaseNr) OF

        // ---------------------------------------------------------------------
        // Init listing.

        0:
        DFound:=0; //Directories found
        FFound:=0; //Files found
        DList.Init:=TRUE; //Init listing
        i:=SysVfprintf(Fp, ADR('%s$r$n$r$n'), VR_TYPE#STRING_TYPE, ADR('"SysDirListing" example program'));
        CaseNr:=10; //Program case        

        // ---------------------------------------------------------------------
        // Init listing.

        1:
        DList.Next:=TRUE; //Next command
        CaseNr:=10; //Program case        

        // ---------------------------------------------------------------------
        // Reset commands.

        10:
        DList.Init:=FALSE; //Init listing
        DList.Next:=FALSE; //Next command

        // If file found returns file data.

        IF (DList.Found) THEN

            // Returns Date/Time.

            i:=SPLIT_DT(TO_DATE_AND_TIME(DList.FTime), ADR(LDTime.Year), ADR(LDTime.Month), ADR(LDTime.Day), ADR(LDTime.Hours), ADR(LDTime.Minutes), ADR(LDTime.Seconds));
            i:=SysVfprintf(Fp, ADR('%02d/'), VR_TYPE#UINT_TYPE, ADR(LDTime.Day));
            i:=SysVfprintf(Fp, ADR('%02d/'), VR_TYPE#UINT_TYPE, ADR(LDTime.Month));
            i:=SysVfprintf(Fp, ADR('%04d'), VR_TYPE#UINT_TYPE, ADR(LDTime.Year));

            i:=SysVfprintf(Fp, ADR(' %02d:'), VR_TYPE#UINT_TYPE, ADR(LDTime.Hours));
            i:=SysVfprintf(Fp, ADR('%02d'), VR_TYPE#UINT_TYPE, ADR(LDTime.Minutes));

            // Returns directory label or file length.

            IF (DList.IsDir) THEN
                DFound:=DFound+1; //Directories found
                i:=SysVfprintf(Fp, ADR(' %10s'), VR_TYPE#STRING_TYPE, ADR('[DIR]'));
            ELSE
                FFound:=FFound+1; //Files found
                i:=SysVfprintf(Fp, ADR(' %10d'), VR_TYPE#UDINT_TYPE, ADR(DList.Size));
            END_IF;

            // Returns file name.

            i:=SysVfprintf(Fp, ADR(' %s$r$n'), VR_TYPE#STRING_TYPE, DList.Name);
            CaseNr:=20; //Program case
            RETURN;        
        END_IF;

        // All files in the directory have been listed.        

        i:=SysVfprintf(Fp, ADR('%16d Files$r$n'), VR_TYPE#USINT_TYPE, ADR(FFound));
        i:=SysVfprintf(Fp, ADR('%16d Directory$r$n'), VR_TYPE#USINT_TYPE, ADR(DFound));
        CaseNr:=30; //Program case

        // ---------------------------------------------------------------------
        // Wait until the output buffer is empty.

        20, 30:
        IF (TO_UDINT(SysFGetOSpace(Fp)) <> SysFGetOBfSize(Fp)) THEN RETURN; END_IF;
        IF (CaseNr = 30) THEN i:=Sysfclose(Fp); END_IF; //Close socket
        CaseNr:=1; //Program case        
    END_CASE;

// [End of file]
LogicLab (Ptp116, ST_DeleteOldestFiles)
PROGRAM ST_DeleteOldestFiles
VAR
    CaseNr : USINT; (* Program case *)
    FFound : USINT; (* Files found *)
    i : UDINT; (* Auxiliary variable *)
    DList : SysGetFileInfos; (* Directory listing *)
    Delete : BOOL; (* Delete files command *)
    FDate : UDINT; (* File date (Epoch) *)
    FName : STRING[ 64 ]; (* File name *)
    Threshold : USINT; (* File number threshold *)
    Dir : STRING[ 32 ]; (* Directory name *)
    DirWithFilter : STRING[ 32 ]; (* Directory name with filter *)
END_VAR

// *****************************************************************************
// PROGRAM "ST_DeleteOldestFiles"
// *****************************************************************************
// If set by debug the "Delete" variable the program maintains a defined number
// of files into a directory. If there are more than defined the oldest are
// deleted.
// -----------------------------------------------------------------------------

    // -------------------------------------------------------------------------
    // PROGRAM INITIALIZATION
    // -------------------------------------------------------------------------
    // Execute initializations.

    IF (SysFirstLoop) THEN
        Threshold:=2; //File number threshold
        Dir:='C:/MyDir'; //Directory to list
        DirWithFilter:=CONCAT(Dir, '/*.*'); //Directory to list with filter to get all its files
        DList.PathName:=ADR(DirWithFilter); //Directory to list
    END_IF;

    // -------------------------------------------------------------------------
    // EXECUTE THE CONTROL
    // -------------------------------------------------------------------------
    // Check if command set.

    IF NOT(Delete) THEN
        CaseNr:=0; //Program case
    ELSE

        // Manage the directory listing.

        DList(); //Directory listing
        DList.Init:=FALSE; //Init listing
        DList.Next:=FALSE; //Next command

        // ---------------------------------------------------------------------
        // PROGRAM CASES
        // ---------------------------------------------------------------------
        // Program case management.

        CASE (CaseNr) OF

            // -----------------------------------------------------------------
            // Init directory listing.

            0:
            FFound:=0; //Files found
            FDate:=SysDateTime; //File date (Epoch)
            DList.Init:=TRUE; //Init listing
            CaseNr:=CaseNr+1; //Program case

            // -----------------------------------------------------------------
            // If file found returns file data.

            1:
            IF NOT(DList.Found) THEN CaseNr:=CaseNr+1; RETURN; END_IF;
            FFound:=FFound+1; //Files found
            DList.Next:=TRUE; //Next command

            // Check if file date is older previous.

            IF (DList.Time > FDate) THEN RETURN; END_IF;
            FDate:=DList.Time; //File date (Epoch)
            i:=SysVsnprintf(ADR(FName), SIZEOF(FName), ADR('%s'), STRING_TYPE, ADR(Dir));
            i:=SysCVsnprintf(ADR(FName), SIZEOF(FName), ADR('/%s'), STRING_TYPE, DList.Name);

            // -----------------------------------------------------------------
            // If number of files in directory is greater than the threshold,
            // the oldest is deleted.

            2:
            IF (FFound <= Threshold) THEN Delete:=FALSE; RETURN; END_IF;
            i:=SysFileRemove(ADR(FName)); //Delete file
            CaseNr:=0; //Program case
        END_CASE;
    END_IF;

// [End of file]
Was this article helpful?