My -fpref-: anti -fren-

Yesterday, I used -fren- to delete the prefix “mus” in the file names of all data and do-files used in the book “Microeconometrics Using Stata” (MUS). Now, I want them back! -fren- is not helpful. So, here is the little program -fpref- that I wrote (my first ado-file) to add a prefix to file names by batch:


[Note: I had to paste it as an object to retain indentations. While indentations are purely aesthetics, a program (like a mathematical proof) doesn’t look right without them. Have not found a way yet to keep indentations (and font style) here for free.]

The first line “capture prog drop fpref” drops the program called -fpref-, if it exists. If it does not exist, Stata will not return an error and will continue to read the next line because of the command -capture- (this line can be deleted in the final version of the ado file). In the next line “prog define fpref”, the command is named as -fpref-. By typing “version 10” in the third line, the command that was named -fpref- is compatible with Stata 10 or newer versions of Stata. The next line defines the syntax. The body of the program follows the syntax. In this program, the first part of the body, with the while{} and if{} loops, returns an error if the operating system used is not Windows or the prefix is missing or separated by blank spaces. The second part of the body concatenates prefix to the old file names, which we have listed in a local macro. Finally, a Stata program always ends with an “end”.

I have saved my fpref.ado (and its corresponding help file fpref.sthlp) and changed my directory. I can now type “help fpref”, which will open the -help- window with the following information:

-help fpref-

To bring back the prefix “mus” to my files:

fpref dta, prefix(mus)  /* adds the prefix “mus” to all *.dta files in the current directory */

fpref do, prefix(mus)  /* adds the prefix “mus” to all *.do files in the current directory */

[Note: fileextensions can by anything, e.g., doc, xls, txt; and prefix can be any character or string (without spaces) that is allowed in a file name.]

Now, all my MUS file names are as they were. Happiness…

Leave a Reply