Announcement

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

  • Renaming a text file in Windows not working

    Hi all,

    A particular code that I've written for renaming a text file runs through without renaming the file.

    My goal is to take the file name "sally_AVT_1000_11_text.txt" (where "sally" represents a username that is between 5 and 8 characters long, depending on the user) and rename it as "sally_VT_1000_11_text.txt" (so, minus the "A"), so it can be properly read in by a separate .ado program.

    The code below parses this file name--it is the only file in the directory I'm working within--and then attempts to rename it. In fact, this code actually works on one computer I work on, meaning it successfully renames the file without the "A", but it does not work on another computer i also need it to work on.


    Code:
    clear
    ssc install valuesof
    ssc install moremata
    version 13
    cap log close
    set more off, permanently
    set linesize 255
    
    **************************
    *The commands below extract the file name from your trainingdata folder. 
    **************************
    *read in the file name automatically - NOTE: the below code will only work if there is only ONE file in the folder.
    local tfile: dir "$dofile/trainingdata" files "*"
    set obs 1
    gen file = `tfile'
    
    replace file = lower(file)
    
    gen twentiethchar = substr(file, -20, 1)
    
    *This code parses that file name into the components.
    capture drop tempname
    gen tempname = regexr(file, "^.*[a-z]_", "")
    gen tempname2 = subinstr(file, tempname, "", .)
    if twentiethchar == "_"{
        gen tempname3 = substr(tempname2, -4, .)
    }
    else {
        gen tempname3 = substr(tempname2, -5, .)
    }
    gen name = subinstr(tempname2, tempname3, "", .)
    gen und = "_"
    egen tempname4 = concat(name und)
    replace file = subinstr(file, tempname4, "", .)
    replace file = subinstr(file, "_text.txt", "", .)
    replace file = upper(file)
    if twentiethchar == "_"{
        gen st = substr(file, 1, 2)
    }
    else {
        gen st = substr(file, 2, 2)
    }
    
    egen tempst = concat(st und)
    replace file = subinstr(file, tempst, "", .)
    replace file = substr(file, -7, .)
    gen yr = substr(file, 1, 4)
    egen tempyr = concat(yr und)
    replace file = subinstr(file, tempyr, "", .)
    ren file batch
    
    *This renames files where the state name includes an extra A at the beginning to the regular state abbr.
    if twentiethchar != "_"{
        gen fileend = "text.txt"
        local orig_file: dir "$dofile/trainingdata" files "*"
        local newfilename = name+und+st+und+yr+und+batch+und+fileend
        foreach file of local orig_file{
            !rename "`file'" "`newfilename'"
        }
    }
    I've tried playing around with a whole host of things in this bottom loop that does the renaming, including defining the directory via cd before trying to rename the file, creating a local macro representing the filepath and including before `file' and/or `newfilename'--see the below example:

    Code:
    if twentiethchar != "_"{
        capture drop fileend
        gen fileend = "text.txt"
        local filebeg "$dofile/trainingdata/"
        local orig_file: dir "$dofile/trainingdata" files "*"
        local newfilename = name+und+st+und+yr+und+batch+und+fileend
        foreach file of local orig_file{
            di "`filebeg'"
            di "`file'"
            di "`newfilename'"
            di "`filebeg'`file'" 
            !rename `file' `newfilename'
        }
    }

    Nothing seems to work on this second computer (and it is important that it work on both). I get the sense that this can be a fickle process from this Stata thread. Any thoughts as to what's causing the issue or possible workarounds I haven't explored? Thanks!

  • #2
    I interpret "My goal is to take the file name "sally_AVT_1000_11_text.txt" ... and rename it as "sally_VT_1000_11_text.txt" (so, minus the "A"), " as

    "Remove everything from the file name string from the first character up to where '_VT' occurs, and replace it with the user name."

    If that is correct, and presuming "file" and "username" are contained in variables, I would do:
    Code:
    replace file = username + substr(file, strpos(file, "_VT_"), .)
    If this is not right, I would need a different description of your problem, specifying precisely what your rule for string replacement would be. In particular, I don't understand the relevance of "twentiethchar" and what not.

    Also, while there are cases in which I have found it convenient to create a file with one observation, that's not common. I would think the user and file names might more conveniently passed on as input to a program. Some description of how the user and file names become available to the problem would be helpful, as there might be a simpler way to pass and process them.

    Regards, Mike

    Comment


    • #3
      Sarah,
      1. first simplify the task. Forget about the program. Create a file manually with the desired name on the computer where it is "not working". Then rename the file in Stata. This will reveal what is the problem.
      2. second, what Stata are you working with? If this is anything below 14.0, make sure the user name is not non-Latin.
      3. I had a real case when the person reporting a bug ultimately confirmed that his username in Windows was this: ` YES Just this inverse apostrophe or whatever the name of that mark is. You can only imagine how Stata loves the user names of this kind!
      4. when everything else fails, try to work with short names in a shallow directory (C:\Data\) or (C:\Stata\) not anywhere the "My Documents With spaces on a network drive mapped to dropbox" folder.
      5. when everything works however, then run the code you have with the trace on and check that the rename command is receiving exactly the same arguments as you tried manually.

      Just a wild guess would be a space in the file name, which confuses rename since the file names are not enclosed in quotes. Its funny that you went through the trouble of putting all those debugging display commands, but you don't show what they produce on either of the computers in question.


      Hope this helps, Sergiy

      Comment

      Working...
      X