gen a = ustrregexs(1) if ustrregexm(oldfile, "(.*)-(.*)\.(.*)") gen b = ustrregexs(2) if ustrregexm(oldfile, "(.*)-(.*)\.(.*)") gen c = ustrregexs(3) if ustrregexm(oldfile, "(.*)-(.*)\.(.*)")
list
*> +-----------------------+ *> | oldfile a b c | *> |-----------------------| *> 1. | a-b.txt a b txt | *> 2. | c-d.txt c d txt | *> +-----------------------+
根据这三个变量创建新的文件名:
gen newfile = b + "-" + a + "." + c
最后再循环把旧文件的内容 copy 到新文件中即可:
cap mkdir "test2" forval i = 1/`n' { copy "test/`=oldfile[`i']'" "test2/`=newfile[`i']'", replace }
评论