Skip to content

file.copy

6 messages · Jeff Newmiller, Duncan Murdoch, Muhuri, Pradip (SAMHSA/CBHSQ)

#
Hello,

Here is something (file.copy) trivial but does not seem to work.  I could not figure out what I am doing wrong.  

The R script below creates folders (fromFolder and toFolder) and finds the list of files (list.of.files) to be copied to the toFolder, which I have verified using the print ()  command. But, the issue is that the file.copy() command does not work.  

Both the R.script and the console are shown below.

Any help/hints will be appreciated.

Thanks,

Pradip Muhuri

#########################  R script  #####################################################
#file.copy.R

#identify the folders
fromFolder <- "H:/R/cis_study"
toFolder <- "F:/cis_study_backup"

# find the list of files to copy 
list.of.files <- list.files(fromFolder, ".R$")

# print objects
print(c(fromFolder, toFolder, list.of.files))
options(warn=1)

# copy the files to the toFolder  - THIS DOES NOT WORK WHILE EVERYTHING PRIOR HAS WORKED
file.copy(list.of.files, toFolder)



#####################  Below is from console ###################
#file.copy.R
[1] "H:/R/cis_study"          "F:/cis_study_backup"    
 [3] "anl.in.scope_111114.R"   "create.oid.data.frame.R"
 [5] "create_xd2012.R"         "file.copy.R"            
 [7] "further.data.R"          "mrj.in.scope_111214.R"  
 [9] "oid.in.scope_111114.R"   "oid_cohort.R"           
[11] "warning.max.R"           "xdate.R"                
[13] "years.before.anl.init.R" "years.before.mrj.init.R"
[15] "years.before.oid.init.R"
Warning in file.copy(list.of.files, toFolder) :
  problem copying .\anl.in.scope_111114.R to F:\cis_study_backup\anl.in.scope_111114.R: No such file or directory
(other similar warning messages are not shown)



Pradip K. Muhuri, PhD
SAMHSA/CBHSQ
1 Choke Cherry Road, Room 2-1071
Rockville, MD 20857
Tel: 240-276-1070
Fax: 240-276-1260
#
Your list.of.files variable just has filenames without the fromFolder path. Try

file.copy(file.path,fromFolder,list.of.files), toFolder)

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
On November 14, 2014 4:07:27 AM PST, "Muhuri, Pradip (SAMHSA/CBHSQ)" <Pradip.Muhuri at samhsa.hhs.gov> wrote:
#
Sorry.. typo...

file.copy(file.path(fromFolder,list.of.files), toFolder)

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
On November 14, 2014 4:36:12 AM PST, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:
#
On 14/11/2014 8:21 AM, Jeff Newmiller wrote:
Or construct the list of files containing full paths.  See ?list.files.

Duncan Murdoch
#
Jeff,
Thank you so much for your help.

Below are the revised code (done with your hints) that has worked and the console. I have just added - overwrite=TRUE) to file.copy().

Pradip

###############################################
#file.copy.jn.way.R

#identify the folders
fromFolder <- "H:/R/cis_study"
toFolder <- "F:/cis_study_backup"

# find the list of files to copy
list.of.files <- list.files(fromFolder, ".R$")

# print objects
print(c(fromFolder, toFolder, list.of.files))
options(warn=1)

# copy the files to the toFolder  - THIS DOES NOT WORK WHILE EVERYTHING PRIOR HAS WORKED

file.copy(file.path(fromFolder,list.of.files), toFolder, overwrite=TRUE)

###################  revised console ########################
[1] "H:/R/cis_study"          "F:/cis_study_backup"    
 [3] "anl.in.scope_111114.R"   "create.oid.data.frame.R"
 [5] "create_xd2012.R"         "file.copy.R"            
 [7] "file.copy_Duncan_way.R"  "further.data.R"         
 [9] "mrj.in.scope_111214.R"   "oid.in.scope_111114.R"  
[11] "oid_cohort.R"            "warning.max.R"          
[13] "xdate.R"                 "years.before.anl.init.R"
[15] "years.before.mrj.init.R" "years.before.oid.init.R"
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
Pradip K. Muhuri, PhD
SAMHSA/CBHSQ
1 Choke Cherry Road, Room 2-1071
Rockville, MD 20857
Tel: 240-276-1070
Fax: 240-276-1260
#
Hello Duncan,

Jeff's tweaks to my code has worked.   Now I am trying your way. Below are the R script and console.   The issue is that the object (list.of.files) has not been created.  Any thoughts?

Thanks,

###############  R script ##########################

#file.copy.dm.way.R

#identify the folders

fromFolder <- file.path("H:", "cis_study")

toFolder <- file.path("F:", "cis_study")

# find the list of files to copied
list.of.files <- list.files(fromFolder, ".R$")

# print objects
print(fromFolder, list.of.files, toFolder)

# copy the files
file.copy(list.of.files, toFiles)

###############  console ###############
Error in print.default(fromFolder, list.of.files, toFolder) : 
  invalid 'digits' argument
logical(0)