(Originally posted on Intersystems CODE by @Eduard Lebedyuk, 10/12/15) The following code snippet outputs all filenames in the file path "dir" in the Cache/IRIS terminal. The class method "test" runs the code:
Class eduardlebedyuk.filenamesInDir Extends %RegisteredObject
{
classmethod test() {
// replace dir with file path you want
set dir = "D:\directory"
set dir = ##class(%File).NormalizeDirectory(dir)
set file=$ZSEARCH(dir_"*")
while file'="" {
write !,file
set file=$ZSEARCH("")
}
}
}
Here's a link to the code on GitHub
Instead of using $ZSEARCH you can use the query FileSet in class %File:
{
set dir=##class(%File).NormalizeDirectory(pDir)
set result=##class(%ResultSet).%New("%File:FileSet")
do result.Execute(dir,pMask)
while result.Next() {
write !,result.GetDataByName("Name")
}
}
Hi, you are right! One of the possible options is this.
PS: "All of them use $ZSEARCH function to traverce directories" - by @Eduard Lebedyuk
See also this related article.