[英]Renaming multiple files with multiple names
setwd("C:\\Users\\...\\Documents\\Main\\eml orders")
files <- list.files(pattern="*.eml")
newfiles <- gsub(".eml$", ".txt", files)
file.rename(files, newfiles)
eml_files <- list.files(pattern = "txt$")
我有將 .eml 轉換為 .txt 文件的代碼,現在我想將相同的文件重命名為我用 function 制作的字符串。
示例 function
fetch_date <- function(x) {
date <- paste0(as.character(Sys.time()), ".txt")
file.rename(x, date)
}
現在我嘗試map(eml_files, fetch_date)
並得到這個錯誤:
cannot rename file '24 New order placed.txt' to '2020-11-14', reason 'The network path was not found'
不知道發生了什么任何幫助將不勝感激。
請注意,您的fetch_date
function 僅輸出一個字符串( Sys.date()
)。 它嘗試用相同的名稱命名多個.txt
對象。 在我的 Mac 上,這會導致最后一個文件被保留而其他文件被覆蓋。 也許您使用 Windows 並且在覆蓋文件時還有另一種默認行為?
eml_files <- list.files(pattern = "txt$")
fetch_date <- function(x) {
date <- paste0(as.character(Sys.time()), ".txt")
file.rename(x, date)
}
map(eml_files, fetch_date)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.