<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to How do I list the folder names found in a PLC Directory?</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/fa3528985a/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/Engineering/thread/fa3528985a/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/Engineering/thread/fa3528985a/</id><updated>2020-02-27T14:11:05.612000Z</updated><subtitle>Recent posts to How do I list the folder names found in a PLC Directory?</subtitle><entry><title>How do I list the folder names found in a PLC Directory?</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/fa3528985a/?limit=25#e0ef" rel="alternate"/><published>2020-02-27T14:11:05.612000Z</published><updated>2020-02-27T14:11:05.612000Z</updated><author><name>timvh</name><uri>https://forge.codesys.com/u/timvh/</uri></author><id>https://forge.codesys.comfc50f2575a3a64411a06ceeadb4b7be1723e3e67</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Thank you for sharing this.&lt;/p&gt;
&lt;p&gt;For your information:&lt;br/&gt;
There is a "Use Case" library "File access". This contains the CAA File library in which there are function blocks for opening, listing and closing libraries.&lt;br/&gt;
In functionality it is pretty much similar to the SysLib library, but it is made according to the V3 CODESYS library guidelines.&lt;br/&gt;
A difference is that it is not blocking, which means that in your case if you have a long list of directories, you will not have the problem of having long scan times.&lt;/p&gt;
&lt;p&gt;In the code you should call the FB's with Execute set to TRUE. When the result is done or error you can call the next FB or do some error handling.&lt;br/&gt;
When finished, call the FB's with Execute set to FALSE.&lt;/p&gt;
&lt;p&gt;In the CODESYS help you can find some examples (see Libraries =&amp;gt; System Libraries =&amp;gt; CAA Libraries =&amp;gt; CAA_File.library).&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>How do I list the folder names found in a PLC Directory?</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/fa3528985a/?limit=25#0d91" rel="alternate"/><published>2020-02-27T14:11:05.247000Z</published><updated>2020-02-27T14:11:05.247000Z</updated><author><name>EnterDownload</name><uri>https://forge.codesys.com/u/enterdownload/</uri></author><id>https://forge.codesys.com8bcbff8a697052c56f2086c5512539b0f8f5c6c2</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;SOLVED :&lt;/p&gt;
&lt;p&gt;The key was using SysDirRead and Directory_Info&lt;/p&gt;
&lt;p&gt;Structure DIRECTORY_INFO :&lt;br/&gt;
----&amp;gt;This structure (system library SysLibDir.lib contains information on a directory, which is read via&lt;br/&gt;
       function SysDirRead.&lt;/p&gt;
&lt;p&gt;If the directory_info object ,"stDirInfo.diDirectory" , is Hex 2 (16#2), then the Directory is a folder, otherwise if "stDirInfo.diDirectory" is &lt;br/&gt;
Hex 0  (16#0) ,then the object is a File.&lt;/p&gt;
&lt;p&gt;The Code for looping to get folder names is toward the middle on the code below: The result folder names are put into an array &lt;br/&gt;
programnames[]&lt;/p&gt;
&lt;p&gt;NOTE : You will need to manually put arbitrary folder names in your PLC directory 1st. (This code will autogenerate the file names).&lt;br/&gt;
           When the code executes,the filter will be on folder names (directories only) and only directory names will populate programnames[]&lt;/p&gt;
&lt;p&gt;VAR&lt;br/&gt;
sDirName : STRING;&lt;br/&gt;
sFirstFileName : STRING;&lt;br/&gt;
sSecondFileName : STRING;&lt;br/&gt;
sThirdFileName : STRING;&lt;/p&gt;
&lt;p&gt;iFilesCount : INT;&lt;/p&gt;
&lt;p&gt;sDirEntry : STRING;&lt;br/&gt;
stDirInfo : DirInfo;&lt;br/&gt;
udiResult : __XWORD;&lt;br/&gt;
udiDirHandle : RTS_IEC_HANDLE;&lt;br/&gt;
udiFileHandle : RTS_IEC_HANDLE; &lt;br/&gt;
udiReadResult : UDINT; &lt;br/&gt;
xStartTest : BOOL:= FALSE; (&lt;em&gt; true, if a button is pressed &lt;/em&gt;)&lt;br/&gt;
xTestDone : BOOL:= FALSE;&lt;br/&gt;
xError : BOOL;&lt;br/&gt;
ErrorCode : RTS_IEC_RESULT;&lt;br/&gt;
udiCreateError: UDINT;&lt;br/&gt;
udiReadError: UDINT;&lt;br/&gt;
udiCloseError: UDINT;&lt;br/&gt;
udiRenameError: UDINT;&lt;br/&gt;
udiDeleteError1: UDINT;&lt;br/&gt;
udiDeleteError2: UDINT;&lt;br/&gt;
udiDirDeleteError: UDINT;&lt;br/&gt;
programnames: ARRAY&lt;span&gt;[1..32]&lt;/span&gt; OF String;&lt;br/&gt;
programcount: int;&lt;/p&gt;
&lt;p&gt;END_VAR&lt;/p&gt;
&lt;p&gt;(&lt;em&gt; This program uses the synchronous functions for directory modification&lt;/em&gt;)&lt;/p&gt;
&lt;p&gt;IF xStartTest THEN&lt;br/&gt;
(&lt;em&gt; create the complete file pathes&lt;/em&gt;) &lt;br/&gt;
(&lt;em&gt; The file names can be adapted &lt;/em&gt;)&lt;br/&gt;
sDirName := '/SystemDirExample';&lt;br/&gt;
sFirstFileName := '/firstTest.txt';&lt;br/&gt;
sSecondFileName := '/secondTest.txt';&lt;br/&gt;
sThirdFileName := '/thirdTest.txt';&lt;br/&gt;
xError := FALSE;&lt;br/&gt;
xTestDone := FALSE;&lt;br/&gt;
sDirName := CONCAT(g_sMainDirectory, sDirName);&lt;br/&gt;
sFirstFileName := CONCAT(sDirName, sFirstFileName);&lt;br/&gt;
sSecondFileName := CONCAT(sDirName, sSecondFileName);&lt;br/&gt;
sThirdFileName := CONCAT(sDirName, sThirdFileName);&lt;/p&gt;
&lt;p&gt;(&lt;em&gt; Clean up existing example files&lt;/em&gt;)&lt;br/&gt;
(&lt;em&gt; Delete existing example files &lt;/em&gt;)&lt;br/&gt;
//udiDeleteError1:=SysFileDelete(szFileName:=sFirstF ileName );&lt;br/&gt;
//udiDeleteError2:=SysFileDelete(szFileName:=sThirdF ileName );&lt;/p&gt;
&lt;p&gt;(&lt;em&gt; delete the directory &lt;/em&gt;)&lt;br/&gt;
//udiDirDeleteError:= SysDirDelete(szDir:= sDirName);&lt;/p&gt;
&lt;p&gt;(&lt;em&gt; create a new directory &lt;/em&gt;)&lt;br/&gt;
udiCreateError := SysDirCreate(szDir:= sDirName);&lt;br/&gt;
(&lt;em&gt; two files are created and stored into the directory&lt;/em&gt;)&lt;br/&gt;
udiFileHandle := SysFileOpen(szFile:=sFirstFileName, am:=AM_WRITE_PLUS, pResult:=ADR(ErrorCode));&lt;br/&gt;
IF udiFileHandle &amp;lt;&amp;gt; RTS_INVALID_HANDLE THEN&lt;br/&gt;
SysFileClose(hFile:=udiFileHandle);&lt;br/&gt;
END_IF&lt;/p&gt;
&lt;p&gt;udiFileHandle := SysFileOpen(szFile:=sSecondFileName, am:=AM_WRITE_PLUS, pResult:=ADR(ErrorCode));&lt;br/&gt;
IF udiFileHandle &amp;lt;&amp;gt; RTS_INVALID_HANDLE THEN&lt;br/&gt;
SysFileClose(hFile:=udiFileHandle);&lt;br/&gt;
END_IF&lt;/p&gt;
&lt;p&gt;(&lt;em&gt; open the directory &lt;br/&gt;
the handle is returned and the first directory entry. Normally the first entry is "." and the second ".."&lt;/em&gt;)&lt;br/&gt;
udiDirHandle := SysDirOpen(szDir:= sDirName, szDirEntry:= sDirEntry, diMaxDirEntry:= 1024, &lt;br/&gt;
pDirInfo:= ADR(stDirInfo), pResult:= udiResult);&lt;/p&gt;
&lt;p&gt;(&lt;em&gt; Iterate through all directory entries and count them. The implicit entries with points are not count.&lt;br/&gt;
when the last entry is reached the SysDirRead always delivers the last entry.&lt;/em&gt;)&lt;br/&gt;
iFilesCount := 0;&lt;br/&gt;
programcount := 1;&lt;br/&gt;
REPEAT&lt;br/&gt;
IF FIND(sDirEntry, '.') &amp;lt;&amp;gt; 1 THEN&lt;br/&gt;
iFilesCount := iFilesCount +1;&lt;br/&gt;
END_IF&lt;br/&gt;
udiReadError:= SysDirRead(hDir:= udiDirHandle, szDirEntry:= sDirEntry, diMaxDirEntry:= 1024, pDirInfo:=ADR(stDirInfo) );&lt;/p&gt;
&lt;p&gt;// SysDirRead is used to Directory Info&lt;br/&gt;
// Here, Im using SysDirRead to Find Folder Names within a Directory&lt;br/&gt;
// If stDirInfo.diDirectory is Hex 2 , then the Directory is a folder, otherwise Hex 0 is a File&lt;br/&gt;
IF stDirInfo.diDirectory = 16#2 AND Find(sDirEntry,'.') &amp;lt;&amp;gt; 1 THEN&lt;br/&gt;
programnames&lt;span&gt;[programcount]&lt;/span&gt;:=sDirEntry;&lt;br/&gt;
programcount:=programcount+1; &lt;br/&gt;
END_IF&lt;/p&gt;
&lt;p&gt;UNTIL&lt;br/&gt;
udiReadError = CmpErrors.Errors.ERR_END_OF_OBJECT&lt;br/&gt;
END_REPEAT&lt;/p&gt;
&lt;p&gt;(&lt;em&gt; The two created files must be read.&lt;/em&gt;)&lt;br/&gt;
//IF iFilesCount &amp;lt;&amp;gt; 2 THEN&lt;br/&gt;
// xError := TRUE;&lt;br/&gt;
//END_IF&lt;/p&gt;
&lt;p&gt;(&lt;em&gt; Close the directory &lt;/em&gt;)&lt;br/&gt;
udiCloseError := SysDirClose(hDir:= udiDirHandle);&lt;/p&gt;
&lt;p&gt;(&lt;em&gt; Rename a file &lt;/em&gt;)&lt;br/&gt;
udiRenameError:=SysFileRename(szOldFileName:= sSecondFileName, szNewFileName:= sThirdFileName);&lt;/p&gt;
&lt;p&gt;(&lt;em&gt; the test is set to done, if no error occured &lt;/em&gt;)&lt;br/&gt;
IF udiCreateError = 0 AND udiCloseError = 0 AND udiRenameError = 0 AND xError = FALSE THEN&lt;br/&gt;
xTestDone := TRUE;&lt;br/&gt;
ELSE&lt;br/&gt;
xError := TRUE;&lt;br/&gt;
END_IF&lt;/p&gt;
&lt;p&gt;xStartTest := FALSE; &lt;/p&gt;
&lt;p&gt;END_IF&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>How do I list the folder names found in a PLC Directory?</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/fa3528985a/?limit=25#74b0" rel="alternate"/><published>2020-02-27T14:11:04.256000Z</published><updated>2020-02-27T14:11:04.256000Z</updated><author><name>EnterDownload</name><uri>https://forge.codesys.com/u/enterdownload/</uri></author><id>https://forge.codesys.comd9b6e99d4330bacde79f109ed3e9fce59ab860e3</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hello All,&lt;/p&gt;
&lt;p&gt;Im new to Codesys. I have a Festo PLC.&lt;/p&gt;
&lt;p&gt;What I would like to do is go to a known file path on my PLC and read the  names of the subfolders found under a Parent folder.&lt;/p&gt;
&lt;p&gt;Parent Folder (C:/Temp)&lt;/p&gt;
&lt;p&gt;---&amp;gt; Subfolder 1&lt;br/&gt;
---&amp;gt; Subfolder 2&lt;br/&gt;
---&amp;gt; Subfolder 3&lt;/p&gt;
&lt;p&gt;My Result should be  a list : Name of Subfolder1,Name of Subfolder2, Name of Subfolder3&lt;/p&gt;
&lt;p&gt;Please provide code AND any pertinent explanation.....thanks&lt;/p&gt;
&lt;p&gt;JL&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>