Search code examples
kdb

Access sub-folders in kdb+/Q


The below script returns the list of files included in Main_Dir (which only includes folders)

path: `$":D:/Main_Dir/
files: key(path)

However, I would like to access the files (.csvs) which reside in the sub-folder for which the path being: D:/Main_Dir/sub_dir1,D:/Main_Dir/sub_dir2...sub_dir3

This code is a part of the larger sub-block, which intends to read csvs residing in the sub-folder.

path: `$":D:/Main_Dir/
files: key(path)
loadcsv: {[path;file]("SDNFFFFJJ";enlist csv) 0: ` sv path,file}
d2: raze loadcsv[path] each files 

Solution

  • For the sake of variety, a recursive approach which would work for arbitrarily nested directories (similar to how MurrMacks answer would):

    q){$[{x~key x}y;(();y)y like x;raze .z.s[x]each` sv'y,'key y]}["*.csv";`:.]
    `:./dir1/f1.csv`:./dir3/f3.csv`:./dir3/subdir1/f3s.csv`:./dir3/subdir1/subsubdir2/f3ss.csv