Statalist The Stata Listserver


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

st: RE: RE: Accessing the path and filename of currently open logfile.


From   "David Harrison" <[email protected]>
To   <[email protected]>
Subject   st: RE: RE: Accessing the path and filename of currently open logfile.
Date   Tue, 30 May 2006 14:15:58 +0100

A slight simplification of what you have done can be achieved by opening
up the code for -_getfilename- and making it store the path information
as it goes along rather than dumping it (remembering, of course, to name
the modified file something new):

I haven't dealth with the extension part here, but you could do this in
a similar way - tokenizing on "."

David


program define _getfilepath, rclass
    version 8
    gettoken pathfile rest : 0
    if `"`rest'"' != "" {
        exit 198
    }
    gettoken word rest : pathfile, parse("\/:")
    while `"`rest'"' != "" {
        local path `"`macval(path)'`macval(word)'"'
        gettoken word rest : rest, parse("\/:")
    }
    if inlist(`"`word'"', "\", "/", ":") {
        di as err `"incomplete path-filename; ends in separator `word'"'
        exit 198
    }
    return local filename `"`word'"'
    return local path `"`path'"'
end

. _getfilepath c:\temp\test.smcl

. ret li

macros:
              r(path) : "c:\temp\"
          r(filename) : "test.smcl"


-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Nick Cox
Sent: 30 May 2006 13:59
To: [email protected]
Subject: st: RE: Accessing the path and filename of currently open
logfile.

A plain 

. log 

returns the information you need as r-class stuff. 
This is documented at [R] log. 

With commands of the kind you indicated, I have done what you outline,
split with
-gettoken- and then use -syntax-. It is worth knowing that -syntax-
looks at the contents of local 0. Thus you need to assign that before
each call to -syntax-. 
Note also that each -syntax- call will
zap (overwrite, possibly with empty strings) the contents of local
macros like varlist, if, in, etc. Hence you may need to save these
before any second -syntax- call. 

Nick
[email protected] 

David Elliott
 
> I'd like to test to see if (1) a  logfile is open and (2) capture its 
> name.
> The logfile name does not seem to be part of the creturn c() system 
> values although the currently used file name -c(filename) - and pwd
> -c(pwd) are available.
> 
> Is there an equivalent to mata's pathsplit(path,path1,path2) command 
> which would be very useful for filename handling?  I have found the 
> undocumented _getfilename which is useful for popping the filename off

> the end of a path/filename string and one can then do a bit of 
> manipulation to get the path, filename and extension for further 
> manipulation.
> 
> Currently I have the following which will do the job:
> 
>   // Get the filename, path and extension for the log file.
> quietly log
> local log = r(filename)
> if "`log'" != "." {
>     _getfilename "`log'"
>     local logfile =  r(filename)
>     local logpath = subinstr(substr("`log'",1,
> length("`log'")-length("`logfile'")),"\","/",.)
>     gettoken nameonly extonly: logfile, parse(".")
>     local logstem "`logpath'""`nameonly'"
>     local extonly = substr(`"`extonly'"',2,.)
>     di in gr "`logpath'" in ye "`nameonly'" in gr "." in white 
> "`extonly'"
> }
> else {
> 	di in red "No logfile open"
> }
> // end excerpt

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index