Codesys V3.5.4

EnterDownload

Member
Join Date
May 2015
Location
NY
Posts
12
Hello All

I have a PLC and Im trying to read the folder names found in a specific directory.

I know how to only Open a directory, but can someone show me code on how to read the folder names in a directory?

Im using SysDir tools to Open the Directory.


thanks


JL
 
Yosi

Thanks for the quick response, BUT how do I distinguish between file names and folder names using SysDirOpen?

For example, the directory I'm using may have BOTH folder names and filenames, but I want only the folder names. How do I folder on folder names only?

thanks

J
 
If you read the document that was linked to, on bottom of page 1 is the note (on the function SysDirRead) :

"The information on the particular entries will be written to structure DIRECTORY_INFO"

then, on page 2, it says further that in the structure DIRECTORY_INFO, one of the fields is 'bDirectory' which will be TRUE if the entry is a directory, FALSE if the entry is a file.
 
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

SysDirectory_Pic.jpg
 
PS. To test the code I manually entered arbitrary folder names in my PLC directory, along with the filenames generated by this code. When executed, the program filters by directories only, & puts all the folder names into the programnames[] array
 
Codesys - Read the folder names found in a specific directory.

pss.

the code for Reading Directory Names (Folders) starts at the Code Comments :

// 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....
 

Similar Topics

Hello, I am using a Hitachi Micro EHV+ for a small project, and I wanted to have a Web visu, done with Codesys V3.5 SP13 Patch 2. I test the...
Replies
6
Views
293
Hello, I have a requirement to manage the text alignment dynamically. So, for example: 1. English Texts should be displayed from Left in...
Replies
0
Views
90
Hello, I am new to Codesys, and am trying to learn about it for a project we're developing. I've got a couple questions, but first a little...
Replies
1
Views
142
Hi everyone, as this is my first experience with Rockwell Software i would like to know what's the best way to make Enumerations?
Replies
10
Views
503
I am trying to get Codesys to work with a couple of Moxa ioLogik E1200 series DIO devices (E1210 and E1211). I am able to write to the E1211 DOs...
Replies
2
Views
169
Back
Top Bottom