Announcement

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

  • Renaming file names in a loop

    Hi all,

    i have three string variables called
    dirname (string containing the path of subfolder (having in mind that cd is correctly set and these paths are set ok wrt cd), for example "DOCS/folder1/folder11"),
    filename (string containing the name of the file in the subfolder, with its extension, for example "Book.docx")
    newfilename (string containing new name of the file, for example "Newnamebook.docx")

    i want to rename all files that are in my database with a loop:

    Code:
    qui count
    forvalues i = 1(1)`r(N)' {
        
        levelsof filename in `i'/`i', local(filen) 
        levelsof newfilename in `i'/`i', local(newfilen)
        levelsof dirname in `i'/`i', local(dirn) 
       
       !rename `dirn'"/"`filen'`dirn'"/"`newfilen'
    }
    Does anybody see what is the error here because it is not working?
    Thanks!

  • #2
    I think the quotation marks in the -!rename- command are wrong. I also think the use of levelsof to isolate a single value is like using a sledge hammer to swat flies. I would do this as:

    Code:
    forvalues i = 1/`r(N)' {
        local filename = filename[`i']
        local newfilename = newfilename[`i']
        local dirname = dirname[`i']
        !rename "`dirname'\\`filename'" "`newfilename'"
    }
    You don't say which OS you are running on. The code above was written for my Windows setup, which use \ as the path separator. (This then requires using \\ in the -!rename- command to avoid interfering with the parsing of `filename'.) If you are running on Mac or Linux, then replace \\ with just /, the path separator in those OS's.

    Comment


    • #3
      Thanks a lot! I tried it again but for some reason it doesn't work for me, but using your code and trying a little bit more i realized only way it works for me is when i refer to the file directly without path, like:

      Code:
      cd "C:\Users\...\Desktop"
      
      local pwd "`c(pwd)'"
       
      filelist, dir("something")
      
      keep if strpos(filename, "___")
      
      gen newfilename = subinstr(filename, "___", "_",.)
      
      
      qui count
      forvalues i = 1/`r(N)' {
          local filename = filename[`i']
          local newfilename = newfilename[`i']
          local dirname = dirname[`i']
          cd "`dirname'"
          !rename "`filename'" "`newfilename'"
          cd "`pwd'"
      
      }
      Here is the full code that i want to use in order to rename files with specific characters in some folder and all of its sub-folders. I work on windows. But i would still like to understand why your code is not working for me, is it again some issue in the referring to the locals in
      Code:
      !rename
      command? Because changing cd in the loop like this doesn't look like nice solution as well...


      Comment


      • #4
        Well, your workaround is a good one, in my opinion.

        But i would still like to understand why your code is not working for me,
        So would I! I test that code in my own setup before I posted it, and it ran just fine. Sorry, but I have no idea why it doesn't work on yours.

        Comment

        Working...
        X