Search code examples
directoryrstudiounzip

How to automate the process of unzip steps in RStudio


I have downloaded the transportation history data. The data for each year contain the same numbers of files with exactly same name. Each year's data was zipped in a single files. I am trying to automate the process of unzipping.
for example: I have three zip files named (2014.zip, 2013.zip, 2012.zip) and each zip file contains three files(car.csv, truck.csv, train.csv). What I want is to unzip these files in their corresponding folders which will be created on the fly. How can I automate this process in RStudio? Thanks.


Solution

  • lapply(filenames, function(x)){
    foldername<-substr(filename, 1, nchar(filename)-4)
    
    if (file.exists(x)==FALSE){
        download.file(url, x)
    }
    
    if (file.exists(foldername)==FALSE){
        dir.create(foldername)
    }
    
    unzip(x)
    
    for (file in list.files(pattern="*.dbf")){
        file.copy(file,foldername)
    
        file.remove(file)
    }}