Article
· Feb 17, 2016 3m read

Listing files in folder

Question:

How do I get a list of files residing in a certain folder/directory, according to some wildcard/filter.

For example all '*.txt' files in 'C:\Temp'.

Answer:

In CACHE –

You can use the %Library.File's FileSet class query.

Here's some sample code using it (also attached):

 

run(pDir,pFileSpec)

      Set tRS=##class(%ResultSet).%New("%Library.File:FileSet")

      Set tSC=tRS.Execute(pDir,pFileSpec)

      Write "Name",?30,"Date Modified",?53,"Type",!
      Write "--------------------------------------------------------------------------",!

      While tRS.Next() {
            Write tRS.Get("Name"),?30,tRS.Get("DateModified"),?53,tRS.Get("Type"),!

      }

And here's an example running it:

 

USER>do run^testFileSet("C:\Temp","*.txt")

Name                          Date Modified          Type

--------------------------------------------------------------------------

C:\Temp\hl7.txt               2014-07-30 12:09:18    F

C:\Temp\hsaa_msgs.txt         2015-06-25 13:02:16    F

C:\Temp\JSONRESTProxy_10.txt  2014-05-04 14:29:04    F

C:\Temp\JSONRESTProxy_8.txt   2014-05-04 14:28:09    F

C:\Temp\myTestFile2.txt       2014-05-05 09:19:31    F

C:\Temp\newStream.txt         2015-07-09 09:41:59    F

C:\Temp\oldStream.txt         2015-07-09 09:41:27    F

C:\Temp\tcpTrace.txt          2014-05-29 12:13:11    F

C:\Temp\tempadts.txt          2015-06-22 08:35:03    F

C:\Temp\WSLic.txt             2014-06-08 10:34:13    F

 

In Ensemble –

You can use the File Outbound Adapter in a BO, calling the Adapter's NameList() method.

 

Here's a sample BO using it (also attached):

Class Play.BO.FileOperation Extends Ens.BusinessOperation
{

Parameter ADAPTER = "EnsLib.File.OutboundAdapter";

Property Adapter As EnsLib.File.OutboundAdapter;

Parameter INVOCATION = "Queue";

Method FileList(
      pRequest As Ens.StringRequest,
      Output pResponse As Ens.Response) As %Status
{
      #dim fileList As %ListOfDataTypes = ""
      Set status = ..Adapter.NameList(.fileList,pRequest.StringValue)
     
      // Returns list of: Filename;Type;Size;DateCreated;DateModified;FullPathName
      //                  1        2    3    4           5            6  
      If $$$ISOK(status)&&$IsObject(fileList) {
            For i=1:1:fileList.Count() {
                  Set fileInfo = fileList.GetAt(i)
                  $$$LOGINFO("Name: "_$Piece(fileInfo,";",1)_" / DateModified: "_$Piece(fileInfo,";",5)_" / Type: "_$Piece(fileInfo,";",2))
            }
           
      }
     
      Quit status
}

 

And here's sample output from the Event Log:

Text 

Name: WSLic.txt / DateModified: modified=2014-06-08 10:34:13 / Type: =F

Name: tempadts.txt / DateModified: modified=2015-06-22 08:35:03 / Type: =F

Name: tcpTrace.txt / DateModified: modified=2014-05-29 12:13:11 / Type: =F

Name: oldStream.txt / DateModified: modified=2015-07-09 09:41:27 / Type: =F

Name: newStream.txt / DateModified: modified=2015-07-09 09:41:59 / Type: =F

Name: myTestFile2.txt / DateModified: modified=2014-05-05 09:19:31 / Type: =F

Name: JSONRESTProxy_8.txt / DateModified: modified=2014-05-04 14:28:09 / Type: =F

Name: JSONRESTProxy_10.txt / DateModified: modified=2014-05-04 14:29:04 / Type: =F

Name: hsaa_msgs.txt / DateModified: modified=2015-06-25 13:02:16 / Type: =F

Name: hl7.txt / DateModified: modified=2014-07-30 12:09:18 / Type: =F

 

Note the adapter method uses the %File:FileSet query behind the scenes.

Discussion (2)1
Log in or sign up to continue