RCD
Recursively traverse directories and execute any command in or for each one of them
If you want to use rcd, then feel free to use Stata's command ssc install to download and install rcd:
ssc install rcd, replaceDisclaimer: There is no warranty on this software either expressed or implied. This program is released under the terms and conditions of GNU General Public License.
About rcd
Programmers:
Date: 03Apr2009
Last updated: 03Apr2009
Recursively traverse directories and execute any command in or for each one of them
Nikos Askitas (nikos@iza.org)
IDSC of IZA
IZA, Bonn Germany
Dan Blanchette (dan_blanchette@unc.edu)
The Carolina Population Center
The University of North Carolina
Chapel Hill, NC USA
IDSC of IZA
IZA, Bonn Germany
Dan Blanchette (dan_blanchette@unc.edu)
The Carolina Population Center
The University of North Carolina
Chapel Hill, NC USA
Date: 03Apr2009
Last updated: 03Apr2009
Recursively traverse directories and execute any command in or for each one of them
rcd [directory_name], [verbose]
[depth(integer >= 1)]
[dirfilter(regular expression)] [: command]
Description
rcd expects a directory name otherwise the current working directory is used. rcd
proceeds to recursively visit all subdirectories starting there. rcd will display what directories
it goes to in verbose mode, obey a maximum depth if one is specified and it will optionally run a command in each directory
if desired. Which directories the submitted command are run in can be selected by using the
dirfilter() option.
rcd may be thought of as a recursor. A typical example of what you can do using it is:
Another way to achieve the same thing would be to use the returned macros that rcd generates:
rcd may be thought of as a recursor. A typical example of what you can do using it is:
. rcd . : dir "data"which would result in visiting every subdirectory starting where you are wanting to see the Stata datafiles in each one of them.
Another way to achieve the same thing would be to use the returned macros that rcd generates:
. rcd .
. forvalues i = 1(1)`r(tdirs)'{
. dir "`r(ndir`i')'`c(dirsep)'*dta"
. }
Options
verbose specifies to print the directories that rcd visits. By default, if
rcd is invoked with no directory specified or options specified, then rcd runs
in verbose mode.
depth specifies an integer value greater than or equal to 1 (one) to
limit how far down the directory tree rcd will go. depth(1) specifies not
to look in subdirectories.
dirfilter specifies regular expression to limit what directories the
specified command will be run in. If you want to see what directories
your command will be run in, use rcd and specify to run pwd like so:
. rcd , dirfilter(data) : pwd
Remarks
Since rcd runs whatever Stata command and changes directories in the process, if there is a lot printed
to the results window and you get tired of clicking more to continue and quit, then your current directory may not be
where you started. To quickly get back to the directory you were in when you invoked rcd just use
the returned local macro r(sdir) like so:
. cd "`r(sdir)'"
Examples
Only look in directories whose names contain the word "data":
The code above locates and lists Stata datafiles to either use or describe all or part of each dataset found in the current directory or any of it's subdirectories whose variable names or labels contain either of the words "Family" or "Children".
Another way to do the same thing by listing as you go would be:
. rcd . , dirfilter(data)An example from the realm of managing your files and your data can be constructed by using the grep command:
. rcd .
. return list
. forvalues i = 1(1)`r(tdirs)'{
. grep Family Children, path("`r(ndir`i')'`c(dirsep)'data")
. }
The code above locates and lists Stata datafiles to either use or describe all or part of each dataset found in the current directory or any of it's subdirectories whose variable names or labels contain either of the words "Family" or "Children".
Another way to do the same thing by listing as you go would be:
. rcd . : grep Family Children
Saved results
| Macros: | |
| r(sdir) | directory rcd was invoked in |
| rcd | saves the following in r(): |
| r(tdirs) | total number of directories searched |
| r(ndir1) | name of the first directory searched |
| r(ndir2) | name of the second directory searched |
Also see


