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" <>
To   <>
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 "."


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'"'

. _getfilepath c:\temp\test.smcl

. ret li

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

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

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. 


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 

*   For searches and help try:

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