Announcement

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

  • unzipping files in different directories in an automatic way

    Why Stata does not let me unzip a file if I write the whole diretory, as below, on the command instead of using -global- or -chdir-?
    unzipfile "C:\Users\Paula\Pesquisa\DADOS\dados_georreferenci ados\Malha_Setor_Censitário_2000\setor_urbano\sp\3 500105.zip", replace

    Actually what I would like to do is something like that:
    foreach i in 3500105 3500303 3500709 {
    unzipfile "C:\Users\Paula\Pesquisa\DADOS\dados_georreferenci ados\Malha_Setor_Censitário_2000\setor_urbano\sp\` i'\`i'.zip", replace
    }

    Each zipfile has its own folder and I would like to unzip them all in an automatic way. The example above is for only 3 files, but I have more than a hundred, each of them in its own folder.

    Thank you!

  • #2
    You don't show us the actual response Stata gave you. Was there an error message? If so, what was it?

    That said, it is possible that these full pathnames, at 112 characters, are simply too long for your operating system to process.

    Comment


    • #3
      Oh, sorry! I do not know why the icon for inserting codes here desappeared!

      Stata says:
      . unzipfile "C:\Users\Paula\Pesquisa\DADOS\dados_georreferenci ados\Malha_Setor_Censitário_2000\setor_urbano\sp\3 500105.zip", r
      > eplace
      cannot open C:\Users\Paula\Pesquisa\DADOS\dados_georreferencia dos\Malha_Setor_Censitário_2000\setor_urbano\sp\35 00105.zip or C
      > :\Users\Paula\Pesquisa\DADOS\dados_georreferenciad os\Malha_Setor_Censitário_2000\setor_urbano\sp\350 0105.zip.zip
      could not perform unzip
      r(601);

      Comment


      • #4
        your local macros is not expanded because the single backslash is preventing macro substitution.

        Use "/" or add an extra backslash, see http://www.stata.com/support/faqs/pr...es-and-macros/
        Last edited by Bjarte Aagnes; 04 Nov 2015, 09:29.

        Comment


        • #5
          @Bjarte. You are correct that the single backslash will block macro substitution when Paula tries to run this in a loop. But in #3, she shows that even in a single command, with no loop and no macro substitution, she cannot get Stata to open the file. I think it is because the full pathname is too long for Windows. (She doesn't say she's running on Windows, but it looks like a Windows pathname.) Additional reason to think this is the problem is that she says in #1 that she can do it if she changes directory first.

          Comment


          • #6
            Originally posted by Paula de Souza Leao Spinola View Post
            Oh, sorry! I do not know why the icon for inserting codes here desappeared!
            Click the capital A in the upper right corner of the field where you are writing to toggle the advanced editor.

            Originally posted by Paula de Souza Leao Spinola View Post
            Stata says:
            . unzipfile "C:\Users\Paula\Pesquisa\DADOS\dados_georreferenci ados\Malha_Setor_Censitário_2000\setor_urbano\sp\3 500105.zip", r
            > eplace
            cannot open C:\Users\Paula\Pesquisa\DADOS\dados_georreferencia dos\Malha_Setor_Censitário_2000\setor_urbano\sp\35 00105.zip or C
            > :\Users\Paula\Pesquisa\DADOS\dados_georreferenciad os\Malha_Setor_Censitário_2000\setor_urbano\sp\350 0105.zip.zip
            could not perform unzip
            r(601);
            Is this exactly what you see in the Results window? The last file mentioned is called 3500105.zip.zip. I assume there is no file with that name.

            Can you confirm that all directories in the path are spelled correctly?

            You wrote in post #1 that you don't want to use cd (chdir is an outdated command). Did you try a loop with cd to unzip the files? If you change to the correct directory you can run unzipfile without the path.

            Comment


            • #7
              Paula, In your code in post #1 there is an extra space between the first left single quote and the macro name.
              This extra space will prevent the first substitution of the local macro (even if you used forward slash as a path delimiter) .

              Remove the extra space. Use double backslash before the locals, or use the forward slash as a path delimiter (see #4).

              (Remember that -unzipfile- extracts files into the current directory).
              Last edited by Bjarte Aagnes; 05 Nov 2015, 13:17.

              Comment


              • #8
                My I suggest that you substitute dir for unzipfile in whatever command is generating the error and then try again? The error message you are getting is consistent with the error message I get when the file specified does not exist.
                Code:
                . unzipfile "gnxl.zip"
                cannot open gnxl.zip or gnxl.zip.zip
                could not perform unzip
                r(601);
                
                . dir "gnxl.zip"
                
                ls: gnxl.zip: No such file or directory
                
                .
                There could be other causes for the error from unzipfile, but this should rule out typographical errors in the file name.

                Comment


                • #9
                  Originally posted by Bjarte Aagnes View Post
                  your local macros is not expanded because the single backslash is preventing macro substitution.

                  Use "/" or add an extra backslash, see http://www.stata.com/support/faqs/pr...es-and-macros/
                  Exactly the macro need to expand, even if you are using the global variable or the change director command. Following link explains the unziping process in detail and you can copy paste the code from there for you use.
                  https://thedatahall.com/how-to-unzip-files-in-stata/

                  Comment

                  Working...
                  X