Directory Example

Navigation:  Libraries > CT File Access > Examples >

Directory Example

Previous pageReturn to chapter overviewNext page

(* example of how to use the directory function blocks *)

PROGRAM DIR_PRG

VAR

  xDirInit:       BOOL := FALSE;

  uiDirState:     UINT := 0;

  sDirNewName:    STRING:='./TestDirectory';

  sDirNextName:   STRING:='./NewDirectory';

  hDir:           CAA.HANDLE;

  deNewDirectory: FILE.FILE_DIR_ENTRY;

  fildcr:         FILE.DirCreate;

  fildop:         FILE.DirOpen;

  fildcl:         FILE.DirClose;

  fildls:         FILE.DirList;

  fildrn:         FILE.DirRename;

  fildrm:         FILE.DirRemove;

END_VAR

 

IF NOT xDirInit THEN

  fildcr(xExecute:=FALSE);

  fildcl(xExecute:=FALSE);

  fildls(xExecute:=FALSE);

  fildrm(xExecute:=FALSE);

  xDirInit:=TRUE;

  uiDirState:=0;

 

  CASE uiDirState OF

  0: (* create a new directory *)

  fildcr.sDirName:=sDirNewName;

  fildcr.xParent:=FALSE;

  fildcr( xExecute:=TRUE);

    IF fildcr.xDone THEN

       uiDirState:=1;

    END_IF

    IF fildcr.xError THEN

    (* error handling*)

       ;

    END_IF

  1:(* open directory *)

  fildop.sDirName:=sDirNewName;

  fildop( xExecute:=TRUE);

    IF fildop.xDone THEN

       hDir := fildop.hDir;

       uiDirState:=2;

    END_IF

    IF fildop.xError THEN

    (* error handling*)

       ;

    END_IF

  2:(* get directory property list *)

    fildls.hDir:=hDir;

    fildls( xExecute:=TRUE);

    IF fildls.xDone THEN

       deNewDirectory.sEntry :=fildls.deDirEntry.sEntry;

       deNewDirectory.szSize :=fildls.deDirEntry.szSize;

       deNewDirectory.xDirectory :=fildls.deDirEntry.xDirectory;

       deNewDirectory.xExclusive :=fildls.deDirEntry.xExclusive;

       deNewDirectory.dtLastModification :=fildls.deDirEntry.dtLastModification;

       uiDirState:=3;

    END_IF

    IF fildop.xError THEN

    (* error handling*)

       ;

    END_IF

  3:(* close directory *)

    fildcl.hDir:=hDir;

    fildcl( xExecute:=TRUE);

    IF fildcl.xDone THEN

       uiDirState:=4;

    END_IF

    IF fildcl.xError THEN

    (* error handling*)

       ;

    END_IF

  4:(* rename directory*)

    fildrn.sDirNameOld:=sDirNewName;

    fildrn.sDirNameNew:=sDirNextName;

    fildrn( xExecute:=TRUE);

    IF fildrn.xDone THEN

       uiDirState:=5;

    END_IF

    IF fildrn.xError THEN

    (* error handling*)

       ;

    END_IF

  5:(* remove directory*)

    fildrm.sDirName:=sDirNextName;

    fildrm.udiTimeOut:=100000;    (* 100ms TimeOut *)

    fildrm.xRecursive:=FALSE;

    fildrm( xExecute:=TRUE);

    IF fildrm.xDone THEN

       uiDirState:=6;

    END_IF

    IF fildrm.xError THEN

    (* error handling*)

       ;

    END_IF

  6: (* end of example*)

       ;

    END_CASE

END_IF