Hi Statalist,
I'm working on creating a program, filetree, that will loop through all files and folders after specifying the path in the syntax, and will print the entire file tree in an excel. What I'm having trouble with is how to have Stata recursively loop through one loop, that then knows to back up to different folder levels (so not identically recursive). Right, now, for each level of a folder I have a different nested loop, so my file tree is limited by how many nested loops I have.
The syntax is:
filetree "[File path, which must be in quotes]" using [Desired name of file tree]
example:
Here is a modified version of the program (this will print a file tree for 3 folders deep:
I'm working on creating a program, filetree, that will loop through all files and folders after specifying the path in the syntax, and will print the entire file tree in an excel. What I'm having trouble with is how to have Stata recursively loop through one loop, that then knows to back up to different folder levels (so not identically recursive). Right, now, for each level of a folder I have a different nested loop, so my file tree is limited by how many nested loops I have.
The syntax is:
filetree "[File path, which must be in quotes]" using [Desired name of file tree]
example:
Code:
filetree "C:\Users\Username\Dropbox\My_Folder" using filetree
Code:
* Author: Lucia Goin
* Contact:
* Purpose: Create file tree in excel
*******************************************************************************
program filetree
* quotes must be around file path
syntax anything using/, [DIRSOnly]
if "`using'" == "" {
loc using filetree
}
* things to automate: commas, and names
* start writing report here, define directory
loc maindir `anything'
file open report using "`maindir'\\`using'.csv", write replace
* write the directory
file write report "`maindir'" _n
* this will create local of all FILES
loc files: dir "`maindir'" files *
* will rewrite over files each time
loc pn: word count `files'
* write a directory with files
if `pn' > 0 {
foreach file of loc files {
file write report ",`file'" _n
}
}
loc i 0
loc sd: dir "`maindir'" dirs "*"
loc dn: word count `sd'
if `dn' > 0 {
file write report _n
foreach subdir of loc sd {
loc subdirectory "`maindir'\\`subdir'"
file write report ",`subdir'" _n
* find all subfiles
loc files: dir "`subdirectory'" files *
* will rewrite over paperfiles each time
loc n: word count `files'
* write subfiles found above
if `n' > 0 {
foreach file of loc files {
file write report ",,`file'" _n
}
}
loc sd`i': dir "`subdirectory'" dirs "*"
loc dn: word count `sd`i''
if `dn' > 0 {
file write report _n
foreach subdir_1 of loc sd`i' {
* actual directory path
loc subdirectory "`maindir'\\`subdir'\\`subdir_1'"
file write report ",,`subdir_1'" _n
* find all subfiles
loc files: dir "`subdirectory'" files *
* will rewrite over paperfiles each time
loc pn: word count `files'
* write subfiles found above
if `pn' > 0 {
foreach file of loc files {
file write report ",,,`file'" _n
}
}
loc sd`i'`i': dir "`subdirectory'" dirs "*"
loc dn: word count `sd`i'`i''
if `dn' > 0 {
file write report _n
foreach subdir_2 of loc sd`i'`i' {
* actual directory path
loc subdirectory "`maindir'\\`subdir'\\`subdir_1'\\`subdir_2'"
file write report ",,,`subdir_2'" _n
* find all subfiles
loc files: dir "`subdirectory'" files *
* will rewrite over paperfiles each time
loc pn: word count `files'
* write subfiles found above
if `pn' > 0 {
foreach file of loc files {
file write report ",,,,`file'" _n
}
}
}
}
}
}
}
}
file close report
end

.
Comment