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


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")