0

I wanted to rename the existing files in my working directory following this link using the part of the existing name. This is the list of file names

> files_names
 [1] "./AKLBNE_oneway_JobId427508.csv" "./AKLMEL_oneway_JobId427512.csv"
 [3] "./AKLSYD.csv"                    "./AMSPEK_oneway_JobId427522.csv"
 [5] "./ATXDXB_oneway_JobId427524.csv" "./AUHLHR_oneway_JobId427528.csv"
 [7] "./BAHDXB_JobId427533.csv"        "./BHMDXB_oneway_JobId427557.csv"
 [9] "./BKKDOH_oneway_JobId427563.csv" "./BKKFRA_JobId427565.csv"       
[11] "./BKKHKG_JobId427568.csv"        "./BKKKIX_JobId427572.csv"       
[13] "./BKKNRT_JobId427575.csv"        "./BNEAKL_oneway_JobId427577.csv"
[15] "./BOMDXB_JobId427581.csv"        "./BRUDXB_JobId427583.csv"       
[17] "./CANLAX_JobId427585.csv"        "./CANPVG_JobId427588.csv"       
[19] "./CDGCUN_JobId427590.csv"        "./CDGDXB_JobId427594.csv"       
[21] "./CDGIAD_JobId427596.csv"        "./CDGJFK_JobId427598.csv"       
[23] "./CDGKUL_JobId427600.csv"        "./CDGLHR_JobId427603.csv"       
[25] "./CDGNRT_JobId427605.csv"        "./CDGSFO_JobId427607.csv"       
[27] "./CDGYUL_JobId427610.csv"        "./CTUPEK_JobId427613.csv"       
[29] "./SINCDG.csv"

This is the code I wrote:

destination<-vector(length = length(files_names))
for (i in 1:length(files_names)){
  files_names<-list.files(pattern = "*.csv", full.names = TRUE)
  destination[i]<-substr(files_names[i],3, 8)
  file.rename(files_names[i], paste(destination[i]))
}

But instead of renaming it created another files in my working directory. What is the problem?

Community
  • 1
  • 1
Rfreak
  • 149
  • 4
  • 9
  • Without going through the code - if the files are opened by another process, that's what would happen. – rbm May 27 '16 at 08:58
  • Just curious, in the condition part of your for loop, you use `length(files_names)` but why do you redefine `files_names` within the for loop itself? – zyurnaidi May 27 '16 at 09:17
  • Also, you don't need to use loop for your purpose. Try `file.rename(files_names, substr(files_names, 3, 8))`. Please note that your `substr` function would take out the `csv` extension from most of your files. – zyurnaidi May 27 '16 at 09:39
  • Yeah, that was not smart to do inside the loop. Is it possible to add `csv` to your code to preserve the extention? `@zyurnaidi ` – Rfreak May 27 '16 at 11:23

1 Answers1

0

I think the problem arises form your files_names data and your file.rename function paramteres.

Use list.files(path, pattern = "\\.csv$", full.names = TRUE) to get the list of csv files in your defined path directory.

That is:

files_names<-list.files(path, pattern = "\\.csv$", full.names = TRUE)
sapply(files_names, function(x) { 
file.rename(from = x, to=paste0(path, "newName.csv")
} )

This should work without any problem.

For the sake of exposition, you can have a look at the following example for renaming txt files.

Suppose we have four different text files (located in C:/Users/R files/) named tetstest1.txt through tetstest4.txt. We want to rename these files to newTextString1 through newTextString4.

Here is what we can do:

# path
# [1] "C:/Users/R files/"

lst=list.files(path, pattern = "tetstest", full.names = TRUE)

# lst
# [1] "C:/Users/R files/tetstest1.txt" "C:/Users/R files/tetstest2.txt"
# [3] "C:/Users/R files/tetstest3.txt" "C:/Users/R files/tetstest4.txt"

sapply(lst, function(x) file.rename(from = x, 
to=sub(pattern="tetstest", replacement="newTextString", x)))


list.files(path, pattern = "tetstest", full.names = TRUE)
# character(0)

list.files(path, pattern = "newTextString", full.names = TRUE)
# [1] "C:/Users/R files/newTextString1.txt" "C:/Users/R files/newTextString2.txt"
# [3] "C:/Users/R files/newTextString3.txt" "C:/Users/R files/newTextString4.txt"

You can just adjust the path, pattern and replacement accordingly.

989
  • 12,579
  • 5
  • 31
  • 53
  • But this does not answer OP's question. – rbm May 27 '16 at 13:43
  • Why not? Asker just needs to change the `path` and `pattern` accordingly. – 989 May 27 '16 at 14:29
  • OP\s question is "nstead of renaming it created another files in my working directory. What is the problem?" You do not answer that question. – rbm May 27 '16 at 14:30
  • if we suppose the files are too large to be duplicated, then this answer fails... – cory May 27 '16 at 14:52