-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathua.ado
207 lines (163 loc) · 4.79 KB
/
ua.ado
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
*! version 1.0 2017/12/19 9:42
*! Yujun Lian, arlionn@163.com
*! -rcd.ado- is written by Dan Blanchette and Nikos Askitas
// cap program drop unicodeall
// program define unicodeall
cap program drop ua
program define ua
version 15
preserve
clear
local currdir `c(pwd)' //一级目录当前路径
capture _on_colon_parse `0' //
local ucmd `s(after)' // unicode command specified by user
dis _n(5)
dis in w _dup(35) "="
dis in w "Files in the following directories"
dis in w "are to be analyzed or translated:"
dis in w _dup(35) "=" _n _n
rcd // get the list of all sub-directories
*-record the return directories by -rcd- in my locals,
* otherwise they will be overwrited by -unicode *- cmds
forvalues i = 1(1)`r(tdirs)'{
local dir`i' `"`r(ndir`i')'"'
}
*-unicode all directories
forvalues i = 1(1)`r(tdirs)'{
qui cd `"`dir`i''"'
dis _n(2) in w _dup(18) "=" _n ///
in r "Current directory: " _n ///
in w _col(5) `"`dir`i''"' _n
* confirm there are files in current directory
* For blank directory, -unicode analyze *- will report error messeage
local withfiles : dir . files *
if `"`withfiles'"' != ""{
`ucmd' // unicode *
}
else{
dis in w "No files needed to be analyzed or translated in this folder"
}
}
restore
qui cd `currdir'
end
/*
Notes:
1. 需要增加 clear 选项: 用 Preserve 代替了
2. 当前文件夹为空时的错误信息处理: 用 `withfiles' 代替了
*/
/*
A recursive cd command written by Dan Blanchette and Nikos Askitas.
*/
program rcd, rclass
version 9
if inlist(`"`1'"',",",":","") {
local pwd `""`c(pwd)'""'
}
else {
local pwd `""`1'""'
}
local version : di "version " string(_caller()) ":"
capture _on_colon_parse `0'
if c(rc) == 0 {
if `"`s(before)'"' != "" {
// reset 0 before -syntax- so that options of the command submitted to -walk- don't create an invalid option message
local 0 `"`s(before)'"'
}
else if `"`s(before)'"' == "" & `"`s(after)'"' == "" {
local 0 `", verbose"'
}
else if `"`s(before)'"' == "" | `"`s(after)'"' != "" {
// reset 0 before -syntax- so that options of the command submitted to -walk- don't create an invalid option message
local 0 `""'
local subcmd : word 1 of `s(after)'
if `"`subcmd'"' != "" {
capture which `subcmd' // abbreviated commands are fine
if _rc != 0 {
capture program list `subcmd' // now check if it's just command user just loaded
if _rc != 0 {
display as error `"command `subcmd' not found as either built-in or ado-file"'
exit 111
}
}
*if "`subcmd'" == "rcd" {
* display as error `"are you crazy?! ;-)"'
* exit 111
*}
}
}
}
else {
if `"`1'"' == "" {
local 0 `", verbose"'
}
}
syntax [anything] [, Verbose depth(integer 999999999) DIRFilter(string) ]
if `pwd' != `"`c(pwd)'"' {
local cwd `""`c(pwd)'""'
if `"`c(os)'"' == "Windows" {
local cwd `""`c(pwd)'`c(dirsep)'""'
}
capture cd `pwd'
quietly cd `cwd'
if _rc != 0 {
display as error `"directory `pwd' not found"'
exit 601
}
}
return local sdir `"`c(pwd)'"'
tempfile rcdirs
tempname rcdf
file open `rcdf' using `"`rcdirs'"', write text
Rcd `pwd' , command(`"`s(after)'"') `verbose' depth(`depth') dirfilter(`"`dirfilter'"') nds(0) rcdf(`rcdf')
file close `rcdf'
local ndir = 0
capture confirm file "`rcdirs'"
if _rc == 0 {
tempname rcd
file open `rcd' using `"`rcdirs'"' , text read
file read `rcd' line
while r(eof) == 0 {
return local ndir`++ndir' `"`line'"'
file read `rcd' line
}
file close `rcd'
}
return local tdirs = `ndir'
end
program def Rcd
version 9.0
syntax [anything(name=pwd)] [, command(string) Verbose depth(integer 999999999) dirfilter(string) nds(integer 0) ///
dir(string) rcdf(string) ]
local footd `""`c(pwd)'""'
if `"`c(os)'"' == "Windows" {
local footd `""`c(pwd)'`c(dirsep)'""'
}
local nds=`nds' + 1
if `nds' <= `depth' {
local cwd `""`c(pwd)'""'
if `"`c(os)'"' == "Windows" {
local cwd `""`c(pwd)'`c(dirsep)'""'
}
capture cd `pwd' // pwd is has quotes around it
quietly cd `cwd' // cwd is has quotes around it
if _rc == 0 {
quietly cd `pwd'
file write `rcdf' `"`c(pwd)'"' _n
if "`verbose'" != "" {
display as text `"cd "`c(pwd)'" "'
}
local dirlist : dir . dirs *, nofail // create dirlist before command since command could change directories: cd, rcd etc
local prefix `"`c(pwd)'"'
if regexm(`"`dir'"',`"`dirfilter'"') {
`command'
}
quietly cd `"`prefix'"'
foreach dir of local dirlist {
Rcd `"`dir'"' , command(`"`command'"') `verbose' depth(`depth') dirfilter(`"`dirfilter'"') nds(`nds') ///
dir(`"`dir'"') rcdf(`rcdf')
}
}
quietly cd `footd' // footd is has quotes around it
}
end