Announcement

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

  • Importing .zip files

    Dear Stata Users,

    I have 10 zip files that contain txt files with the same name ('xyz'). The names of the zip files are file1990, where the '1990' indicates a year. I want to unzip these files into a folder. I want to do smth like this (please the "pseudo-code").

    Code:
    forvalues x=1990(2)2020 {
        cd "C:\Users\Z1\Online Drivers\Projects\File"
        di "Imports `x'"
        
        unzipfile file`x'.zip, save(xyz`x'.txt)    using "C:\Users\Z1\Online Drivers\Projects\File_unzip"
        }
    Is it possible to modify the line of code starting with "unzipfile"?




  • #2
    Originally posted by Alberto Alvarez View Post
    Is it possible to modify the line of code starting with "unzipfile"?
    Modify in what sense? As you can see below, the loop covers all the for values specified.

    Code:
    forvalues x=1990(2)2020 {
        di `"unzipfile file`x'.zip, save(xyz`x'.txt)    using "C:\Users\Z1\Online Drivers\Projects\File_unzip""'
    }
    Res.:

    Code:
    . forvalues x=1990(2)2020 {
      2.
    .     di `"unzipfile file`x'.zip, save(xyz`x'.txt)    using "C:\Users\Z1\Online Drivers\Projects\File_unzip""'
      3.
    . }
    unzipfile file1990.zip, save(xyz1990.txt)    using "C:\Users\Z1\Online Drivers\Projects\File_unzip"
    unzipfile file1992.zip, save(xyz1992.txt)    using "C:\Users\Z1\Online Drivers\Projects\File_unzip"
    unzipfile file1994.zip, save(xyz1994.txt)    using "C:\Users\Z1\Online Drivers\Projects\File_unzip"
    unzipfile file1996.zip, save(xyz1996.txt)    using "C:\Users\Z1\Online Drivers\Projects\File_unzip"
    unzipfile file1998.zip, save(xyz1998.txt)    using "C:\Users\Z1\Online Drivers\Projects\File_unzip"
    unzipfile file2000.zip, save(xyz2000.txt)    using "C:\Users\Z1\Online Drivers\Projects\File_unzip"
    unzipfile file2002.zip, save(xyz2002.txt)    using "C:\Users\Z1\Online Drivers\Projects\File_unzip"
    unzipfile file2004.zip, save(xyz2004.txt)    using "C:\Users\Z1\Online Drivers\Projects\File_unzip"
    unzipfile file2006.zip, save(xyz2006.txt)    using "C:\Users\Z1\Online Drivers\Projects\File_unzip"
    unzipfile file2008.zip, save(xyz2008.txt)    using "C:\Users\Z1\Online Drivers\Projects\File_unzip"
    unzipfile file2010.zip, save(xyz2010.txt)    using "C:\Users\Z1\Online Drivers\Projects\File_unzip"
    unzipfile file2012.zip, save(xyz2012.txt)    using "C:\Users\Z1\Online Drivers\Projects\File_unzip"
    unzipfile file2014.zip, save(xyz2014.txt)    using "C:\Users\Z1\Online Drivers\Projects\File_unzip"
    unzipfile file2016.zip, save(xyz2016.txt)    using "C:\Users\Z1\Online Drivers\Projects\File_unzip"
    unzipfile file2018.zip, save(xyz2018.txt)    using "C:\Users\Z1\Online Drivers\Projects\File_unzip"
    unzipfile file2020.zip, save(xyz2020.txt)    using "C:\Users\Z1\Online Drivers\Projects\File_unzip"
    
    .

    Comment


    • #3
      The syntax used for the unzip command in post #1 is invented syntax intended to convey the following more direct statement of the problem, at least as I understand it.
      • The directory "C:\Users\Z1\Online Drivers\Projects\File" contains zip archives file1990.zip, find1992.zip, ..., file 2020.aip
      • Each zip archive contains a single file named xyz.txt
      • From the zip archive file1990.zip extract xyz.txt and store it in the directory "C:\Users\Z1\Online Drivers\Projects\File_unzip" with the name xyz1990.txt
      • And similarly for all the other zip archives.
      So the direct answer to the question posed in post #1

      Is it possible to modify the line of code starting with "unzipfile"?
      is no - there is no syntax for the unzip command that allows you to accomplish this with a single command. Perhaps the following untested code will start you in a useful direction.
      Code:
      local zips "C:\Users\Z1\Online Drivers\Projects\File"
      local unzips "C:\Users\Z1\Online Drivers\Projects\File_unzip"
      
      cd "`zips'"
      forvalues x=1990(2)2020 {
          unzipfile "`zips'\file`x'.zip", replace
          copy xyz.txt "`unzips'\txt`x'.txt"
      }
      erase xyz.txt
      Last edited by William Lisowski; 07 Sep 2022, 06:51.

      Comment


      • #4
        -unzipfile- is a Stata command, but that is not valid code. Perhaps something like this? (I haven't tested it; I don't have a Windows machine)

        Code:
        cd "C:/Users/Z1/Online Drivers/Projects/File_unzip"
        
        forvalues x=1990(2)2020 {
            di "Imports `x'"
            
            unzipfile "C:/Users/Z1/Online Drivers/Projects/File/file`x'.zip", replace
            shell ren xyz.txt xyz`x'.txt
            }
        Last edited by Hemanshu Kumar; 07 Sep 2022, 06:59.

        Comment

        Working...
        X