Announcement

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

  • Trouble renaming files using renfiles command

    I am using Stata 15.1 on a Mac computer. I am trying to batch rename files using the command "renfiles" (I would also like to use the related command, "mvfiles", which is not working either, but I'm guessing the reasons are related, so let's stick with renfiles).


    Here is my code:
    renfiles , folder("/Users/Gio/Documents/Research/Data/NHTSA/FARS/DBF Files/FARSDBF99/") match("PERSON.dbf") insign("PERSON") outsign("per1999")
    When I run the code, I get no errors. However, when I look in the directory where the file "PERSON.dbf" is located, there are no changes. What could be causing renfiles to fail?

    On a side note, !rename command (having difficulty finding information on how this command works) does not work either but does issue an error. Here is the code:
    !rename "/Users/Gio/Documents/Research/Data/NHTSA/FARS/DBF Files/FARSDBF99/PERSON.dbf" "/Users/Gio/Documents/Research/Data/NHTSA/FARS/DBF Files/FARSDBF99/PER.dbf"
    Here is the error:
    /bin/bash: rename: command not found

    Any help with this would be greatly appreciated. I just want to rename some files using Stata.

  • #2
    Let me take these in reverse order.

    This command
    Code:
    ! rename "/Users/Gio/Documents/Research/Data/NHTSA/FARS/DBF Files/FARSDBF99/PERSON.dbf" "/Users/Gio/Documents/Research/Data/NHTSA/FARS/DBF Files/FARSDBF99/PER.dbf"
    consists of the Stata shell command (the ! is another name for the shell command, see help shell for details), which causes Stata to hand whatever follows to the operating system to execute. In this case, "rename" is a Windows-specific command, and you received an error message from macOS suggesting this. On Unix-based operating systems, like linux and macOS, the command you want is "mv" rather than "rename". I expect that using
    Code:
    ! mv "/Users/Gio/Documents/Research/Data/NHTSA/FARS/DBF Files/FARSDBF99/PERSON.dbf" "/Users/Gio/Documents/Research/Data/NHTSA/FARS/DBF Files/FARSDBF99/PER.dbf"
    would work for you. Note: In the discussion above, I inserted a space following the ! to highlight the fact that there are two commands - one from you to Stata (!) and one from Stata to your operating system (rename or mv).

    With regard to the renfiles command, I installed it and reviewed the code, and it is specific to Windows. It will not work on linux or macOS. You could use the filelist command written by Robert Picard (from SSC, see ssc describe filelist for an installation link) to create a list of files, and then loop through them using the shell command to cause macOS to rename them or move them. My memory suggests there exists a tool that does this, but perhaps I'm only recalling Robert's examples written in response to other questions. Searching Statalist for findfiles may find sample code that gets you close to what you need.

    Comment


    • #3
      There's an undocumented undocumented (some undocumented functions are documented) Stata function that renames files (source). It's a pure file rename (will not move the file to a new directory) and requires Stata 14 and later.

      So you can use, as William has suggested, filelist (from SSC) to build a dataset of files to rename. Then, it's just a matter of looping over each file and renaming them. The following example scans all files in the "my_files" subdirectory of Stata's current directory (help cd) and loops over each to append a prefix to each file:

      Code:
      filelist, dir("my_files")
      
      local n = _N
      forvalues i=1/`n' {
          local dir = dirname
          local oldname = filename[`i'] 
          local newname = "pre_`oldname'"
          _renamefile "`dir'/`oldname'" "`dir'/`newname'"
      }
      Here's a sample run:
      Code:
      . dir my_files/*
      
      -rw-r--r--@ 1 robert  staff  236 Dec 14 09:59 my_files/file1.txt
      -rw-r--r--@ 1 robert  staff  236 Dec 14 09:59 my_files/file2.txt
      -rw-r--r--@ 1 robert  staff  236 Dec 14 09:59 my_files/file3.txt
      -rw-r--r--@ 1 robert  staff  236 Dec 14 09:59 my_files/file4.txt
      
      . do test2
      
      . filelist, dir("my_files")
      Number of files found = 4
      
      . 
      . local n = _N
      
      . forvalues i=1/`n' {
        2.         local dir = dirname
        3.         local oldname = filename[`i'] 
        4.         local newname = "pre_`oldname'"
        5.         _renamefile "`dir'/`oldname'" "`dir'/`newname'"
        6. }
      
      . 
      . 
      . exit
      
      end of do-file
      
      
      . dir my_files/*
      
      -rw-r--r--@ 1 robert  staff  236 Dec 14 09:59 my_files/pre_file1.txt
      -rw-r--r--@ 1 robert  staff  236 Dec 14 09:59 my_files/pre_file2.txt
      -rw-r--r--@ 1 robert  staff  236 Dec 14 09:59 my_files/pre_file3.txt
      -rw-r--r--@ 1 robert  staff  236 Dec 14 09:59 my_files/pre_file4.txt

      Comment

      Working...
      X