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

RE: st: capturing input file information


From   "Morten Andersen" <[email protected]>
To   <[email protected]>
Subject   RE: st: capturing input file information
Date   Wed, 14 Jan 2004 21:48:10 +0100

>Is there an easy way to capture information of an input file into a local
>macro, so it can be saved as a note with Stata data set? 

>I am looking for information similar to that stored in c(filename) and
>c(filedate) for a Stata dataset in memory,

I have made a general ado-file for this: dirlist.ado. It produces a directory list and puts the result into return macros. Ado-file and hlp-file below. Actually it is part of a collection of ado-files which I hope to submit to the SSC archives.

It has been tested in Mac OS X and Windows XP. There could be some problems with layout of dir output, however. Please report such problems to me.

Regards,

Morten Andersen

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

Cut out the ado and hlp files below.
Please note that there could be problems with line breaks caused by the e-mail program.

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

*! 1.0.4 MA 2003-12-04
* saves directory data in r() macros fnames, fdates, ftimes, fsizes, nfiles
* dirfile, dodoc, logdo, project
*--------+---------+---------+---------+---------+---------+---------+---------

program define dirlist, rclass

	version 8

	syntax anything
	
	tempfile dirlist

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

	}
	
	if "`c(os)'" == "MacOSX" | "`c(os)'" == "Unix" {
	
		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 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

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

{smcl}
{* 2003-12-04}{...}
{hline}
help for {hi:dirlist} {right: (version 1.0.4, 2003-12-04)}
{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: The {it:filespec} should not be enclosed in quotes, and spaces 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:Possible problems} 

{p 4 4 2}
Quotes around {it:filespec} and spaces in path names on other OSs?{break}
Format 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 PROGRAMS --------------------------------------



<<winmail.dat>>




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