GetFileList


The GetFileList function returns a lua table containing the names of files in a folder. The names are full path names including the full path. The names must match the specified template, which can be simply "*.*".

Syntax

nFiles = GetFileList( sFolder )

nFiles = GetFileList( sFolder, sTemplate )

nFiles = GetFileList( sFolder, sTemplate, tblNames )

bullet.gif    sFolder is the folder to search.

bullet.gif    sTemplate is the name pattern to match.

bullet.gif    tblNames is a table containing the file names, including the full path.

bullet.gif    On success, nFiles > 0 and tblNames contains an array of strings.

bullet.gif    On failure, nFiles=0.

Remarks

This function creates a table that can be parsed using the Lua pairs function, as shown in the example below. The names do no unpack in any "nice" order, like numeric ot alphabetical order. Also see the CFindFiles class for another way to load files from a folder.

Example

This example selects a target folor, then matches all files matching the template "*.*". The files are listed at the end. Note that the entire "for" loop could be replaced by list(tbl) but the latter method does not permit formatting using Printf.

 

-- fetch the folder

 

 

sStart = "C:\\Downloads"

-- default location

 

sFolder, bOK = GetFolder(sStart)

-- select folder in GetFolder()

 

if not bOK then Exit("aborted\n") end

 

 

-- find all "*.fts" files in the folder

 

 

tbl = {}

-- create a table for the file names

 

nFiles = GetFileList( sFolder, "*.*", tbl )

-- return the table of file names

 

if nFiles < 1 then Exit("No files\n") end

-- check if no files returned

 

Printf("%d files found\n", nFiles)

 

 

-- Now extract the names from the table and list them.

 

 

-- Remember: Lua tables are not in numerical

 

 

-- or alphabetical order of the keys.

 

 

for key, sFileName in pairs(tbl) do

 

 

  Printf("%s: %s\n", key, sFileName )

 

 

end

 

 

Related Topics

Directory Functions, CFindFiles , GetFolder, GetFileName


Mira Pro x64 Script User's Guide, Copyright Ⓒ 2023 Mirametrics, Inc. All Rights Reserved.