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

st: Re: reading file creation dates and names

From   "Michael Blasnik" <>
To   <>
Subject   st: Re: reading file creation dates and names
Date   Thu, 25 Mar 2004 17:32:43 -0500

For getting a list of filenames and looping through them, you can something
like this:

local files: dir "." files  "*.txt"
foreach file of local files {
    infile using myfile.dct, using(`file')
    gen filename="`file'"
    save my`file'
    drop _all

The first command puts a listing of all files meeting the spec *.txt into
local macro files.  The foreach command then loops through these files and
saves each file with a variable for it's filename.  You may want use an
append, save, replace approach if you just want one big file as the result.

I don't know of any direct way of getting at the file creation date, but
there have been methods posted where you use an operating system call to get
a directory listing into a text file and then either use the file, infile,
or infix commands to grab the dates as well as the file names.  I think
something was recently posted about this.

Michael Blasnik

----- Original Message ----- 
From: "Douglas Crawford" <>
To: <>
Sent: Thursday, March 25, 2004 4:32 PM
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:

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