Skip to content

Instantly share code, notes, and snippets.

@miroslavradojevic
Created February 10, 2018 19:38
Show Gist options
  • Save miroslavradojevic/ce0d36487b310d80a160f718aaa28bdc to your computer and use it in GitHub Desktop.
Save miroslavradojevic/ce0d36487b310d80a160f718aaa28bdc to your computer and use it in GitHub Desktop.
Script selects N random jpg unique files from given source directory and copies them to the given destination directory. Source dir needs to have at least N files if sampled withoud replacement.
rm(list=ls(all=TRUE))
N = 500;
dirSrc <- "/Users/miroslav/ndethcmml/ScSPM/image/patch/0"
dirDst <- "/Users/miroslav/ndethcmml/ScSPM/image/patch_1k_1/0"
# check if the directory exists and create one if not
dir.create(dirDst, recursive = T, showWarnings = F) # file.path(mainDir, subDir)
fSrcList <- list.files(dirSrc, pattern = "\\.jpg$", ignore.case = T, full.names = T)
# randomly sample N files without repetition
fSrcListSelected <- fSrcList[sample(1:length(fSrcList), N, replace = F)]
# clear the destination directory first
do.call(file.remove, list(list.files(dirDst, full.names = TRUE)))
# copy selected files to the destination directory
file.copy(fSrcListSelected, dirDst)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment