我正在尝试使用R根据给定的日期顺序动态地创建文件夹。如果没有具有当前日期的文件夹,则从date_1开始。因此,例如,如果今天创建了一个新文件夹,则初始结果将如下所示:
/2020-05-15_1
然后,随后的文件夹将如下所示:/2020-05-15_2
/2020-05-15_3
等等想法是每次运行脚本时顺序地动态生成它们。
我一直使用sapply
,list.dirs
,sapply
和dir.create
的组合,但无法正常工作并且有点卡住。任何帮助将非常感激。谢谢!
以下根据您的指定创建文件夹。
# create today's date in format YYYY-MM-DD
today <- as.character(Sys.Date())
# create name of the first directory for today
first_today <- paste(today, 1, sep = "_")
# directories that exist in current working directory
dirs <- dir()
# check if the first directory already exists
if(first_today %in% dirs) {
# subset to today's directories
dirs_today <- dirs[grepl(today, dirs)]
# get the number of todays directories
n <- length(dirs_today)
# create the name for today's next directory
new_dir <- paste(today, n + 1, sep = "_")
dir.create(new_dir)
} else {
dir.create(first_today)
}