How to `rm -rf *` in TCL

北城余情 提交于 2021-01-28 02:04:54

问题


I want to delete all files in a directory using TCL. (I'm using Xilinx Vivado's TCL console under Win 10.) I found that in TCL documentation that

file delete ?-force? ?- -? pathname ?pathname ... ?

should work. But

file delete -force -- [glob *]

does nothing. What's the wrong with that?


回答1:


Make that

  file delete -force -- {*}[glob *]

... so that the path names returned by [glob] are turned into multiple arguments to [file delete] (using the expansion operator {*}), rather than one argument representing the one list of a path names (read by [file delete] as one, well complex file path).

Alternatively, on Tcl older than 8.5, use an explicit loop:

 foreach path [glob *] {
    file delete -force -- $path
 }



回答2:


Additional things for you do consider:

  1. do you need to be concerned about deleting files, not directories? Consider the -type option for the glob command.
  2. if you need to work recursively, don't reinvent the wheel and use tcllib. The fileutil::traverse and fileutil packages are relevant.


来源:https://stackoverflow.com/questions/54255592/how-to-rm-rf-in-tcl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!