R: How to Select Files in Directory Which Satisfy Conditions Both on the Beginning and End of Name

How to select files in directory according to date in filename and copy to another folder

Try this

#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)

R list files with multiple conditions

 Filter(function(x) grepl("USD", x), file.ls)

alternatively, you could construct a regular expression for pattern that only matches filenames containing both strings, but that's a wizard's game.

Reading a file without mentioning its specific file name in R

Maybe a better approach would be to read all file names in the directory, find the match, and then read that complete name.

tmp=list.files()
tmp=tmp[grep("^W",tmp)]
read.table(tmp,...)

You better pray that there is only 1 match.

List files starting with a specific character

We need to use the metacharacter (^) to specify the start of the string followed by the number 5. So, it can a more specific pattern like below

list.files(pattern ="^5[0-9]*_[0-9]*name.Rdata")

Or concise if we are not concerned about the _ and other numbers following it.

list.files(pattern = "^5.*name.Rdata")

How to update a data frame using R?

in that case i think the command would look something like this:

list_of_new_maps <- list.files(pattern="*Aug 26 2020.csv") # there are probably _ so it would be Aug_26_2020.csv (i assume). 

if there are multiple new files you have to discern the changes between them but the idea stays the same.

this might be helpful:

R: How to select files in directory which satisfy conditions both on the beginning and end of name?

EDIT

this might be helpful insofar as you can always just take the last result to continue

z<-for (i in 1:30) { # here month length would be more suitable to account for the variation 28/29-31
if (i<10) {

new_stuff<-paste0(".*202010",i,".csv$") # for days from 1-9 here it would be january 2020 -> 2020_1_01-09. to be more inclusive of changing months you might have to tick up that part of the name as well
csv_list<-list.files(pattern = new_stuff)
print(csv_list)
}
else{

new_stuff<-paste0(".*20201",i,".csv$") # for days 10-28/31
csv_list<-list.files(pattern = new_stuff)
print(csv_list)
}

}
z

z$csv_list[[length(z$csv_list)]] #last entry / newest input to list

Select files in a directory from vector

The argument you're passing to pattern = is where things are going wrong I believe. This three-step approach might get you the desired result:

# Extract all .rds files
list <- list.files("Data/", pattern =".rds", full.names = TRUE)

# Define pattern for grepl
files <- c(20388, 20389, 20390)
pattern <- paste(files, sep="", collapse="|")

# Results in
pattern
[1] "20388|20389|20390" # grepl will interpret "|" as "or"

# Now we can subset list with the following
list[grepl(pattern,list)]

Pattern for file path

you can add ".+" to "myfile" to say one or more any character. Be carefull the dot need to be escaped with \\, if not it means any character

list.files(path=path, pattern="myfile_.+\\.csv")

should work

List files with multiple conditions

Try this:

list.files(path, recursive = TRUE, full.names = FALSE, 
pattern = "B0[2348].jp2$")

The pattern accepts a regular expression.



Related Topics



Leave a reply



Submit