apoc.load.directory

Procedure APOC Full

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?)

Input parameters

Name Type Default

pattern

STRING?

*

urlDir

STRING?

config

MAP?

{}

Config parameters

The procedure support the following config parameters:

Table 1. Config parameters
name type default description

recursive

boolean

true

find all files in current folder and subfolders

Output parameters

Name Type

value

STRING?

Usage Examples

Given the config:

apoc.import.file.use_neo4j_config=true

and the following files in the import directory:

Table 2. 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

The Query:

CALL apoc.load.directory();

will give the following output:

Table 3. Results
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");
Table 4. Results
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");
Table 5. Results
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});
Table 6. Results
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:

beta.csv
name,age
Selma,8
Rana,11
Selina,18

and

alpha.csv
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:

Table 7. 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" }