Check if a directory exists and then copy a file in it

alex87
2016-06-16
2016-06-25
  • alex87 - 2016-06-16

    I would like to know a simple method how to check if a directory exists and then copy several files in it.
    More detailed explanation of the problem would be like this: I need to know if user connects a usb key into plc and then copy some files, that PLC made, on to the usb key.

    I was trying to do it with FILE.DirOpen, if it returns error directory doesn't exist and vice versa.
    The code follows and it doesn't work:

    VARS
    FileCheckUsb    : File.DirOpen;
    FileCheckUsb(xExecute:= TRUE, sDirName:= strUsbKeyDirectory);
    IF FileCheckUsb.xDone = TRUE THEN
       xUsbConnected := TRUE;
       FileCheckUsb(xExecute:= FALSE);
    ELSIF FileCheckUsb.xError = TRUE THEN
       xUsbConnected := FALSE;
       FileCheckUsb(xExecute:= FALSE);
    END_IF
    

    And here is also code for copying the file from HardDisk to UsbKey. Doesn't work either.

    VARS
    FileCopy      : File.Copy;
    IF xTransferToUsb THEN
       sFileNameCopy1 := CONCAT(g_sMainDirectory,'FooBar.Recipes.txtrecipe');
       sFileNameCopy2 := CONCAT(strUsbKeyDirectory,'FooBar.Recipes.txtrecipe');
       
       FileCopy(xExecute:= TRUE, sFileNameSource:= sFileNameCopy1, sFileNameDest:= sFileNameCopy2, xOverWrite:= TRUE );
       IF FileCopy.xDone = TRUE THEN
          FileCopy(xExecute:= FALSE);
       ELSIF FileClose.xError = TRUE THEN
          FileCopy(xExecute:= FALSE);
       END_IF
       
       xTransferToUsb := FALSE;
    END_IF
    

    The code is called inside the RECIPE_PRG.

    My PLC is JanzTec em[VIEW] 12 with WindowsCE as an operating system.

    All your help would be much appreciated.

     
  • rickj - 2016-06-25

    I think you need to setup a state machine using case statements. File system operations span multiple executions of you task. So when you trigger an operation by toggling an execute bit you have to advance to the next state where you wait for either the Done or the Error bit to be set. I would look something like the following

    VARS
    FileCopy   : File.Copy;
    Step      : INT;
    IF xTransferToUsb THEN
       sFileNameCopy1 := CONCAT(g_sMainDirectory,'FooBar.Recipes.txtrecipe');
       sFileNameCopy2 := CONCAT(strUsbKeyDirectory,'FooBar.Recipes.txtrecipe');
       xTransferToUsb := FALSE;
       Step := 01;
    END_IF
       
     CASE Step OF
       00: // Wait for trigger
        
       01: // Initiate file copy 
          FileCopy(
             xExecute:=TRUE, 
             sFileNameSource:=sFileNameCopy1, 
             sFileNameDest:=sFileNameCopy2, 
             xOverWrite:=TRUE
          );
          Step := 02;
          
       02: // Wait for operation to complete
          IF FileCopy.xDone THEN
             Step := 03;
          ELSIF FileClose.xError THEN
             Step := 04;
          END_IF
       
       03: // Operation completed successfully
          // Do something
          Step := 05;
          
       04: // Operation failed
          // Do something else
          Step := 05;
       05: // Reset operation
          FileCopy(xExecute:= FALSE);
          Step := 00;
     END_CASE
     
    
     

Log in to post a comment.