Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: RE: RE: Access datasets created by date


From   Morten Andersen <[email protected]>
To   Statalist <[email protected]>
Subject   Re: st: RE: RE: Access datasets created by date
Date   Mon, 04 Apr 2005 11:13:59 +0200

MITRA PINAKI
 
>     I have a list of text files in a folder which becomes available
> daily. I would like to access these daily files by date in my program.
> Is it possible in stata to identify the datasets created by date? My
> dataset are named like the following:

Maybe dirlist.ado can help you.

I have attached dirlist.ado and dirlist.hlp (cut them out between ---- lines
and save them as text files in /ado/personal).

Dirlist captures file names, sizes, dates, times in return macros.

The following example uses dirlist to print out information for all datasets
in the working directory from november 2004:

dirlist *.dta
local fnames "`r(fnames)'"
local fdates "`r(fdates)'"
local n `r(nfiles)'
forvalues i=1/`n' {
    local fdate: word `i' of `fdates'
    if index("`fdate'","2004-11")==1 {
        local fname: word `i' of `fnames'
        de using "`fname'", short
        /* just as an example, you can do what you wish with the file
           "`fname'" */
   }
}

In this version, ISO date format is used.

The ado-file uses shell commands to capture directory information. Please
note that on some Windows versions using 12-hour (AM/PM) time formats there
may be problems.

If others find it useful it could be posted to SSC.

Kind regards,

Morten

----------------------------------------------------------------------------
Morten Andersen, MD, PhD               Research Unit for General Practice
Senior Researcher                      University of Southern Denmark
Phone   +45 6550 3791                  J.B. Winsloews Vej 9
Fax     +45 6311 1642                  DK-5000 Odense C
E-mail  [email protected]        Denmark
----------------------------------------------------------------------------

-------------------------------- BEGIN dirlist.ado -------------------------

*! 1.3 MA 2004-10-06 23:56:48
* saves directory data in r() macros fnames, fdates, ftimes, fsizes, nfiles
* used by dodoc.ado
*--------+---------+---------+---------+---------+---------+---------+------

program define dirlist, rclass

    version 8

    syntax anything
    
    tempfile dirlist

    if "`c(os)'" == "Windows" {
    
        local shellcmd `"dir `anything' > `dirlist'"'

    }
    
    if "`c(os)'" == "MacOSX" {
    
        local anything = subinstr(`"`anything'"', `"""', "", .)
    
        local shellcmd `"ls -lT `anything' > `dirlist'"'

    }
        
    if "`c(os)'" == "Unix" {
    
        local anything = subinstr(`"`anything'"', `"""', "", .)
    
        local shellcmd `"ls -l --time-style='+%Y-%m-%d %H:%M:%S'"'
        local shellcmd `"`shellcmd' `anything' > `dirlist'"'
        
    }

    quietly shell `shellcmd'

    * read directory data from temporary file
    
    tempname fh
    
    file open `fh' using "`dirlist'", text read
    file read `fh' line
    
    local nfiles = 0
    local curdate = date("`c(current_date)'","dmy")
    local curyear = substr("`c(current_date)'",-4,4)
    
    while r(eof)==0  {
    
        if `"`line'"' ~= "" & substr(`"`line'"',1,1) ~= " " {

            * read name and data for each file

            if "`c(os)'" == "MacOSX" {
                
                local fsize : word 5 of `line'
                local fda   : word 6 of `line'
                local fmo   : word 7 of `line'
                local ftime : word 8 of `line'
                local fyr   : word 9 of `line'
                local fname : word 10 of `line'
                local fdate =  ///
                    string(date("`fmo' `fda' `fyr'","mdy"),"%dCY-N-D")
                   
            }

            if "`c(os)'" == "Unix" {
                
                local fsize : word 5 of `line'
                local fdate : word 6 of `line'
                local ftime : word 7 of `line'
                local fname : word 8 of `line'
                   
            }

            if "`c(os)'" == "Windows" {
            
                local fdate : word 1 of `line'
                local ftime : word 2 of `line'
                local word3 : word 3 of `line'
                
                if upper("`word3'")=="AM" | upper("`word3'")=="PM" {
                    local ftime "`ftime'-`word3'"
                    local fsize : word 4 of `line'
                    local fname : word 5 of `line'
                }
                else {
                    local fsize : word 3 of `line'
                    local fname : word 4 of `line'
                }  
    
            }

            local fnames "`fnames' `fname'"
            local fdates "`fdates' `fdate'"
            local ftimes "`ftimes' `ftime'"
            local fsizes "`fsizes' `fsize'"
            local nfiles = `nfiles' + 1

        }

        file read `fh' line
    
    }
    
    file close `fh'
    
    return local fnames `fnames'
    return local fdates `fdates'
    return local ftimes `ftimes'
    return local fsizes `fsizes'
    return local nfiles `nfiles'
    
end

* end

-------------------------------- END dirlist.ado -------------------------
-------------------------------- BEGIN dirlist.hlp -------------------------

{smcl}
{* 2004-03-12 15:57:14}{...}
{hline}
help for {hi:dirlist} {right: (version 1.3, 2004-10-06)}
{hline}

{title:Retrieve directory information}

{p 4 13 2}{cmd:dirlist} [{it:filespec}]


{title:Description}

{p 4 4 2}
{cmd:dirlist} is used as the {cmd:dir} command, but retrieves the
information
about files in in return macros (see below).

{p 4 4 2}
{it:filespec} may be any valid Windows, Unix, or Macintosh file path or file
specification (see {hi:[U] 14.6 File-naming conventions}) and may include
"{cmd:*}" to indicate any string of characters.

{p 4 4 2}
Directory data are written to a temporary file using shell commands
(Windows {cmd:dir} and Mac OS X or Unix {cmd:ls}) and subsequently read by
the program. 

{p 4 4 2}
Mac OS X: Spaces in the {it:filespec} should be preceded by an escape
character "{cmd:\}".


{title:Examples}

{p 4 8 2}
{cmd:. dirlist dm50*.do}

{p 4 4 2}
You can then access the returned results:

{p 4 4 2}
{cmd:. return list}

    macros:
            r(nfiles) : "4"
            r(fsizes) : "814 209 296 493"
            r(ftimes) : "13:27:15 13:29:05 12:22:01 13:41:09"
            r(fdates) : "2003-10-30 2003-10-30 2003-10-30 2003-10-30"
            r(fnames) : "dm501.do dm502.do dm503.do dm504.do"
            
{p 4 8 2}
{cmd:. dirlist ~/DM\ data/dm50*.do} {it:(Mac OS X, space in directory name)}


{title:Note} 

{p 4 4 2}
The ado-file has been tested on Mac OS X, Windows XP and one type of Linux.
Possible problems could occur caused by the layout of directory lists
regarding column arrangement and date format.


{title:Author} 

{p 4 4 2}
Morten Andersen, Research Unit for General Practice{break}
University of Southern Denmark, Denmark{break}
        [email protected]


{title:Also see}

{p 4 13 2}
Online:  help for
{help dir},
{help shell},
{help return}
{p_end}

-------------------------------- END dirlist.hlp -------------------------

*
*   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