[R] copyING directories and files

Romain Francois romain.francois at dbmail.com
Fri Dec 11 11:35:57 CET 2009


What about this as a start :

dir.copy <- function( source = getwd(), target ){
	
	files <- list.files( source, recursive = TRUE )
	dirs  <- unique( gsub( "/[^/]+$", "", files[grepl("/", files)] ) )
	
	if( !file.exists( target ) ){
		dir.create( target )
	}
	
	for( d in dirs){
		dir.create( file.path( target, d) , recursive = TRUE )
	}
	for( f in files ){
		file.copy( file.path(source, f) , file.path( target, f ) )
	}
	invisible(NULL)
}


# what I am copying :
 > system( "tree" )
.
└── bar
     ├── blabla.txt
     ├── bla.txt
     └── foobar
         └── blabla.txt

2 directories, 3 files

 > dir.copy( getwd(), "/tmp/target" )
 > system( "tree /tmp/target" )
/tmp/target
└── bar
     ├── blabla.txt
     ├── bla.txt
     └── foobar
         └── blabla.txt

2 directories, 3 files

Romain

On 12/11/2009 11:18 AM, Uwe Ligges wrote:
>
> I'd use a shell() command to call xcopy or robocopy, or cp given you
> have some unix tools installed.
>
> Uwe Ligges
>
>
>
> Paul Evans wrote:
>> Hi,
>>
>> I am using the windows version of R. I wanted to copy a directory
>> (containing several files) to another directory. Is there any command
>> in R that will let me do this (something like the 'cp' command in
>> UNIX)? I have looked at 'file.copy', but as the name implies I think
>> it only copies one file at a time.
>> thanks!

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/Gq7i : ohloh
|- http://tr.im/FtUu : new package : highlight
`- http://tr.im/EAD5 : LondonR slides




More information about the R-help mailing list