Re: [D-RUG]: how to check the existence of a local file based on a date?

10 views
Skip to first unread message
Message has been deleted

Michael Hannon

unread,
Jan 6, 2017, 12:51:24 AM1/6/17
to davi...@googlegroups.com
IIUC, you just need to pull the date string out of the file names. If
that's the case, you could do so with a regular expression. Then you
can compare the two sets of dates in various ways ("setdiff" being one
of them). Have a look at the appended.

-- Mike

> dir1 <- list.files("tmp/dir1", "*.envi", full.names=FALSE)
> dir1
[1] "fM_df_20100101T000000_20100101T235959_200_0001.envi"
[2] "fM_df_20100102T000000_20100102T235959_200_0001.envi"
[3] "fM_df_20100103T000000_20100101T235959_200_0001.envi"

> dir2 <- list.files("tmp/dir2", "*.envi", full.names=FALSE)
> dir2
[1] "fM_fg_20100101T000000_20100101T235959_300_0001_7.envi"
[2] "fM_fg_20100102T000000_20100102T235959_300_0001_7.envi"
[3] "fM_fg_20100104T000000_20100102T235959_300_0001_7.envi"

> dates1 <- gsub("^[^0-9]+([0-9]+).*$", "\\1", dir1)
> dates2 <- gsub("^[^0-9]+([0-9]+).*$", "\\1", dir2)

> setdiff(dates1, dates2)
[1] "20100103"
>

On Thu, Jan 5, 2017 at 3:05 AM, bic ton <bict...@gmail.com> wrote:
> Hi all,
>
> happy new year 2017!
>
> I have two directories with some files for two years(2010/01/01-2011/12/31).
> The files are named like this:
>
> dir1:
> fM_df_20100101T000000_20100101T235959_200_0001.envi
> fM_df_20100102T000000_20100102T235959_200_0001.envi
> etc
> (only the date change)
>
> dir2:
> fM_fg_20100101T000000_20100101T235959_300_0001_7.envi
> fM_fg_20100102T000000_20100102T235959_300_0001_7.envi
> etc
> (only the date change)
>
> dir1<- list.files ("dir1", "*.envi", full.names = TRUE)
> dir1=dir1[1]
> dir2<- list.files ("dir2", "*.envi", full.names = TRUE)
> dir1=dir1[1]
>
> now do things:
>
> if(!file.exists){............
>
> for (i in seq_along(dir1)) {
>
> r1=raster(dir1[1])
>
> r2=raster(dir2[1])}
>
>
> }
>
> The problem is some files (dates) are missing but I do not know which ones.
> What I need is
>
> check if file with date 20100101 exist in both dir1 and dir2 (based on the
> file names), do the work
>
> ..........
>
> otherwise skip and look fo 20100102.... and so on until 20111231.
>
>
> Any hints on this please?
>
> Thanks in advance,
>
> Bic
>
>
> --
> Check out our R resources at http://d-rug.github.io/
> ---
> You received this message because you are subscribed to the Google Groups
> "Davis R Users' Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to davis-rug+...@googlegroups.com.
> Visit this group at https://groups.google.com/group/davis-rug.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages