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.
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:
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!
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'" } }
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!
Comment