tcl中“if {-s $ file-name}”的等效命令是什么?

时间:2011-07-12 19:14:58

标签: shell tcl

如何在tcl中检查文件是否存在但为空?我的意思是存在任何等效命令,例如shell中的if [ -s <file-name> ]

2 个答案:

答案 0 :(得分:4)

Brian Fenton has the right answer。把他的想法表达为代码:

proc -s {filename} {
  set rc [catch {file size $filename} size]
  return [expr {$rc == 0 && $size > 0}]
}

if {[-s $file]} {...}

答案 1 :(得分:2)

您可以使用“文件大小”。如果文件不存在,则会返回错误,因此您只需抓住它,就可以了。