Q:

Select CSV files and read in pairs

I am comparing two pairs of csv files each at a time. The files I have each end with a number like cars_file2.csv, Lorries_file3.csv, computers_file4.csv, phones_file5.csv. I have like 70 files per folder and the way I am comparing is, I compare cars_file2.csv and Lorries_file3.csv then  Lorries_file3.csv and 
computers_file4.csv, and the pattern is 2,3,3,4,4,5 like that. Is there a smart way I can handle this instead of manually coming back and change file like the way I am reading here or I can use the last number on each csv to read them smartly. NOTE the files have same suffixes _file:
library(daff)

setwd("path")

# Load csvs to compare into data frames
x_original <- read.csv("cars_file2.csv", strip.white=TRUE, stringsAsFactors = FALSE)

x_changed <- read.csv("Lorries_file3.csv", strip.white=TRUE, stringsAsFactors = FALSE)

render(diff_data(x_original,x_changed ,ignore_whitespace=TRUE,count_like_a_spreadsheet = FALSE))

My intention is to compare each two pairs of csv and recorded, Field additions, deletions and modified

A:

You may want to load all files at once and do your comparison with a full list of files.
This may help:
# your path
path <- "insert your path"

# get folders in this path
dir_data <- as.list(list.dirs(path))

# get all filenames
dir_data <- lapply(dir_data,function(x){

  # list of folders
  files <- list.files(x)
  files <- paste(x,files,sep="/")

  # only .csv files
  files <- files[substring(files,nchar(files)-3,nchar(files)) %in% ".csv"]

  # remove possible errors
  files <- files[!is.na(files)]

  # save if there are files
  if(length(files) >= 1){
    return(files)  
  }
})

# delete NULL-values
dir_data <- compact(dir_data)

# make it a named vector
dir_data <- unique(unlist(dir_data))
names(dir_data) <- sub(pattern = "(.*)\\..*$", replacement = "\\1", basename(dir_data))
names(dir_data) <- as.numeric(substring(names(dir_data),nchar(names(dir_data)),nchar(names(dir_data))))

# remove possible NULL-values
dir_data <- dir_data[!is.na(names(dir_data))]

# make it a list again
dir_data <- as.list(dir_data)

# load data
data_upload <- lapply(dir_data,function(x){
  if(file.exists(x)){
    data <- read.csv(x,header=T,sep=";")
  }else{
    data <- "file not found"
  }
  return(data)
})

# setup for comparison
diffs <- lapply(as.character(sort(as.numeric(names(data_upload)))),function(x){

  # check if the second dataset exists
  if(as.character(as.numeric(x)+1) %in% names(data_upload)){

    # first dataset
    print(data_upload[[x]])

    # second dataset
    print(data_upload[[as.character(as.numeric(x)+1)]])

    # do your operations here
    comparison <- render(diff_data(data_upload[[x]],
                     data_upload[[as.character(as.numeric(x)+1)]],
                     ignore_whitespace=T,count_like_a_spreadsheet = F))
    numbers <- c(x, as.numeric(x)+1)

    # save both the comparison data and the numbers of the datasets
    return(list(comparison,numbers))

  }
})

# you can find the differences here
diffs

This script loads all csv-files in a folder and its sub-folders and puts them into a list by their numbers. In case there are no doubles, this will work. If you have doubles, you will have to adjust the part where the vector is named so that you can index the full names of the files afterwards.

