简体   繁体   中英

What does the recursive argument mean in dir.create?

I am trying to get my head around the dir.create() function to create nested folders "folder2" and "folder3".

The solution would be:

dir.create(file.path("folder2", "folder3"), recursive = TRUE)

Edit: explanation of ?dir.create is

recursive logical. Should elements of the path other than the last be created? If true, like the Unix command mkdir -p.

What is the purpose of the recursive attribute?

Picking up from 'mkdir -p' in your question, and explanation here :

If your 'folder2' included parent folders, these will need to be created first, else the create function would not be able to create the final file at the end of the path (there wouldn't be anywhere to put it, as the full path doesn't exist yet).

Lets say 'folder2' was 'a/b/c/'. To create folder 'c', you first need to create folder 'b'. But for 'b' you first need 'a'. So all the parent folders need to be created before the final child folder (or file).

'recursion' refers to 'stepwise repeating again and again'. [make 'a' then 'b' and finally 'c']

Let's say you have a Temp folder in your working directory. If you want to create a folder within a folder in Temp .

dir.create('Temp/A/B/')

This will fail since folder A is not present. However,

dir.create('Temp/A/B/', recursive = TRUE)

will work and create a folder B within folder A .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM