Gevorg Arutiunian · Aug 22, 2018 1m read

Get all filenames in a specified directory

(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

2 1,391
Discussion (3)4
Log in or sign up to continue

Instead of using $ZSEARCH you can use the query FileSet in class %File:

ClassMethod getFiles(pDir As %String, pMask As %String)
   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