Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: RE: Removing multiple files in a directory - clean-up

From   Nick Cox <>
To   "" <>
Subject   Re: st: RE: Removing multiple files in a directory - clean-up
Date   Fri, 2 Dec 2011 11:56:31 +0000

You can just escape through to your operating system:

!del *.dta


!rm *.dta

as the case may be.

Also check out -fs- from SSC as a convenience wrapper.


On 2 Dec 2011, at 10:54, Tim Evans <> wrote:

Hi all,

Actually, think I may have solved this myself using the following (though happy to learn of a neater way if it exists):

local workdir "C:\test1\analysis"
cd `workdir'

local datafiles: dir "`workdir'" files "*.dta"

foreach datafile of local datafiles {
   rm `datafile'


Best wishes


-----Original Message-----
From: [mailto:owner-] On Behalf Of Tim Evans
Sent: 02 December 2011 10:02
To: ''
Subject: st: Removing multiple files in a directory - clean-up

I am using Stata 11.2 and am looking for a routine that will look for all of the .dta files in my specified directory and remove them once I have completed my analysis as a clean-up exercise.

I am using the code below after I have run my regression analysis, to list and combine my datafiles created following the regression analysis, but I cant seem to find out how I can remove all of the unwanted .dta files after I have exported the master analysis as a .csv file. This is important as I will then go on to use the code below after another set of regressions saved in the same directory, and I don't want to combine regression results from different sets of analysis. Is there a way I can amend the code below to remove all the files:


cd c:\test1
! dir *.dta /a-d /b >c:\test1\filelist.txt

file open myfile using c:\test1\filelist.txt, read

file read myfile line
use `line'
save master_data, replace

file read myfile line
while r(eof)==0 { /* while you're not at the end of the file */
   append using `line'
   file read myfile line
file close myfile
sort label2 analysis
rename estimate odds_ratio
drop order axis
order label2 analysis  odds_ratio  min95 max95 stderr z p flag parm
save master_data, replace
rm c:\test1\filelist.txt
outsheet using C:\test1\results\Section1.csv,c replace

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index