Skip to content
Jas edited this page Oct 25, 2019 · 7 revisions

File and Folder Discovery

This library provides functionality to find files and folders within directories on disk.

.file.listFolder / .file.listFolderPaths

These functions list the contents of the specified folder. .file.listFolderPaths returns files and folders with the supplied path prepended (so they can be used with get for example). .file.listFolder returns just file and folder names.

.file.find / .file.findFilePaths

These functions return files that match a specified regular expression. Again, .file.findFilePaths will return any match files or folders with the supplied path prepended, where as .file.find will just return their names.

The regex to find files can be specified either as a symbol, which is then interpreted as match anywhere, or a kdb regex string.

Examples

Using symbol as regex:

q) srcFolder:` sv .file.getCwd[],`src;
Running system command: "echo %cd%"

q) .file.findFilePaths[`log; srcFolder]
,`:C:\Users\jasra_000\git\kdb-common/src/log.q

Using kdb string regex:

q) .file.findFilePaths["*.q"; srcFolder]
`:C:\Users\jasra_000\git\kdb-common/src/convert.q`:C:\Users\jasra_000\git\kdb-common/src/cron.q`:C:\Users\j..

Using string for exact match:

q) .file.findFilePaths["util.q"; srcFolder]
,`:C:\Users\jasra_000\git\kdb-common/src/util.q

.file.tree

This function recursively descends from the specified root folder down all child folders until no more folders are found (it behaves similarly to the Linux command tree). All discovered files are returned with the specified path prefixed.

Please note that folders that are symbolic links will be followed so you must ensure that there are no circular references within the folder structure you wish to tree.

.file.ensureDir

This function checks if the specified folder exists on disk already. If it doesn't it creates the folder before returning. It uses the OS library to create the new folder

.file.loadDir

This is a simple wrapper around system "l" with the conversion of a folder path symbol into a string.

.file.getCwd

This function returns the current working directory of the kdb process. It uses the OS library to get the raw result from the OS and then ensures it's in a file path symbol format.

Example

OS library working directory function:

q) .os.run[`pwd;::]
Running system command: "echo %cd%"
"C:\\Users\\jasra_000\\git\\kdb-common "

File library .file.getCwd

q) .file.getCwd[]
Running system command: "echo %cd%"
`:C:\Users\jasra_000\git\kdb-common

.file.isCompressed

This function returns true if the specified file has been compressed by kdb+ native compression.

Clone this wiki locally