[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   "Joseph Coveney" <>
To   "Statalist" <>
Subject   Re: st: I can't get fs to work from inside a do file
Date   Sun, 17 Feb 2008 08:21:32 +0900

Gabi Huiber wrote:

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?


Analogous lines run without a hitch for me from within a do-file:

. do "F:\"

. fs *.dta
1.dta  2.dta

. local  files = r(files)

. foreach file of local files {
 2.     display in smcl as input "`file'"
 3. }

. exit

end of do-file

As far as expanding the returned macro and assigning its contents to another
local macro, in order to maintain the compound quotes and spacing properly,
you'll want to type

local files `"`r(files)'"'

instead of

local files `r(files)'

You can see what happens:

. fs *.dta
1.dta  2.dta

. local files `r(files)'

. display in smcl as text "`files'"

. display in smcl as text `"`files'"'
1.dta" "2.dta

. local files `"`r(files)'"'

. display in smcl as text `"`files'"'
"1.dta" "2.dta"

By the way, in my reply, I wrote:

foreach k of local `r(files)' {
   do "`k'"

It should have been:

foreach k in `r(files)' {
   do "`k'"

Joseph Coveney

*   For searches and help try:

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