[R] Zip multiple files with same prefix?

arun smartpink111 at yahoo.com
Fri May 23 13:48:59 CEST 2014



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 <rkirkwo2 at uwyo.edu> 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.

______________________________________________
R-help at r-project.org 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.



More information about the R-help mailing list