forked from wbuchanan/eda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirfile.ado
122 lines (85 loc) · 3.81 KB
/
dirfile.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
********************************************************************************
* Description of the Program - *
* Utility for checking/handling directory construction/management related to *
* brewscheme. *
* *
* Data Requirements - *
* none *
* *
* System Requirements - *
* Active Internet Connection *
* *
* Program Output - *
* void *
* *
* Lines - *
* 121 *
* *
********************************************************************************
*! dirfile
*! v 0.0.3
*! 04NOV2015
// Drop the program from memory if loaded
cap prog drop dirfile
// Define the file
prog def dirfile
// Interpret under Stata version 13
version 13
// Syntax for calling the program
syntax , Path(string) [ REBuild ]
// Check for file path existence
cap confirm new file `"`path'"'
// Doesn't exist create it
if _rc == 0 {
// Make the directory
mkdir `"`path'"'
// Print message to the console (mostly for debugging)
di as res `"The directory `path' was successfully created."'
} // End IF Block for non-existent file path
// Does exist and user wants to rebuild the directory
else if _rc == 602 & "`rebuild'" != "" {
// Get all the files in the directory
loc dirfiles : dir `"`path'"' files "*", respectcase
// Loop over the files in the directory
forv i = 1/`: word count `dirfiles'' {
// Print message to screen and get user input
di as res `"Delete the file `: word `i' of `dirfiles'' from `path' ? (Y/n)"' _request(_del)
// If user enters nothing, y, or Y delete the file
if inlist(`"`del'"', "y", "Y", "") {
// Erase the file from the disk
erase `"`path'/`: word `i' of `dirfiles''"'
// Success message to console
di as res `"Erased the file : `path'/`: word `i' of `dirfiles''"'
} // End IF Block for user selected file deletion
} // End Loop over files in directory
// Check for files in directory again
loc dirfiles : dir `"`path'"' files "*", respectcase
// Check for files in directory again
loc subdirs : dir `"`path'"' dirs "*", respectcase
// If the directory is empty
if `"`dirfiles'`subdirs'"' == "" {
// Ask user if they want to delete the directory
di as res `"`path' is empty. Delete the directory too? (Y/n)"' ///
_request(_del)
// If y, Y, or null delete the directory
if inlist(`"`del'"', "y", "Y") {
// Remove the directory
qui: rmdir `"`path'"'
} // End IF Block for directory removal
} // End IF Block for directory removal
} // End ELSE Block for existing directory with rebuild option
// If directory exists but user does not want to rebuild
else if _rc == 602 & "`rebuild'" == "" {
// Print message to console
di as res "Directory exists and rebuild option not specified. No further action"
} // End ELSEIF Block for continuance action
// Work around for stupid Windoze bug
else if _rc == 603 & `"`c(os)'"' == "Windows" {
} // End ELSE IF Block for stupid Windows bug
// Some other error with the file path
else {
// Error out with the returned error code
err _rc
} // End ELSE Block for other error code handling
// End of program definition
end