1

I've got a lot of files in a folder, many of them are empty and others with data inside.

What I'm trying to do is this:

#Load all data in a list
file_all <- list.files(file.path(getwd(), "testall"), pattern = "\\.txt$") 

Using this list, I'm trying to skip empty files using the method explained by @nrussell How to skip empty files when importing text files in R?

library(plyr)
df_list <- lapply(files, function(x) {
    if (!file.size(x) == 0) {
        list.files(x)
    }
})

And (not empty files)

    df_list2 <- lapply(files, function(x) {
    if (file.size(x) == 0) {
        list.files(x)
    }
})

The difference between @nrussell and mine is that I want to create a list of empty files and another list with not empty files. I'd like to know how many files are empty and how many are not empty.

Community
  • 1
  • 1
Enrique
  • 842
  • 1
  • 9
  • 21
  • Why don't you store the files in two different vectors using an `apply` function. – Eli Sadoff Nov 03 '16 at 14:09
  • @EliSadoff that was a mistake, sorry. I'm storing in two different vectors. – Enrique Nov 03 '16 at 14:13
  • Do this: list.of.files <- file.info(dir()) sizes <- file.info(dir())$size list.of.non.empty.files <- rownames(list.of.files)[which(sizes != 0)] And then of course you have to read in from the list of non empty files list.of.empty.files <- rownames(list.of.files)[which(sizes == 0)] – USER_1 Nov 03 '16 at 14:17
  • Please show current and desired results. Specifically, show why your current attempt does not work? Errors? Undesired output? Illustrate with actual data. – Parfait Nov 03 '16 at 14:28

1 Answers1

3
# create a list of files in the current working directory
list.of.files <- file.info(dir())

# get the size for each file
sizes <- file.info(dir())$size

# subset the files that have non-zero size
list.of.non.empty.files <- rownames(list.of.files)[which(sizes != 0)]

# here you can further subset that list by file name, eg - if I only want all the files with extension .mp3
list.of.non.empty.files[grep( ".mp3", list.of.non.empty.files)]


# get the empty files
list.of.empty.files <- rownames(list.of.files)[which(sizes == 0)]
USER_1
  • 2,409
  • 1
  • 28
  • 28