Codesys - Read the folder names found in a specific directory.
Thanks Yosi and Danatomega
Both your answers put me on the right trail and I have solved my issue.
However, I noticed that with the version 3.5.4 of Codesys and my version of libraries that the "
Structure DIRECTORY_INFO" is slightly different.
In Codesys 3.5.4, Structure DIRECTORY_INFO uses the flags diDirectory (instead of bDirectory) ,tDirFileName and ulSize.
So to filter on "Folder/Directories", DiDirectory must be a Hex2 (16#2) ...otherwise diDirectory for Files will be Hex0 (16#0)
I have attached a picture of the DirInfoFlags definition (top picture) and the Codesys code using diDirectory (bottom picture).
Thanks again for your help!
J
Code for looping to get folder names:
VAR
sDirName : STRING;
sFirstFileName : STRING;
sSecondFileName : STRING;
sThirdFileName : STRING;
iFilesCount : INT;
sDirEntry : STRING;
stDirInfo : DirInfo;
udiResult : __XWORD;
udiDirHandle : RTS_IEC_HANDLE;
udiFileHandle : RTS_IEC_HANDLE;
udiReadResult : UDINT;
xStartTest : BOOL:= FALSE; (* true, if a button is pressed *)
xTestDone : BOOL:= FALSE;
xError : BOOL;
ErrorCode : RTS_IEC_RESULT;
udiCreateError: UDINT;
udiReadError: UDINT;
udiCloseError: UDINT;
udiRenameError: UDINT;
udiDeleteError1: UDINT;
udiDeleteError2: UDINT;
udiDirDeleteError: UDINT;
programnames: ARRAY[1..32] OF String;
programcount: int;
END_VAR
(* This program uses the synchronous functions for directory modification*)
IF xStartTest THEN
(* create the complete file pathes*)
(* The file names can be adapted *)
sDirName := '/SystemDirExample';
sFirstFileName := '/firstTest.txt';
sSecondFileName := '/secondTest.txt';
sThirdFileName := '/thirdTest.txt';
xError := FALSE;
xTestDone := FALSE;
sDirName := CONCAT(g_sMainDirectory, sDirName);
sFirstFileName := CONCAT(sDirName, sFirstFileName);
sSecondFileName := CONCAT(sDirName, sSecondFileName);
sThirdFileName := CONCAT(sDirName, sThirdFileName);
(* Clean up existing example files*)
(* Delete existing example files *)
//udiDeleteError1:=SysFileDelete(szFileName:=sFirstFileName );
//udiDeleteError2:=SysFileDelete(szFileName:=sThirdFileName );
(* delete the directory *)
//udiDirDeleteError:= SysDirDelete(szDir:= sDirName);
(* create a new directory *)
udiCreateError := SysDirCreate(szDir:= sDirName);
(* two files are created and stored into the directory*)
udiFileHandle := SysFileOpen(szFile:=sFirstFileName, am:=AM_WRITE_PLUS, pResult:=ADR(ErrorCode));
IF udiFileHandle <> RTS_INVALID_HANDLE THEN
SysFileClose(hFile:=udiFileHandle);
END_IF
udiFileHandle := SysFileOpen(szFile:=sSecondFileName, am:=AM_WRITE_PLUS, pResult:=ADR(ErrorCode));
IF udiFileHandle <> RTS_INVALID_HANDLE THEN
SysFileClose(hFile:=udiFileHandle);
END_IF
(* open the directory
the handle is returned and the first directory entry. Normally the first entry is "." and the second ".."*)
udiDirHandle := SysDirOpen(szDir:= sDirName, szDirEntry:= sDirEntry, diMaxDirEntry:= 1024,
pDirInfo:= ADR(stDirInfo), pResult:= udiResult);
(* Iterate through all direcotry entries and count them. The implicit entries with points are not count.
when the last entry is reached the SysDirRead always delivers the last entry.*)
iFilesCount := 0;
programcount := 1;
REPEAT
IF FIND(sDirEntry, '.') <> 1 THEN
iFilesCount := iFilesCount +1;
END_IF
udiReadError:= SysDirRead(hDir:= udiDirHandle, szDirEntry:= sDirEntry, diMaxDirEntry:= 1024, pDirInfo:=ADR(stDirInfo) );
// SysDirRead is used to Directory Info
// Here, Im using SysDirRead to Find Folder Names within a Directory
// If stDirInfo.diDirectory is Hex 2 , then the Directory is a folder, otherwise Hex 0 is a File
IF stDirInfo.diDirectory = 16#2 AND Find(sDirEntry,'.') <> 1 THEN
programnames[programcount]:=sDirEntry;
programcount:=programcount+1;
END_IF
UNTIL
udiReadError = CmpErrors.Errors.ERR_END_OF_OBJECT
END_REPEAT
(* The two created files must be read.*)
//IF iFilesCount <> 2 THEN
// xError := TRUE;
//END_IF
(* Close the directory *)
udiCloseError := SysDirClose(hDir:= udiDirHandle);
(* Rename a file *)
udiRenameError:=SysFileRename(szOldFileName:= sSecondFileName, szNewFileName:= sThirdFileName);
(* the test is set to done, if no error occured *)
IF udiCreateError = 0 AND udiCloseError = 0 AND udiRenameError = 0 AND xError = FALSE THEN
xTestDone := TRUE;
ELSE
xError := TRUE;
END_IF
xStartTest := FALSE;
END_IF