apoc.load.directory
Procedure Apoc Extended
apoc.load.directory('pattern', 'urlDir', {config}) YIELD value - Loads list of all files in the folder specified by the parameter urlDir satisfying the given pattern. If the parameter urlDir is not specified or empty, the files of the import folder are loaded instead.
Signature
apoc.load.directory(pattern = * :: STRING?, urlDir = :: STRING?, config = {} :: MAP?) :: (value :: STRING?)
Config parameters
The procedure support the following config parameters:
name | type | default | description |
---|---|---|---|
recursive |
boolean |
true |
find all files in current folder and subfolders |
Usage Examples
Given the config:
apoc.import.file.use_neo4j_config=true
and the following files in the import
directory:
fileName | path |
---|---|
db.json |
/ |
1.csv |
/ |
3.csv |
/ |
4.csv |
/ |
8.xls |
/ |
11.txt |
/ |
5.csv |
subfolder1 |
6.xls |
subfolder1 |
7.xls |
subfolder1 |
20.xls |
subfolder1/foo |
2.csv |
subfolder2 |
9.xls |
subfolder2 |
10.txt |
subfolder2 |
The Query:
CALL apoc.load.directory();
will give the following output:
value |
---|
"4.csv" |
"1.csv" |
"db.json" |
"3.csv" |
"subfolder2/10.txt" |
"subfolder2/2.csv" |
"subfolder2/9.xls" |
"11.txt" |
"8.xls" |
"subfolder1/5.csv" |
"subfolder1/.DS_Store" |
"subfolder1/7.xls" |
"subfolder1/foo/20.xls" |
"subfolder1/6.xls" |
We can set a file pattern, for example searching for files with the file ending .csv
:
CALL apoc.load.directory("*.csv");
value |
---|
"4.csv" |
"1.csv" |
"3.csv" |
"subfolder2/2.csv" |
"subfolder1/5.csv" |
We can also set a path, for example searching only the files in subfolder1
and its subfolders:
CALL apoc.load.directory("*.csv", "subfolder1");
value |
---|
"subfolder1/5.csv" |
It is possible to limit the search to only a single folder without traversing any subfolders:
CALL apoc.load.directory("*", "subfolder2", {recursive: false});
value |
---|
"subfolder2/10.txt" |
"subfolder2/2.csv" |
"subfolder2/9.xls" |
If we set apoc.import.file.use_neo4j_config=false
, we can search with an absolute path:
CALL apoc.load.directory('*', 'file:///Users/username/Downloads', {recursive: false});
We can concatenate this procedure with other procedures, for example with apoc.load.csv to obtain an aggregation of multiple results. Let’s suppose we have these 2 files:
name,age Selma,8 Rana,11 Selina,18
and
name,beverage Selma,Soda Rana,Tea|Milk Selina,Cola
so we can execute this query:
CALL apoc.load.directory('*.csv')
YIELD value WITH value as url ORDER BY url DESC
CALL apoc.load.csv(url, {results:['map']}) YIELD map RETURN map
with these results:
map |
---|
{ "name": "Selma", "age": "8" } |
{ "name": "Rana", "age": "11" } |
{ "name": "Selina", "age": "18" } |
{ "name": "Selma", "beverage": "Soda" } |
{ "name": "Rana", "beverage": "Tea|Milk" } |
{ "name": "Selina", "beverage": "Cola" } |