File extension renaming in R

蓝咒 提交于 2020-05-15 11:10:10

问题


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 

回答1:


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                    ## no files here
renameFiles.R
edd@max:/tmp/filerename/new$ cat renameFiles.R     ## code we will run

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  ## running it
 [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$ 


来源:https://stackoverflow.com/questions/40754689/file-extension-renaming-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!