apoc.load.directory
Procedure APOC Full
apoc.load.directory('pattern', 'urlDir', {config}) YIELD value - Loads list of all files in folder specified by urlDir or in import folder if urlDir string is empty or not specified
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
The first parameter is the pattern of file to search for.
By default is '*', that is, search all files.
The second is the search path of directory.
By default is an empty string, that is, search file in import
directory.
Let’s suppose we have set the config option:
apoc.import.file.use_neo4j_config=true
and in the import
directory the following files:
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 |
We can do the following query:
CALL apoc.load.directory();
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:
CALL apoc.load.directory("*.csv");
value |
---|
"4.csv" |
"1.csv" |
"3.csv" |
"subfolder2/2.csv" |
"subfolder1/5.csv" |
Furthermore we can also set a path, like:
CALL apoc.load.directory("*.csv", "subfolder1");
value |
---|
"subfolder1/5.csv" |
CALL apoc.load.directory("*", "subfolder1");
value |
---|
"subfolder1/5.csv" |
"subfolder1/.DS_Store" |
"subfolder1/7.xls" |
"subfolder1/foo/20.xls" |
"subfolder1/6.xls" |
And we can search only in current folder:
CALL apoc.load.directory("*", "subfolder2", {recursive: false});
value |
---|
"subfolder2/10.txt" |
"subfolder2/2.csv" |
"subfolder2/9.xls" |
Instead, 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', 'path/to/directory')
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" } |