Statalist The Stata Listserver


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

RE: st: looping over all files in a folder


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: looping over all files in a folder
Date   Tue, 22 Aug 2006 23:54:59 +0100

You certainly should not be copying and pasting
stuff to do this.
 
Dimitriy Masterov already drew attention 
to the extended macro function -: dir- 
for this. 
 
In addition, -fs- from SSC puts filenames in a macro.
It is a wrapper for that e.m.f. 
 
-fs- was named by analogy with -ds-. In
each case, the obvious output is a compact
list in the Results window, but the really
useful output is the macro left in memory.
 
So,
 
cd /mydir/interestingstuff
qui fs *
foreach f in `r(files)' {
 	...
}
 
Nick
 n.j.cox@durham.ac.uk
 
Radu Ban
 
> I want to read into Stata a bunch of text files, that are all sitting
> in the same folder. I've written a do-file that does the reading in
> part, but now I'm looking for a way to loop this do-file over all the
> files in the folder.
> 
> I have seen the -dir- command which gives me a list of all the files
> in the folder, but after that I'm pretty much stuck. I think I can
> copy and paste this list into my editor and then play with it but it
> seems like a very inefficient method. Especially if new files are
> added into the folder at a later time.


 
 

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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