Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: getting list of open files


From   Jeph Herrin <[email protected]>
To   [email protected]
Subject   Re: st: getting list of open files
Date   Wed, 06 Jan 2010 07:45:48 -0500

Eric,

Good idea - not ideal, but a better work around than mine.
In fact, I can implement it via my own file open and close
programs to simplify things; something like:

 program define fileopen
	syntax, Handle(string) Using(string) Options(string)
	file open `handle' using `using', `options'
	global fh_`handle' "`handle' : `options'"
 end

 program define fileclose
	syntax , Handle(string)
	file close `handle'
	macro drop fh_`handle'
 end


then just inspect globals with the fh_ stem to get open
file handles, as well as info about them.

thanks,
Jeph


Eric Booth wrote:
<>

You can store the handle names in a global macro each time you create a new handle (and -macro drop- the handle when you close it) & then get them into a single list in a global macro using the extended_function   all {globals} ["pattern"] .  You can manipulate them from there.   For example:

*********
clear all
cap file close _all
cap macro drop _all
local sf "`pwd'"
	
**for each handle you create, make a global macro**
forv n= 1/10 {
tempfile testm`n'
	file open handle`n' using "`sf'/testm`n'", write replace
	global handle`n'  "handle`n'"     //<-- create the global
file write handle`n' "test" _n	
}

//If you close a handle, you'll need to drop the macro
file close handle5
macro drop handle5   //<-- remove the global


*----------------------------------examine open handles
//store all open handles in a single macro//
global overview: all globals "handle*" macro li overview

//use the list of open handles//
foreach m of global overview {
	di "`m'"
file write `m' _page(2) " the end " /* something else */
	}

*~*
file close _all
*****************
This would be less messy if the -file query- command could return the open handles along with the number of open files r(N).

~ Eric
__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
[email protected]
Office: +979.845.6754


On Jan 5, 2010, at 1:28 PM, Jeph Herrin wrote:

I have been using -file- and it's subcommands to
do a lot of report writing; thus, I have written a
number of subroutines that use file handles to output
formatted text.

These would be easier to code if I could get a list of
open file handles. -file query- (which seems to have
no documentation other than that it exists) will output
the list of file handles, their type, and status, but
only returns -r(N)-, the number of open files.

Is there another way to get the list of open file handles
in a macro where I can manipulate them? Barring something
simpler, I can write a script to open a log file, call
-file query-, close the log file, then parse the log file
into a table, but that's messy for something I would want
to call in all of my subroutines.

Any other ideas?

thanks,
Jeph

*



*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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