get list of all files in a folder (and subfolders)
jcor
Subfolders are searched by default, set recurse=0 to disable this.
Tested in MS Windows.
// Given a path to a folder on disk, gets all files ending in "ext"
Function/S findFiles(path, ext[, recurse])
// By default, subfolders are searched. Turn off with recurse=0.
// 2017-05-02, joel corbin
//
string path, ext; variable recurse
if (paramIsDefault(recurse))
recurse=1
endif
path = sanitizeFilePath(path) // may not work in extreme cases
string fileList=""
string files=""
string pathName = "tmpPath"
string folders =path+";" // Remember the full path of all folders in "path" & search each for "ext" files
string fldr
do
fldr = stringFromList(0,folders)
NewPath/O/Q $pathName, fldr // sets S_path=$path, and creates the symbolic path needed for indexedFile()
PathInfo $pathName
files = indexedFile($pathName,-1,ext) // get file names
if (strlen(files))
files = fldr+":"+ replaceString(";", removeEnding(files), ";"+fldr+":") // add the full path (folders 'fldr') to every file in the list
fileList = addListItem(files,fileList)
endif
if (recurse)
folders += indexedDir($pathName,-1,1) // get full folder paths
endif
folders = removeFromList(fldr, folders) // Remove the folder we just looked at
while (strlen(folders))
KillPath $pathName
return fileList
End
static function /s sanitizeFilePath(path)
// Avoid annoyances with escape characters when using Microsoft Windows directories.
string path
path = replaceString("\t", path, "\\t")
path = replaceString("\r", path, "\\r")
path = replaceString("\n", path, "\\n")
return path
end
Function/S findFiles(path, ext[, recurse])
// By default, subfolders are searched. Turn off with recurse=0.
// 2017-05-02, joel corbin
//
string path, ext; variable recurse
if (paramIsDefault(recurse))
recurse=1
endif
path = sanitizeFilePath(path) // may not work in extreme cases
string fileList=""
string files=""
string pathName = "tmpPath"
string folders =path+";" // Remember the full path of all folders in "path" & search each for "ext" files
string fldr
do
fldr = stringFromList(0,folders)
NewPath/O/Q $pathName, fldr // sets S_path=$path, and creates the symbolic path needed for indexedFile()
PathInfo $pathName
files = indexedFile($pathName,-1,ext) // get file names
if (strlen(files))
files = fldr+":"+ replaceString(";", removeEnding(files), ";"+fldr+":") // add the full path (folders 'fldr') to every file in the list
fileList = addListItem(files,fileList)
endif
if (recurse)
folders += indexedDir($pathName,-1,1) // get full folder paths
endif
folders = removeFromList(fldr, folders) // Remove the folder we just looked at
while (strlen(folders))
KillPath $pathName
return fileList
End
static function /s sanitizeFilePath(path)
// Avoid annoyances with escape characters when using Microsoft Windows directories.
string path
path = replaceString("\t", path, "\\t")
path = replaceString("\r", path, "\\r")
path = replaceString("\n", path, "\\n")
return path
end
Forum
Support
Gallery
Igor Pro 9
Learn More
Igor XOP Toolkit
Learn More
Igor NIDAQ Tools MX
Learn More