[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: I can't get fs to work from inside a do file

From   "Gabi Huiber" <>
Subject   Re: st: I can't get fs to work from inside a do file
Date   Sat, 16 Feb 2008 17:16:47 +0000

Hello Joseph,

Thanks for your reply. I will try the cd solution, but I can confirm
that everything you see there is right. If after my do file doesn't
run, as shown in my previous post, I simply copy every line from the
results window into the command window, and run them each
individually, they all work. In other words, the fs command has no
problem seeing the /prototype/${paper} directory when it is run out of
the command line.

About evaluating the r(files) macro: in this case, I must. There are
five do-files in the /prototype/${paper} directory, to be used in the
foreach loop. If you do

local dofiles r(files)

you get a string of all their names in a single word. I checked with
word count `dofiles'. That, of course, defeats the foreach loop. It's
bizarre, because this single-word string shows the do-file names
neatly delimited with spaces, whereas if you do

local dofiles=r(files)

like I did, the do-file names in that folder look like they're all
strung together as in[...] yet the local `dofiles'
does have five distinct words, one for each do-file, as it should. Try
it with one of your do-file directories to see what I mean. Of course
running the foreach loop directly off the `r(files)' local fixes the
problem, but it's still a strange one, don't you think?

Thanks again,


On Feb 16, 2008 3:34 PM, Joseph Coveney <> wrote:
> Gabi Huiber wrote:
> Nick Cox has this little command called fs, which reads the files in a
> certain folder and remembers them in r(files). I tried to use it to
> run a bunch of do-files from inside a root do-file, because the name
> and number of these subsequent do-files can vary between the
> sub-folders of interest. So here's what I did:
> [redacted]
> . fs prototype/${paper}/*.do
> > local dofiles=r(files)
> >
> > foreach k of local dofiles {
> > do "prototype/${paper}/`k'"
> > }
> >
> > clear
> > di "goodbye"
> As you can see, Stata breezes right through the lines after fs. It
> also does not interpret fs: display r(files) shows nothing. However, I
> can copy the fs line from the results window and paste it in the
> command window, and it will execute just fine. Ditto for all the lines
> below it.
> Any ideas?
> --------------------------------------------------------------------------------
> Do you see the files displayed in the Review window when the -fs- command
> executes?  If not, then -fs- isn't seeing any do-files in the directory that
> you specify, and might not even be directed correctly to the directory at
> all.  I tend to have trouble with -fs- querying a directory that's not
> current, so I just change directories in the do-file to make the target
> directory current, and then change back afterward.
> Also, avoid evaluating the returned macro;  you'll truncate the list to 244
> characters, and might also mess up the compound quotes.
> Try something like the following instead:
> cd prototype/${paper}/
> fs *.do
> foreach k of local `r(files)' {
>     do "`k'"
> }
> . . .
> cd <original directory>
> Joseph Coveney
> *
> *   For searches and help try:
> *
> *
> *
*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index