val val - 10 days ago 5
R Question

File extension renaming in R

I am just trying to change the filename extensions to .doc. I'm trying the code below but it does not work. How come? I'm using instructions from here

startingDir<-"C:/Data/SCRIPTS/R/TextMining/myData"

filez<-list.files(startingDir)

sapply(filez,FUN=function(eachPath){
file.rename(from=eachPath,to=sub(pattern =".LOG",replacement=".DOC",eachPath))
})


The output I get is:

DD17-01.LOG DD17-02.LOG DD17-03.LOG DD17-4.LOG DD17-5.LOG DD37-01.LOG DD37-02.LOG DD39-01.LOG DD39-02.LOG DD39-03.LOG
FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

Answer

It is even easier. Here we start by creating 10 files (in the shell):

$ for i in 0 1 2 3 4 5 6 7 8 9; do touch file${i}.log; done

Then in R it is really just three vectorized operations:

files <- list.files(pattern="*.log")
newfiles <- gsub(".log$", ".doc", files)
file.rename(files, newfiles)

We read the filenames, do the transformation on all of them at once (replacing the trailing .log with .doc) and renamed all files at once from the old names to the new names.

This will echo a TRUE for each implicit renaming:

edd@max:/tmp/filerename$ Rscript renameFiles.R 
 [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
edd@max:/tmp/filerename$ ls
file0.doc  file1.doc  file2.doc  file3.doc  file4.doc  file5.doc  
file6.doc  file7.doc  file8.doc  file9.doc  renameFiles.R
edd@max:/tmp/filerename$ 

Edit: Here is an even more explicit walkthrough doing EVERYTHING in R:

edd@max:/tmp/filerename/new$ ls
renameFiles.R
edd@max:/tmp/filerename/new$ cat renameFiles.R 

options(width=50)
ignored <- sapply(1:10, function(n) write.csv(n, file=paste0("file", n, ".log")))
files <- list.files(pattern="*.log")
print(files)

newfiles <- gsub(".log$", ".doc", files)
file.rename(files, newfiles)

files <- list.files(pattern="*.doc")
print(files)
edd@max:/tmp/filerename/new$ Rscript renameFiles.R 
 [1] "file10.log" "file1.log"  "file2.log" 
 [4] "file3.log"  "file4.log"  "file5.log" 
 [7] "file6.log"  "file7.log"  "file8.log" 
[10] "file9.log" 
 [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[10] TRUE
 [1] "file10.doc" "file1.doc"  "file2.doc" 
 [4] "file3.doc"  "file4.doc"  "file5.doc" 
 [7] "file6.doc"  "file7.doc"  "file8.doc" 
[10] "file9.doc" 
edd@max:/tmp/filerename/new$ 
Comments