apoc.load.directory

Procedure APOC Core

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

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

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:

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

We can do the following query:

CALL apoc.load.directory();
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:

CALL apoc.load.directory("*.csv");
Table 4. Results
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");
Table 5. Results
value

"subfolder1/5.csv"

CALL apoc.load.directory("*", "subfolder1");
Table 6. Results
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});
Table 7. Results
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:

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', '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:

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