1

I have been attempting a q that aims to search for files in a directory that match specific search strings, retrieve information about those files (modification date and file size), store the information in a data frame, and save the data frame to a file.

These are the steps I had undergone while designing the function

  1. The initial check ensures that the variable search.strings is a non-empty character vector, meaning it contains a list of strings to search for.
  2. The list.files function is then used to retrieve a character vector containing the names of files and folders in a specific directory. Only the entries that match the first string in search.strings are included in the vector.
  3. Next, any directory names are removed from the vector obtained in the previous step, leaving only the file names.
  4. For each remaining entry in search.strings, if there is more than one entry, the grep function is used to filter and retain only the file names that contain that particular search string.
  5. After checking all the entries in search.strings, a vector of the desired file names is obtained. If no files remain in the vector, the function exits with a message indicating that there were no files matching all the search strings.
  6. If there are files that match all the search strings, a data frame named df is created. This data frame has three columns: the file names are placed in the first column, the last modification dates of the files are placed in the second column, and the file sizes in bytes are placed in the third column.
  7. The file.info function is used to retrieve the last modification date and file size for each file in df.
  8. The resulting data frame df is then written to a file located in a specified directory using the write.table function.
  9. Finally, the function returns the df data frame

This is my try at the Question

search_for_filenames_containing_multiple_patterns_and_output_file_info <- 
    function(directory = '.',search.strings = vector(mode = 'character')){
        if (length(search.strings)==0) {
            stop('Nothing to match with specified')
        }
        match1 <- list.files(directory,pattern = search.strings[1],
                   ignore.case = TRUE,full.names = FALSE)
        match1 <- match1[!dir.exists(paste(directory, "/", match1, sep = ""))]  
        if (length(search.strings)>1) {
            for (i in 2:length(search.strings)) {
                match1 <- grep(search.strings[i],match1,ignore.case = TRUE,value = TRUE)
        }
        if (length(match1)==0) {
            return('there were no files matchng all the entries of search.strings')
        }
        info <- file.info(match1)
        df1 <- data.frame(Name_file= match1, mtime = info[,4],
                         size = info[,1], stringsAsFactors = FALSE)
        outpfilename <- paste(directory, "/", "scrlisting.txt", sep = "")
        write.table(df1,outpfilename,append = FALSE,quote = FALSE,sep = '\t',row.names = FALSE,col.names = FALSE)
        return(df1)
        }
    }

The directory i am conducting my test on The problem i am facing is that when i input '1' as a argument in the search.string argument the function doesnt return anything. I am unable to understand what is causing this problem as when i run the code blocks indivisually i am getting the right output.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • Welcome to Stack Overflow! I think this is a good attempt at the problem, but this problem is more complicated than most question presented on SO. If you don't get a good response, consider closing this question and submitting a question that is more focused on just the "when i input '1' as a argument in the search.string argument the function doesnt return anything" part. – wibeasley May 28 '23 at 20:04
  • Are you familiar with the [fs](https://fs.r-lib.org/) package? Especially the `dir_ls()` & `dir_map()` functions? I'd start there. Also note that [`readr::read_csv()`](https://readr.tidyverse.org/reference/read_delim.html) can accept a *vector* of paths and return a single data.frame. – wibeasley May 28 '23 at 20:08

0 Answers0