File – Properties (EXAMPLE)

(* example of how to use the CAA_FILE.library - file property function blocks *)

PROGRAM FILE.PROP_PRG

VAR

                xFilePropInit:                           BOOL:=FALSE;

                uiFilePropState:                       UINT:=0;

                sFileName:                              CAA.FILENAME:= 'TestFile.txt';

                hFile:                                       CAA.HANDLE;

                szFileSize:                              CAA.SIZE := 0;

                dtLastFileMod:                        DT;

                sLastFileModification:             STRING:='';

                filop:                                       FILE.Open;

                filcl:                                         FILE.Close;

                filsa:                                       FILE.SetAttribute;

                filgs:                                       FILE.GetSize;

                filgt:                                        FILE.GetTime;

END_VAR

 

IF NOT xFilePropInit THEN

                filop(xExecute:=FALSE);

                filcl(xExecute:=FALSE);

                filsa(xExecute:=FALSE);

                filgs(xExecute:=FALSE);

                filgt(xExecute:=FALSE);

                xFilePropInit:=TRUE;

                uiFilePropState:=0;

ELSE

                CASE uiFilePropState OF

                0: (* get file size *)

                               filgs.sFileName:=sFileName;

                               filgs( xExecute:=TRUE);

                               IF filgs.xDone THEN

                                               szFileSize:=filgs.szSize;

                                               uiFilePropState:=1;

                               END_IF

                               IF filgs.xError THEN

                                               (* error handling*)

                                               ;

                               END_IF

                1: (* get file date of last modification *)

                               filgt.sFileName:=sFileName;

                               filgt( xExecute:=TRUE);

                               IF filgt.xDone THEN

                                               dtLastFileMod:=filgt.dtLastModification;

                                               sLastFileModification:=DT_TO_STRING(dtLastFileMod);

                                               uiFilePropState:=2;

                               END_IF

                               IF filgt.xError THEN

                                               (* error handling*)

                                               ;

                               END_IF

                2: (* open file *)

                               filop.sFileName:=sFileName;

                               filop.eFileMode:=FILE.MRDWR;

                               filop.xExclusive:=TRUE;

                               filop( xExecute:=TRUE);

                               IF filop.xDone THEN

                                               hFile:=filop.hFile;

                                               uiFilePropState:=3;

                               END_IF

                               IF filop.xError THEN

                                               (* error handling*)

                                               ;

                               END_IF

 

                3:(* set file attribute - read only *)

                               filsa.hFile:=hFile;

                               filsa.eFileAttrib:=FILE.READONLY;

                               filsa( xExecute:=TRUE);

                               IF filsa.xDone THEN

                                               uiFilePropState:=4;

                               END_IF

                               IF filsa.xError THEN

                                               (* error handling*)

                                               ;

                               END_IF

 

                4:(* close file *)

                               filcl.hFile:=hFile;

                               filcl( xExecute:=TRUE);

                               IF filcl.xDone THEN

                                               uiFilePropState:=5;

                               END_IF

                               IF filcl.xError THEN

                                               (* error handling*)

                                               ;

                               END_IF

                5:(* end of example*)

                               ;

                END_CASE

END_IF