Announcement

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

  • Renaming Files in Different Directories

    Hi,

    I am trying to rename files located in different directories using Stata 12.1. I've looked at previous posts about renaming files and tried to apply those here, and my code seems to run. However, the names of my files do not change in the directory. The blue screen pops up so quickly after the !rename command that I can't tell what the error is.

    I would like my code to (1) determine all the folders in a particular directory (2) look through each folder and compile all the files located within that folder (3) rename the files using the name of the folder within the new name. Examples of file names, folder names, and directories include: IMG_5996.jpg (file name), "All Lab Tests" (folder name), and TZA_01 Mark Laboratory (directory). There are instances where a folder will also have subfolders, but I haven't yet coded that part given the issues I'm having here.

    Code:
    global main "G:/Tanzania Data/"
    global FAC f1 f2 f3 f4 f5 f6 f7 f8 f9 f10
    local tza 01 03 05 06 07 08 09 10 11 12 15
    
    local num: word count `tza'
    forvalues i = 1/`num' {
     local j: word `i' of `tza'
     local k: word `i' of ${FAC}
     
     local mydirs: dir "${main}TZA`j'_${`k'}/Photos" dirs "*"
     foreach dir of local mydirs {
      local path "${main}TZA`j'_${`k'}/Photos/`dir'"
      local myfiles: dir "`path'" files "*.jpg"
      local n = 1
      foreach file of local myfiles {
       local name = trim("`dir'")
       local subfile = "TZA`j'_`name'_`n'.jpg"
       !rename "`path'/`file'" "`subfile'"
       local n = `n' + 1
      }
     }
    }
    Thanks in advance,
    Simone

  • #2
    Have you tried replacing
    Code:
    !rename "`path'/`file'" "`subfile'"
    with something like
    Code:
    display `" !rename "`path'/`file'" "`subfile'" "'
    to confirm the generated code? And then perhaps typing one of the generated commands at a Windows command prompt (sorry, my MS Windows terminology is weak) to see what happens?

    Comment


    • #3
      You can use filelist (from SSC) to build-up a list of all the files in a directory. By default, the command navigates subdirectories recursively. To install, type

      Code:
      ssc install filelist
      in Stata's command window.

      Once you have a list in memory, then all you have to do is to loop over each observation and issue the correct Windows shell command to rename each file.

      Comment


      • #4
        Thanks for the suggestions. I didn't know about filelist, so that was quite informative. There seems to be a bigger issue of the communication between Stata and DOS. Has anyone ever encountered this?

        Comment


        • #5
          I can't speak to issues of DOS/Windows but you can achieve what you want by using Stata commands only (with of course the help of filelist (from SSC)). Here's a short example that will create a directory called "main" within Stata's current directory (help cd). The "main" directory will contain several sub-directories and those will contain a number of text files.

          Code:
          * create files across several directories within a main directory
          mkdir "main"
          forvalues i = 1/5 {
              mkdir "main/d`i'"
              forvalues j = 1/5 {
                  clear
                  qui set obs 1
                  gen s = "this is file `j'"
                  outfile using "main/d`i'/f`j'.txt"
              }
          }
          
          * make a list of all files in the "main" directory
          filelist , dir("main")
          
          * copy each file using a new name and then delete the original
          local obs = _N
          forvalues i = 1/`obs' {
              local source = dirname[`i'] + "/" + filename[`i']
              local destination = dirname[`i'] + "/new_" + filename[`i']
              copy "`source'" "`destination'"
              rm "`source'"
          }

          Comment


          • #6
            Sorry for the late reply. I changed to use copy and that works perfectly. Rename was still behaving a bit unstable.

            Comment

            Working...
            X