Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to unzip multiple files into different folders (with different names)

    Hello everyone,
    I am new in this forum, and I hope you can help me solve this. What I have to do:

    - unzip multiples files (around 40 zipped files), into "separated" 40 folders (names of the folders remain as names of zipped files)
    - inside the folders after unzip are several csv files. Then I need to create new column (for remark) then append and merge them all into a big single data file.

    However, I guess I dont know how to specify the folder path, so that when I do the unzipfile, it does not automatically create separate folders (with folders name = name of zip files)
    This is my current code when I try to unzip multiple files into separated folders, which did not work:

    HTML Code:
    local sourcedir "\filezip"
    local unzipdir "filezip\temp"
    cap noi mkdir "`unzipdir'"
    local fls: dir "`sourcedir'" files "*.zip"
    cd "`unzipdir'"
    foreach f in `fls' {
        di "Working on `f'"
        unzipfile "`sourcedir'/`f'", replace
    }
    Hope that someone can help me with this. I guess when I know how to specify the folder path (maybe this is the issue), I can continue with the append/merge files
    Many thanks

  • #2
    Welcome to Statalist.

    Are you sure that is the full path in line 1 and line 2? Usually for Window, it should start with something like "C:/" and for Mac it should be "/User"... these current paths seem incomplete. It'd also be helpful if you can post the errors you got. Does it say something like whatever directory not found?

    Comment


    • #3
      Im so sorry I was not clear before.

      So here comes the full path
      Code:
      local sourcedir "C:\LuciaWork\DATA\filezip"
      local unzipdir "C:\LuciaWork\DATA\filezip\temp"
      In fact, the code run without error, but the result is not what I want.

      From the zipfile, lets call BIGZIP >> we have around 40 zipfiles insides (called ZIP1,....,ZIP40) >>> inside each ZIP1,...,ZIP40 there are several CSV files of similar name: year2010, year 2011, year2012,..., year2019. These CSV files have same name, but comes from different folder ZIP1, ZIP2,...,ZIP40
      So from the code, when unzip the CSV files, the loop over-ran each other. So I got 10 CSV files from year2010 to year2019.
      But I expect to have year2010...year2019 in each separated folders of ZIP1, ZIP2, ..., ZIP40

      My final outcome is that I should be able to make some changes in each csv files for example creating new column to specific this is from ZIP1, this is from ZIP2... (which I guess later on I need to convert them all into .dta), and append/merge them into one big file

      Comment


      • #4
        From the output of help unzipfile we read
        unzipfile extracts files and directories from a file in zip archive format into the current directory.
        without saying that it creates a subdirectory to contain the extracted contents, so you are going to need to create a suitably named output directory for each zip archive. The following untested modification of your loop may start you in a useful direction.
        Code:
        foreach f of local fls {
            di "Working on `f'"
            local zipname : subinstr local f ".zip" ""
            cap noi mkdir "`unzipdir'/`zipname'"
            cd "`unzipdir'/`zipname'"
            unzipfile "`sourcedir'/`f'", replace
        }
        Crossed with post #3, so this addresses my understanding of post #1. Similar changes should solve the problem more fully described in post #3.
        Last edited by William Lisowski; 23 Nov 2021, 08:29.

        Comment

        Working...
        X