Zip multiple files with same prefix?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Zip multiple files with same prefix?

mr_kirkwood
I have thousands of files that need to be zipped. The files that need to be zipped together have the same seven digit prefix e.g. 40111h1.

I am a novice at R-Studio and would appreciate some help on this matter.

Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: Zip multiple files with same prefix?

arun kirshna


Hi,

May be this helps:
#If you are okay with compressed tar files
##If the files are all in the working directory
 vec1 <-  list.files()
vec1
#[1] "40111h162n.csv"  "40111h1n49.txt"  "40111_h1n4.txt"  "40111h1n50.txt"
#[5] "40111h2mn52.csv"

lst1 <- split(vec1,substr(vec1,1,7))

#Create the subdirectory based on the first 7 alphanumeric prefix.
 sapply(file.path(getwd(),names(lst1)),dir.create)
#or
# sapply(seq_along(lst1),function(i) dir.create(file.path(getwd(), names(lst1[i])))
#move the files to the sub-directory
 lapply(seq_along(lst1), function(i) file.rename(lst1[[i]],paste(file.path(names(lst1[i])), lst1[[i]],sep="/")))
#[[1]]
#[1] TRUE
#
#[[2]]
#[1] TRUE TRUE TRUE
#
#[[3]]
#[1] TRUE
list.files(recursive=TRUE)
#[1] "40111h1/40111h162n.csv"  "40111h1/40111h1n49.txt"
#[3] "40111h1/40111h1n50.txt"  "40111h2/40111h2mn52.csv"
#[5] "40111_h/40111_h1n4.txt"

##
#creating compressed tar file

tarF <- paste(names(lst1),"tgz",sep=".")
subDir <- list.dirs(full.names=FALSE)[-1]
 sapply(seq_along(tarF),function(i) tar(tarF[i],file.path(subDir)[i], compression='gzip'))
#[1] 0 0 0

list.files(recursive=TRUE)
#[1] "40111h1/40111h162n.csv"  "40111h1/40111h1n49.txt"
#[3] "40111h1/40111h1n50.txt"  "40111h1.tgz"           
#[5] "40111h2/40111h2mn52.csv" "40111h2.tgz"           
#[7] "40111_h/40111_h1n4.txt"  "40111_h.tgz"    

A.K.




On Thursday, May 22, 2014 4:21 PM, mr_kirkwood <[hidden email]> wrote:
I have thousands of files that need to be zipped. The files that need to be
zipped together have the same seven digit prefix e.g. 40111h1.

I am a novice at R-Studio and would appreciate some help on this matter.

Thanks!



--
View this message in context: http://r.789695.n4.nabble.com/Zip-multiple-files-with-same-prefix-tp4691088.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Zip multiple files with same prefix?

Ista Zahn
In reply to this post by mr_kirkwood
Something like

files <- list.files()
groups <- unique(substr(files, 1, 7))

for(goup in groups) {
      zip(paste0(group, ".zip"), grep(group, files, value=TRUE))
}

should get you started.

Best,
Ista

On Thu, May 22, 2014 at 3:34 PM, mr_kirkwood <[hidden email]> wrote:

> I have thousands of files that need to be zipped. The files that need to be
> zipped together have the same seven digit prefix e.g. 40111h1.
>
> I am a novice at R-Studio and would appreciate some help on this matter.
>
> Thanks!
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Zip-multiple-files-with-same-prefix-tp4691088.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.