plctwincatcodesysststructured-text

TwinCAT - How to check contents of a directory from Beckhoff PLC


I am trying to perform file management within the PLC. Currently, NT_StartProcess works as follows, but I will not have any feedback after the process has been spawned. Is there a way to check the contents of a directory from the PLC? Is there any way to get feedback from NT_StartProcess?

// File Locations
sTargetFilePath := 'C:\LocalHistory\test.job';
sTargetDirectory := 'C:\\CustomerDir';
 
// Build Command String
sCommand := '/C ';                  // Special command indicating command string input
sCommand := CONCAT(sCommand, 'move ');      // Add move command
sCommand := CONCAT(sCommand, sTargetFilePath);  // Add target file
sCommand := CONCAT(sCommand, ' ');          // Required space for command 
sCommand := CONCAT(sCommand, sTargetDirectory); // Add target location
 
// Output -> ‘/C move C:\NET-DRIVE\NewOrders\Original.xml 'C:\NET-DRIVE\OldOrders’
 
 
 
Process(
    NETID := '',                    // Local System
    PATHSTR := 'C:\Windows\System32\cmd.exe',   // Path to local cmd executable
    COMNDLINE := sCommand,          // Comnmand to be executed
    ERR => bError,                   // Error Output
    ERRID => iErrorId                // Error Id Output
);
 
// Trigger Command
IF bTrigger THEN
    bTrigger := FALSE;
    Process(START:=TRUE);
    Process(START:=FALSE);
END_IF

Solution

  • Yes, there is a way. What you are looking for are those two function blocks: FB_EnumFindFileList, FB_EnumFindFileEntry

    As for feedback from NT_StartProcess, you can't get it directly. There are two workaround I've been using:

    1. Write the results to a text file, and open this file using TwinCAT. In your case it would be something like this:
    ‘/C move C:\NET-DRIVE\NewOrders\Original.xml 'C:\NET-DRIVE\OldOrders && echo DONE > out.txt || echo FAIL > out.txt’
    

    Command after && will be executed only, if previous succeded. Command after || will be executed only if previous failed.
    > operator writes output of previous command to a file
    Example above should create a file out.txt and write DONE or FAIL inside. I don't have a PLC with TwinCAT with me at the moment, but it works in windows cmd.

    1. Write a program (In C# for example) that does what needs to be done and then connects to TwinCAT via ADS to put the result in variables