如何根据文件名中的日期选择目录中的文件并将其复制到另一个文件夹

时间:2019-07-18 01:24:21

标签: r

我在文件夹(tempfiles1)中有一组具有以下名称的文件:

ASC05012019R.DBF
ASC05012019R.NTX
ASC05012019H.DBF
ASC05012019H.NTX
ASC05012019F.DBF
ASC05012019F.NTX
ROS12012019R.DBF
ROS12012019R.NTX
ROS12012019H.DBF
ROS12012019H.NTX
ROS12012019F.DBF
ROS12012019F.NTX
BAL25012019R.DBF
BAL25012019R.NTX
BAL25012019H.DBF
BAL25012019H.NTX
BAL25012019F.DBF
BAL25012019F.NTX
ROK20012019R.DBF
ROK20012019R.NTX
ROK20012019H.DBF
ROK20012019H.NTX
ROK20012019F.DBF
ROK20012019F.NTX

每个文件名都有3个不同的字母开头,但所有字母后均带有ddmmyyyy格式的日期。

文件夹中还有其他文件(如上所示),如R.NTX,H.NTX或F.NTX等,但是我只在寻找扩展名为“ R.DBF”,“ H.DBF”的文件”和“ F.DBF”。

我希望从一个日期范围(例如05012019到22012019)中进行选择,然后将所有R.DBF,H.DBF和F.DBF文件复制到另一个文件夹(tempfiles2)。

我已经能够指定我的文件夹:

current_folder <- "G:/m/HR/tempfiles1"
new_folder <- "G:/m/HR/tempfiles2"

并从每个文件名中提取日期:

list_of_files <- substr(list.files(current_folder, ".DBF"),4,11) 
list_of_files <- as.Date(list_of_files, format= "%d%m%Y")

但是这里就是我被困住的地方。我尝试使用模式,但这返回了无效的模式参数错误:

list_of_files1 <- list.files(current_folder, pattern = 2019-01-05)

另外,这只会给我一个约会。

我可以使用file.copy复制文件,就像这样:

file.copy(file.path(current_folder,list_of_files), new_folder)

但是我不知道如何选择日期。

上面示例使用05012019到22012019之间的日期的最终结果是将正确的文件复制到tempfiles2文件夹:

ASC05012019R.DBF
ASC05012019H.DBF
ASC05012019F.DBF
ROS12012019R.DBF    
ROS12012019H.DBF    
ROS12012019F.DBF       
ROK20012019R.DBF    
ROK20012019H.DBF    
ROK20012019F.DBF    

2 个答案:

答案 0 :(得分:2)

这是一种方法:

# make some files to work with
files <- c("ASC05012019R.DBF", "ASC05012019R.NTX", "ASC05012019H.DBF", 
           "ASC05012019H.NTX", "ASC05012019F.DBF", "ASC05012019F.NTX", "ROS12012019R.DBF", 
           "ROS12012019R.NTX", "ROS12012019H.DBF", "ROS12012019H.NTX", "ROS12012019F.DBF", 
           "ROS12012019F.NTX", "BAL25012019R.DBF", "BAL25012019R.NTX", "BAL25012019H.DBF", 
           "BAL25012019H.NTX", "BAL25012019F.DBF", "BAL25012019F.NTX", "ROK20012019R.DBF", 
           "ROK20012019R.NTX", "ROK20012019H.DBF", "ROK20012019H.NTX", "ROK20012019F.DBF", 
           "ROK20012019F.NTX")
dir.create('temp')
setwd('temp')
file.create(files)
#>  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
#> [15] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

# use a set in a regex pattern to get files with the right ending
files <- list.files(pattern = '[RHF].DBF$')
files
#>  [1] "ASC05012019F.DBF" "ASC05012019H.DBF" "ASC05012019R.DBF"
#>  [4] "BAL25012019F.DBF" "BAL25012019H.DBF" "BAL25012019R.DBF"
#>  [7] "ROK20012019F.DBF" "ROK20012019H.DBF" "ROK20012019R.DBF"
#> [10] "ROS12012019F.DBF" "ROS12012019H.DBF" "ROS12012019R.DBF"

# extract and parse the dates
file_dates <- as.Date(sub('\\D+(\\d+).*', '\\1', files), '%d%m%Y')
file_dates
#>  [1] "2019-01-05" "2019-01-05" "2019-01-05" "2019-01-25" "2019-01-25"
#>  [6] "2019-01-25" "2019-01-20" "2019-01-20" "2019-01-20" "2019-01-12"
#> [11] "2019-01-12" "2019-01-12"

# subset based on the dates
wanted_files <- files[file_dates > as.Date('2019-01-05') & file_dates < as.Date('2019-01-22')]
wanted_files
#> [1] "ROK20012019F.DBF" "ROK20012019H.DBF" "ROK20012019R.DBF"
#> [4] "ROS12012019F.DBF" "ROS12012019H.DBF" "ROS12012019R.DBF"

# make a new directory
new_dir <- 'temp2'
dir.create(new_dir)

# move the files you care about
file.rename(wanted_files, file.path(new_dir, wanted_files))
#> [1] TRUE TRUE TRUE TRUE TRUE TRUE

# check that they're there
list.files(new_dir)
#> [1] "ROK20012019F.DBF" "ROK20012019H.DBF" "ROK20012019R.DBF"
#> [4] "ROS12012019F.DBF" "ROS12012019H.DBF" "ROS12012019R.DBF"

答案 1 :(得分:1)

尝试一下

#Define start and end date to select from files
start_range <- as.Date("05012019", format = "%d%m%Y")
end_range <- as.Date("22012019",  format = "%d%m%Y")

#Get full path of file names to copy
file_path <- list.files(current_folder, ".DBF", full.names = TRUE)

#Get date from the filenames to compare
list_date <- as.Date(substr(list.files(current_folder, ".DBF"),
              4,11), format= "%d%m%Y")

#Select the files which lie in the range of dates
files_to_copy <- file_path[list_date %in% seq(start_range, end_range, by = "1 day")]

#Copy the files
file.copy(files_to_copy, new_folder)