Shameless plug: -fpref- at SSC archive

You may now install -fref-, which is updated to include a suffix() option, by typing:

ssc install fpref

-fpref- adds a prefix or a suffix, or both, to file names by batch. Batch renaming files can be done using the command prompt; but for those who are not comfortable with DOS, -fref- is for you.

The ado and help files are also available at IDEAS and EconPapers.

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…

Friendly -fren-

Today I downloaded resources (do-files and datasets) used in the the book Microeconometrics Using Stata, Revised Edition (Cameron and Trivedi, 2009). When I unzipped the file, I found that all the file names in the folder have the prefix “mus”. I want to get rid of these prefixes. Renaming the files one by one seems crude (something that a muggle would do in Harry Potter’s world); somebody must have written a Stata program for this! So I googled using the keywords “stata rename filenames”, and there it was: “FREN: Stata module to batch rename files (Windows only)”.

-fren- (Liu Wei, 2010) renames file names (with specific file extensions) in the current directory. Its syntax is straight forward:

fren file_extension , from(str1) [ to(str2) ]

For example, to get rid of the prefix “mus” from the names of do-files and datasets in the folder, I first installed the program and changed the directory:

ssc install fren
cd path
frendta , from(mus)
frendo , from(mus)

And, all those “mus” were gone. Happiness…

To check other commands Liu Wei has written, go to Liu Wei at IDEAS.