Bookmark and Share

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


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

Re: st: RE: recursively search folder sub directories and store filenames in a text file


From   Robert Picard <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: RE: recursively search folder sub directories and store filenames in a text file
Date   Tue, 29 Oct 2013 09:45:06 -0400

Here is a way, from an initial directory, to recursively list all
files in Stata.

Robert

* ----- begin example ---------------------------
cap program drop dirlist
program define dirlist

   syntax , fromdir(string)

   // list of all files in "`fromdir'"
   local flist: dir "`fromdir'" files "*"
   foreach f of local flist {
      dis "`fromdir'/`f'"
   }

   // recursively list directories in "`fromdir'"
   local dlist: dir "`fromdir'" dirs "*"
   foreach d of local dlist {
      dirlist , fromdir("`fromdir'/`d'") `list'
   }

end

local cdir = "`c(pwd)'"
dirlist, fromdir("`cdir'")

* ----- end example -----------------------------

On Tue, Oct 29, 2013 at 8:04 AM, Tim Evans <[email protected]> wrote:
> Hi all,
>
> I am using Stata 11.2 and have a working directory called "T:\Projects\Final". In this folder I have a number of subfolders i.e. GEH_2013, SWB_2013 and within these I have for example GEH_COL and GEH_OGD. Within these folders I have a csv file.
>
> So folder structure looks like :
>
> T:\Projects\Final
> T:\Projects\Final\GEH_2013
> T:\Projects\Final\GEH_2013\GEH_COL
> T:\Projects\Final\GEH_2013\GEH_COL\ GEH_COL_combined.csv T:\Projects\Final\GEH_2013\GEH_OGD
> T:\Projects\Final\GEH_2013\GEH_OGD\ GEH_OGD_combined.csv
> T:\Projects\Final\SWB_2013
> T:\Projects\Final\SWB_2013\SWB_COL
> T:\Projects\Final\SWB_2013\SWB_COL\SWB_COL_combined.csv
> T:\Projects\Final\SWB_2013\SWB_OGD
> T:\Projects\Final\SWB_2013\SWB_OGD\SWB_OGD_combined.csv
>
>
> What I am trying to do is ultimately identify the names of each csv file contained at the third level of sub-directory and append the csv files into one large file.
>
> I have taken a look at using the following:
>
> rcd, :! dir *.csv /a-d /b >filelist.txt
>
> but all this does is create a text file in each sub-directory with the name of the csv file in that directory - so for T:\Projects\Final I have an empty text file as no csv files here, but what I need is a single text file that contains the filename and path for each csv file contained within T:\Projects\Final.
>
> Once I have this, my aim is to use the filenames and paths stored in the text file and to combine each csv file into one file.
>
> If anyone has a more elegant method of appending all csv files that are stored within sub-directories of a folder then I'd be grateful to hear!
>
> Best wishes
>
> Tim
>
> **************************************************************************
> The information contained in the EMail and any attachments is confidential and intended solely and for the attention and use of the named addressee(s). It may not be disclosed to any other person without the express authority of Public Health England, or the intended recipient, or both. If you are not the intended recipient, you must not disclose, copy, distribute or retain this message or any part of it. This footnote also confirms that this EMail has been swept for computer viruses by Symantec.Cloud, but please re-sweep any attachments before opening or saving. http://www.gov.uk/PHE
> **************************************************************************
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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