-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdirfile.ado
136 lines (92 loc) · 4.21 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
********************************************************************************
* 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 - *
* 134 *
* *
********************************************************************************
*! dirfile
*! v 0.0.6
*! 09NOV2016
// 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 [anything(name = root id = "Root directory")], [ Path(string) REBuild ]
// Default value for path parameter
if `"`path'"' == "" loc pmatch "*"
// Passes the value from the path parameter to be used in extended macro function
else loc pmatch `path'
// Check for file path existence
qui: loc newfile : dir "`root'" dirs "`pmatch'", respectcase
// Clean up quotation marks
loc newfile : list clean newfile
// Doesn't exist create it
if `"`newfile'"' == "" mkdir `"`root'/`path'"'
// Does exist and user wants to rebuild the directory
else if `"`newfile'"' != "" & "`rebuild'" != "" chksubdir `root'/`newfile'
// If directory exists but user does not want to rebuild
else di as res "Directory exists and rebuild option not specified. No further action"
// End of program definition
end
// Defines subroutine to check for files in subdirectories and remove them
prog def chksubdir
// Defines syntax for calling the subroutine
syntax anything(name = subdirnm id = "Subdirectory name")
// Check for any subdirectories
loc dirfiles : dir `"`subdirnm'"' dir "*", respectcase
// If there are subdirectories
if `: word count `dirfiles'' > 0 {
// Loop over subdirectories
forv d = 1/`: word count `dirfiles'' {
// Call the subroutine recursively
chksubdir `subdirnm'/`: word `d' of `dirfiles''
} // Loop over the subdirectories
} // End IF Block for subdirectory recursion
// Check for filenames
loc filenames : dir `"`subdirnm'"' files "*", respectcase
// Test if the subdirectory contains any files
if `: word count `filenames'' > 0 {
// Loop over the files in the directory
forv i = 1/`: word count `filenames'' {
// Stores the individual file name to check/test
loc filenm `: word `i' of `filenames''
// Print message to screen and get user input
di as res `"Delete the file `filenm' from `subdirnm'? (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 `"`subdirnm'/`filenm'"'
// Success message to console
di as res `"Erased the file : `subdirnm'/`filenm'"'
} // End IF Block for user selected file deletion
} // End Loop over files in directory
} // End IF BLOCK for subdirectories with files
// Check for any subdirectories
loc dirfiles : dir `"`subdirnm'"' dir "*", respectcase
// Check for filenames
loc filenames : dir `"`subdirnm'"' files "*", respectcase
// If the directory is empty
if `"`dirfiles'`filenames'"' == "" {
// Ask user if they want to delete the directory
di as res `"`subdirnm' is empty. Delete the directory too? (Y/n)"' ///
_request(_del)
// If y, Y, or null delete the directory
if inlist(`"`del'"', "y", "Y") qui: rmdir `"`subdirnm'"'
} // End IF Block for directory removal
// End of subroutine
end