dateFromSpotifyFile <- function(f, origin=.origin) {
  if (!length(f))
    return(as.Date(integer(), origin=origin))
  # if (length(f) > 1)
  #   return(as.Date(sapply(f, dateFromSpotifyFile, origin=origin)))

  ## Extract the date
  pat.date <- "(20\\d{2}-\\d{1,2}(-\\d{1,2})?|20\\d{1,6})"
  date <- stringr::str_extract(basename(f), pat.date)

  ## Some files (one so far) have "Oct2015" style date formatting
  pat.abb <- month.abb %>% {c(., tolower(.), toupper(.))} %>% regOr %>% sprintf("%s20\\d{2}", .)
  date.abb <- stringr::str_extract(basename(f), pat.abb)
  # date.abb[c(5, 7, 11, 13)] <- c("dec2012", "Dec12", "2011oct", "2099 oct")
  for (i in seq_along(month.abb)) {
    mabb <- month.abb[[i]]
    wh.dates <- grep(mabb, date.abb, ignore.case=TRUE)
    if (length(wh.dates)) {
      date.abb[wh.dates] <- removeText(pat=mabb, date.abb[wh.dates], ignore.case=TRUE)
      wh.twodigs <- (nchar(date.abb[wh.dates]) == 2 & grepl("\\d{2}", date.abb[wh.dates]))
      date.abb[wh.dates[wh.twodigs]] %<>% paste0("20", .)
      date.abb[wh.dates] <- paste0(date.abb[wh.dates], "-", i)
    }
  }

  ## Identify which of `date` should be replaced with the date.abb version
  ## Keep in mind that `date` will still capture the '2015' of 'Oct2015', 
  ##   but `date.abb` will have the more complete '2015-10'
  wh.to_replace <- !is.na(date.abb) & (is.na(date) | nchar(date) <= 4)
  unclear <- (!wh.to_replace & !is.na(date.abb))
  if (any(unclear))
    warning("Date is unclear (both an abb date and a regular date found) for file(s):\n\t", pasteC(basename(f)[unclear], C="\n\t"))
  ## REPLACE
  if (any(wh.to_replace))
    date[wh.to_replace] <- date.abb[wh.to_replace]


  ## Confirm that there is no digit in the file names of those which we could not extract a date for
  wh.na <- is.na(date)
  wh.has_digs <- grepl("\\d", basename(f)) ## CHANGED WITH FOLDER ISSUE 2016-02-16. PREVIOUSLY:   # wh.has_digs <- grepl("\\d", basename(f[wh.na]))
  if (any(wh.has_digs & wh.na))
    warning("Could not find dates for file(s):\n\t", pasteC(basename(f)[wh.has_digs & wh.na], C="\n\t"))

  ##  most dates are formatted without dash (ie, '20147' or '2014731'), but some dates do have dashes
  ##  we need to add dashes.  Firt remove from those present (easier than filtering and treating differently)

  ## remove dashes
  date <- gsub("-", "", date)
  ## split into date-parts
  n <- strsplit(gsub("(^20\\d{2})", "\\1-", date), "-")
  ## return the conversion
  as.Date(
     sapply(n, function(x) 
              if (length(x) == 3) {
                warning ("not sure how to convert date with three parts:")
                print(n)
                NA
              } else if (length(x) != 2) {
                  NA 
              } else {
                Y <- x[[1]]
                m <- substr(x[[2]], 1, 2)
                d <- substr(x[[2]], 3, 4)
                if (d == "")
                  d <- "01"
                sprintf(fmt="%s-%s-%s", Y, m, d)
             })
   , format="%Y-%m-%d"
   , origin=origin
  )
}


findSpotFileByDate <- function(files.list, date, full=FALSE, fl.nm=substitute(files.list), showWarnings=TRUE, ignoreThese=c("allFileGroups", "fullFilePaths")) {
  if (is.Date(files.list) || all(grepl("^\\d", files.list) & !grepl("[A-Za-z]", files.list))) {
    date <- files.list
    files.list <- NULL
  }

  if (missing(files.list) || is.null(files.list)) {
    files.list <- setdiff(lsos(type="datestamped.filenames")$Name, ignoreThese)
    selfname_(files.list)
    ret <- lapply(files.list, function(fl) findSpotFileByDate(files=get(fl), date=date, fl.nm=fl, showWarnings=FALSE))
    ret <- ret[sapply(ret, length) > 0]
    if (showWarnings && !length(ret))
      warning("Could not find the given date(s) in any datestamped.filenames object")
    return(ret)
  }

  date <- as.Date(date, origin=.origin)

  file.dates <- dateFromSpotifyFile(files.list)
  
  if (!full)
    files.list <- basename(files.list)

  inds <- file.dates %in% date
  if (!any(inds) && showWarnings) {
    fl.nm <- capture.output(fl.nm)
    warning ("No files found matching the given date(s)")
  }

  files.list[inds]
}
