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

Re: st: reading file creation dates and names


From   Morten Andersen <[email protected]>
To   Statalist <[email protected]>
Subject   Re: st: reading file creation dates and names
Date   Thu, 25 Mar 2004 23:44:05 +0100

Dear Douglas and other statalisters,

I have an ado-file -dirlist- that might be useful for you. It picks up file
information corresponding to a -dir- command.

The ado- and hlp-files are attached below. If it is of general interest,
I'll be happy to send it to the SSC archive.

Please report any problems that occur directly to me, accompanied by a log
showing the corresponding dir/ls listing from the command line of your OS.

Best wishes

Morten

----------------------------------------------------------------------------
Morten Andersen, MD, PhD               Research Unit of General Practice
Senior Researcher                      University of Southern Denmark
Phone   +45 6550 3791                  Winsloewparken 19
Fax     +45 6311 1642�                 DK-5000 Odense C
E-mail  [email protected]        Denmark
----------------------------------------------------------------------------

The following sections should be cut out between lines and placed as
separate files preferably in the ado/personal directory. Although I am
supposed to mail in plain text mode, sometimes unusual characters and
unexpectec line breaks can occur, so files may have to be edited.

------- begin dirlist.ado --------------------------------------------------
*! 1.0.7 MA 2004-03-12 15:56:51
* 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" | "`c(os)'" == "Unix" {
    
        local anything = subinstr(`"`anything'"', `"""', "", .)
    
        local shellcmd `"ls -lkT `anything' > `dirlist'"'

    }
        
    shell `shellcmd'

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

            * read name and data for each file

            if "`c(os)'" == "MacOSX"  | "`c(os)'" == "Unix"{
                
                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 = "`fmo'"+" "+"`fda'"+" "+"`fyr'"
                local fdate = string(date("`fdate'","mdy"),"%dCY-N-D")
                   
            }

            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.0.7, 2004-03-12)}
{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 and Windows XP. 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, 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 ---------------------------------------------------

> From: Douglas Crawford <[email protected]>
> Reply-To: [email protected]
> Date: Thu, 25 Mar 2004 13:32:47 -0800
> To: [email protected]
> Subject: st: reading file creation dates and names
> 
> Hi everyone,
> 
> I have approximately 250 text files that I would like to assemble into
> one large data file.  The unique identifiers for the observations are
> contained in the file names and the file creation dates.  This brings
> me to three questions:
> 
> 1 - I would like to automatically  create a variable = to the file
> creation date.  I read through the various infile, infix, and insheet
> commands in the [R] (as well as the FAQ's), but couldn't find a
> solution.
> 2 - Is there a way of creating a foreach command that does not require
> explicit definition of the items.  In other words can I say something
> like foreach file in folder {...
> 3 - If '2' is possible then I will also need to create a variable for
> each of the files = to the file name.
> 
> If these are possible, I am sure they have been discussed before, but I
> just couldn't access the information.
> 
> Thank you very much for your help.
> 
> Kindest regards,
> 
> Douglas
> 
> 
> 
> Douglas Crawford
> Ph.D. Candidate, Dept. of Biochemistry UCSF
> 
> *
> *   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/
> 


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