2017-04-25 4 views
0

私はtclで初心者です。特定のディレクトリとそのディレクトリ下のすべてのファイルをtclで除外する方法

ディレクトリ下のすべてのファイルを収集するためのスクリプトを書くことができます。それはすべてのサブディレクトリです。

私は同じもののためにbelwo procを書いています。

proc rglob { dirpath } { 
    set rlist "" 
    foreach fpath [glob -nocomplain -types f -directory ${dirpath} *] { 
      lappend rlist ${fpath} 
    } 
    foreach dir [glob -nocomplain -types d -directory ${dirpath} *] { 
      lappend rlist {*}[rglob ${dir}] 
    } 
    return ${rlist} 
} 



rglob /a/b/c 

は、dir cとそのサブディレクトリを含むすべてのファイルを提供します。

ここで、cの下にいくつかのディレクトリを除外したい場合を考えます.dir1とdir2が2つのディレクトリであり、ディレクトリdir2を除外したいとします。同じことをどうやって進めるべきなのか教えてください。 set exclude_dir dir2

答えて

0

これは、exclude_dirs_list内の名前を持つすべてのディレクトリを検索resalsから除外します。実際、名前は完全なパスではないので、/ a/b/c/dir1だけでなく、/ a/b/c/d/e/dir1も除外されます。あなただけ/ A/B/C/DIR1を除外するために必要がある場合は受理のように、あなたは、このコードは基本的に同じである

lappend rlist {*}[rglob [file join ${dirpath} ${dir}] {}] 
+0

それは '使用することがより慣用的なら{$ DIR NIます$ exclud_dirs_list} { 'ディレクトリインクルードテスト(not inの場合は' ni ')に対しては効果が同じです。 –

1

lappend rlist {*}[rglob [file join ${dirpath} ${dir}] $exclude_dirs_list] 

から

proc rglob { dirpath exclude_dirs_list} { 
    set rlist "" 
    foreach fpath [glob -nocomplain -tails -types f -directory ${dirpath} *] { 
      lappend rlist [file join ${dirpath} ${fpath}] 
    } 
    foreach dir [glob -nocomplain -tails -types d -directory ${dirpath} *] { 
     if {[lsearch -exact $exclude_dirs_list $dir] == -1} { 
     lappend rlist {*}[rglob [file join ${dirpath} ${dir}] $exclude_dirs_list] 
     } 
    } 
    return ${rlist} 
} 

rglob /a/b/c [list dir1 dir2] 

変更する必要があります答えは少しシンプルです:

proc rglob {dirpath args} { 
    set exclude $args 
    set rlist [glob -nocomplain -types f -directory $dirpath *] 
    foreach dir [glob -nocomplain -types d -directory $dirpath *] { 
     if {$dir ni $exclude} { 
      lappend rlist {*}[rglob $dir {*}$exclude] 
     } 
    } 
    return $rlist 
} 

用途:rglob dirpath ?arg arg ...?rglob .,rglob . ./abc ./def

globまたはregexpマッチングを使用すると、ディレクトリ名の一致を改善できます。

ドキュメント: foreachglobiflappendni (operator)procreturnset{*} (syntax)

関連する問題