Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a directory list

Status
Not open for further replies.

WaltS

Programmer
Mar 11, 2002
13
US
I am writing a script to create a list of subdirectories contained in a given directory. I start the list with the top level directory and lappend any directories that it finds. I am having a problem getting it to continue past the first level down. What I would like the script to do is read the list as it is updated and continue through until there are no more subdirectories. The code i came up with is this:
proc dirlist { } {
global dirlong
global basedir
global subdir
foreach name $dirlong {
cd $basedir
set current [pwd]
set unsorted [glob -nocomplain *]
if {$unsorted != ""} {
set files [lsort $unsorted]
foreach filename $files {
if {[file isdirectory $filename] != 0} {
set newdir [concat $basedir/$filename]
puts "Appended $newdir"
lappend dirlong $newdir
} else {
lappend filelist $filename
}
}
}
puts "Directory list is\n $dirlong"
}
return
}
 
Too complicated ;)
Make it easy on yourself.

proc dir_list {parent {dflag "0"}} {
set search_tree {} ; set m 0
foreach name [glob $parent*] {
if {[file isfile $name]} {
continue
} elseif {[file isdirectory $name]} {
incr m
lappend search_tree $name
}
}

if {$dflag > 0} {
puts "Found $m directories."
}
return $search_tree
}

set my_tree [dir_list "/home/mars/"]
puts $my_tree
"/home/mars/tclstuff /home/mars/bin /home/mars/KDesktop /hom
e/mars/Mail /home/mars/awkstuff /home/mars/html_stuff /home/
mars/dingo /home/mars/cgitcl /home/mars/docs /home/mars/twea
kbios /home/mars/komodo /home/mars/Komodo-
1.2 /home/mars/setedit"

HTH
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top