TCL:遍历文件夹并返回文件名称

时间:2023-03-09 16:45:33
TCL:遍历文件夹并返回文件名称
########################################
#proc tcl_dir : show all file in current path
#parameter
#     path : the path you want to look
# flag :
# 1--only show curent path file list
# 2--show curent path file list include path
# 3--show curent path file list with its subdirectory
# 4--show curent path file list with its subdirectory include path
# result : result will be setted "" and return new result
# eg:
# return [tcl_dir [file dirname [info script]] 1 {}]
#author : li
########################################
proc tcl_dir {path flag result} {
# puts "\'[pwd]\'"
if {"NULL"!=$path && ""!=$path} {
#catach excption or error
if {[catch {cd $path} err]} {
return $err
}
set path {}
}
foreach fileList [glob -nocomplain *] {
# switch $flag {a {} b {}}
switch $flag { \
1 {
puts $fileList
lappend result $fileList
} \
2 {
if {[file isdirectory $fileList]} {
#add subdirectory's result to variable result
lappend result [tcl_dir $fileList $flag {}]
cd ..
}
puts $fileList
lappend result $fileList
} \
3 {
puts [file join [pwd] $fileList]
lappend result [file join [pwd] $fileList]
} \
4 {
if {[file isdirectory $fileList]} {
lappend result "[tcl_dir $fileList $flag {}]"
cd ..
}
puts [file join [pwd] $fileList]
lappend result [file join [pwd] $fileList]
} \
}
}
return $result
}