Comparing input and output files

When running jobs on multiple files, sometimes you might find that files are missed. To ensure that you process ALL files, you can compare your total input files with those already processed using grep. Here’s an example of implementing this in R. You could couple this with a while loop for a script, making sure the script repeats until input files meet output files… This example assumes that your output files retain the original input file with the addition of an extension (_output.csv) which we first have to remove.

# Create a list of unprocessed files by comparing input and exisitng output files

# list of files e.g. Sys.glob(paste0(path, "*.csv")

# list of files e.g. Sys.glob(paste0(opath, "*.csv") with an extension 

# Remove extension of output files so they match the original file pattern
fs_processed_test=paste0(strsplit(fs_processed_test, "_output.csv"), ".csv")

	missing_files=setdiff(fs_original_basename, fs_processed_test)


# Create vector of things to match (e.g. processe file names - extensions have been removed)

# Returns matching string value (in this case, the file name)
# taken from:
matches_test <- unique (grep(paste(toMatch_test,collapse="|"), 
                        fs_original_test, value=TRUE))

# Returns numerical index position of matching string in vector
# matches <- unique (grep(paste(toMatch,collapse="|"), 
#                        fs_original, value=FALSE))

# subset original_fs to exclude any matches

print(paste0(length(to_process), " files still to process..."))
Written on December 6, 2017