0

I have a large number of files that have unique names and I need these files to be moved to a new file location based on whether they match the name in a dataframe.

For example, The file names would look like:

/users/documents/data/181009_153350-de-7-135_1.csv
/users/documents/data/123439_152450-de-5-134_1.csv 
/users/documents/data/181249_134329-de-7-131_1.csv 

The dataframe contains a column that has a portion of the file name (i.e. "181009_153350"). If the the "181009_153350" is matched from the dataframe to the filename it will move that file to a new location. The number can be quite varied and need to match the dataframe to be moved.

The dataframe would look like: dataframe

I was able to figure out how to move files when they have a pattern, but I couldn't figure out how to implement this when there is no pattern and matched.

I currently have this code to move the file based on the pattern:

File_move_funct <- function(Curr, New){
  library(filesstrings)
  current.folder <- Curr
  new.folder <- New
  list.of.files <- list.files(current.folder,pattern = "-de-", full.names = T, recursive = TRUE)
  list.of.files
  move_files(list.of.files, new.folder, overwrite = FALSE)
}

I began by trying to save a portion of the filename that I wanted as a dataframe itself to match the two dataframes. This was with the full pathname of the file to ensure I could transfer the file later.

files: /users/documents/data/181009_153350-de-7-135_1.csv <- (just an example, my filenames are longer) list <- as.data.frame(substr(list.of.files, 91,103))

Then, I tried match the values of one dataframe to the other, but couldn't get it to work.

compare2 <- function(df1, df2){
  n = nrow(df1); p = ncol(df1)
  result = matrix(NA,nrow = n, ncol = p)
  for(j in seq_len(p)){
    for(i in seq_len(n)){
    result[i,j] <- df1[i,j] == df2[i,j]
    }
  }
  print(result)
}

Overall, I just want to move a file if a specific portion of its filename matches the data within the column of a dataframe.

I don't think I am doing this efficiently or correctly. I have been trying for awhile and don't seem to be progressing.

  • Can you try to edit to make this more reproducible/understandable to people who don't have your data frames or you files? A concrete example with a few rows of sample data from your data frames along with an explanation of what exactly you want to do with those rows would help a lot. – Gregor Thomas Mar 14 '23 at 14:10
  • @GregorThomas Hopefully the additional information helps – TrentonS36 Mar 14 '23 at 15:02
  • As mentioned in the package readme file, ‘filesstrings’ is essentially made obsolete by ‘fs’. I would strongly recommend using the latter package instead! – Konrad Rudolph Mar 14 '23 at 15:05
  • @KonradRudolph I don't mind changing this, but its not my primary issue with what I am trying to do now. This was a code I did previously to address a different project. It just provided an easy way to move files that contain a pattern. Now I need a way that to move files that don't have a pattern and need to be matched. – TrentonS36 Mar 14 '23 at 15:14
  • If you want to do partial matching of strings, then regex functions will be useful like `grepl`. As-is, I do not know how to help you because your question is still vague. *"I just want to move a file if a specific portion of its filename matches the data within the column of a dataframe"* - I have no idea what "specific portion" you want to match, and you posted a picture of the data frame which is hard to work with to test solutions. Posting a relevant sample of the data frame as copy/pasteable text would be much nicer, like `dput(that_dataframe[1:5, ])` for the first 5 rows. – Gregor Thomas Mar 14 '23 at 15:48

0 Answers0