在R中重命名文件扩展名 [英] File extension renaming in R
问题描述
我只是想将文件扩展名更改为.doc.我正在尝试下面的代码,但是它不起作用.怎么会?我正在使用
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))
})
我得到的输出是:
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
推荐答案
更容易.在这里,我们先创建10个文件(在外壳中):
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
然后在R中实际上只是三个 vectorized 操作:
Then in R it is really just three vectorized operations:
files <- list.files(pattern="*.log")
newfiles <- gsub(".log$", ".doc", files)
file.rename(files, newfiles)
我们读取文件名,一次对所有文件名进行转换(将尾随的.log
替换为.doc
),并立即将 all 文件从旧名称重命名为新名称.
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.
这将为每个隐式重命名回显TRUE
:
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$
这是在R中进行所有操作的更明确的演练:
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$
这篇关于在R中重命名文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!