-
Notifications
You must be signed in to change notification settings - Fork 40
/
abspath.ado
101 lines (70 loc) · 2.16 KB
/
abspath.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
/***
version: 1.1.0
abspath
=======
a Stata program that returns the absolute path of any given _filename_
Syntax
------
> abspath _filename_
Example
-------
Return absolute path of a file with relative path in Stata
. abspath ../../myfile.smcl
. abspath "./my file.smcl"
Author
------
E. F. Haghish
Center for Medical Biometry and Medical Informatics
University of Freiburg, Germany
_haghish@imbi.uni-freiburg.de_
- - -
This help file was dynamically produced by
[MarkDoc Literate Programming package](http://www.haghish.com/markdoc/)
***/
program abspath, rclass
// get the current path and save it
// --------------------------------------------------------------------
local wd : pwd
// make sure no problem happenes if the file has double quotation sign
capture local fname : display `0'
if _rc == 0 {
local 0 `fname'
}
confirm file "`0'"
// get the path defined in the input and navigate to it
// --------------------------------------------------------------------
if "`c(os)'" == "Windows" {
local 0 : subinstr local 0 "/" "\", all
tokenize "`0'", parse("\")
}
else tokenize "`0'", parse("/")
while !missing("`1'") {
if !missing("`3'") local p = "`p'" + "`1'" //avoid the last 2
//if missing("`3'") local n = "`n'" + "`1'" //get the last 2
if missing("`2'") local filename = "`filename'" + "`1'"
macro shift
}
if !missing("`p'") quietly cd "`p'" //make sure it's not missing
// get the current path
// --------------------------------------------------------------------
local path : pwd
local pathstr `path'
if "`c(os)'" == "Windows" {
local filename : subinstr local filename "/" "\", all
local path : subinstr local path "/" "\", all
local pathstr = "`path'" + "\"
local abspath = "`path'" + "\" + "`filename'"
}
else {
local abspath = "`path'" + "/" + "`filename'"
local pathstr = "`path'" + "/"
}
// go the the original path and return output
// --------------------------------------------------------------------
quietly cd "`wd'"
display "`abspath'"
return local pathstr `pathstr'
return local path `path'
return local abspath `abspath'
return local fname `filename'
end