[英]Rename several files in windows folder with R
I have a folder in windows with 1000 files with ".dts" format like 20170217.dts . 我在Windows中有一个文件夹,其中包含1000个文件,格式为“ .dts”,如20170217.dts 。 I want to rename all the file's name and change the year to 2019 , like 20190217.dts .
我想重命名所有文件的名称,并将年份更改为2019,例如20190217.dts 。 My aim is properly rename all files in the folder to be able to open them in another software.
我的目标是正确重命名文件夹中的所有文件,以便能够在其他软件中打开它们。 I tried this:
我尝试了这个:
# define file path
filepath <- "C:/my file path/"
myfile_dts_2019 <- list.files(path = filepath , pattern =".dts")
# print
myfile_dts_2019
[1] "20172303.dts" "20172403.dts"
# replace 2017 with 2019
newfiles <- stringr::str_replace(myfile_dts_2019,"2017","2019")
newfiles
[1] "20192303.dts" "20192403.dts"
# try to rename on the folder
file.rename(myfile_dts_2019, newfiles)
[1] TRUE TRUE
My script seems working in R, however the original files in the folder still have a name start with 2017 . 我的脚本似乎可以在R中运行,但是文件夹中的原始文件仍然具有以2017开头的名称。 I am wondering how can I rename them inside the windows folder as well.
我想知道如何在Windows文件夹中重命名它们。
list.files
have default full.names = FALSE
which means it only returns file name and not the path of the file. list.files
具有默认的full.names = FALSE
,这意味着它仅返回文件名,而不返回文件路径。 To rename the file we need the complete path, add full.names = TRUE
and then rename. 要重命名文件,我们需要完整的路径,添加
full.names = TRUE
,然后重命名。
myfile_dts_2019 <- list.files(path = filepath , pattern =".dts", full.names = TRUE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.