Example: File search (FB_EnumFindFileEntry, FB_EnumFindFileList)

Here you can unpack the complete sources: Sample02.zip

 

Example  FB_EnumFindFileEntry:

In the local TwinCAT system all files should be listed in the following directory: C:\Windows\system32\. The file names should be written as messages into the TwinCAT System Manager logger output. It should be possible to cancel this process.

PROGRAM P_TestEnumEntry
VAR
    fbEnum : FB_EnumFindFileEntry := ( sNetID := '', tTimeout := T#5s, sPathName := 'C:\Windows\System32\*.*' );
    bEnum : BOOL;
    bAbort : BOOL;
    nState : BYTE;
END_VAR

The listing of the found files begins with a rising edge at the bEnum variable. The process is canceled with a rising edge at the bAbort variable.

CASE nState OF
0:
    IF bEnum THEN (* flag set ? *)
     bEnum := FALSE; (* reset flag *)
        fbEnum.eCmd := eEnumCmd_First; (* enum first entry *)
        nState := 1;
    END_IF

1: (* enum one entry *)
 IF bAbort THEN
        bAbort := FALSE;
        fbEnum.eCmd := eEnumCmd_Abort;
    END_IF
    fbEnum( bExecute := FALSE );
    fbEnum( bExecute := TRUE );
    nState := 2;

2: (* wait until function block not busy *)
 fbEnum( bExecute := FALSE );
    IF NOT fbEnum.bBusy THEN
        IF NOT fbEnum.bError THEN
            IF NOT fbEnum.bEOE THEN
                ADSLOGSTR( ADSLOG_MSGTYPE_HINT OR ADSLOG_MSGTYPE_LOG, 'FB_EnumFindFileEntry, find file name: %s', fbEnum.stFindFile.sFileName );
                fbEnum.eCmd := eEnumCmd_Next; (* enum next entry *)
             nState := 1;
            ELSE (* no more entries *)
             nState := 0;
            END_IF
        ELSE (* log error *)
         ADSLOGSTR( ADSLOG_MSGTYPE_ERROR OR ADSLOG_MSGTYPE_LOG, 'FB_EnumFindFileEntry error:%s', DWORD_TO_HEXSTR( fbEnum.nErrID, 0, FALSE ) );
            nState := 0;
        END_IF
    END_IF
END_CASE

 

Log messages in the TwinCAT System Manager logger output:

Example: File search (FB_EnumFindFileEntry, FB_EnumFindFileList) 1:

 

Example FB_EnumFindFileList:

PROGRAM P_TestEnumList
VAR
    fbEnum      : FB_EnumFindFileList;
    filesList       : ARRAY[1..10] OF ST_FindFileEntry;
    bFirst      : BOOL;
    bNext       : BOOL;
    bAbort      : BOOL;
    bBusy       : BOOL;
    bError      : BOOL;
    nErrID      : UDINT;
    bEOE        : BOOL;
    nFindFiles      : UDINT;
END_VAR

Online view:

Example: File search (FB_EnumFindFileEntry, FB_EnumFindFileList) 2: