0

I use the below command to delete file in TCL. But it errors out, when the file does not exist.

Am I correct to say the "nocomplain" will prevent it from erroring out? If not, what options, can I use to stop it from error out when file does not exist?

eval file delete [glob nocomplain a.txt b.txt]

Kimi
  • 43
  • 1
  • 7
  • Why are you using `eval`? Use `{*}` to turn a list into individual arguments. – Shawn Dec 12 '20 at 00:37
  • And see https://www.tcl.tk/man/tcl8.6/TclCmd/glob.htm with regards to 'nocomplain'. – Shawn Dec 12 '20 at 00:38
  • Or just `file delete a.txt b.txt` since you don't have any wildcards in those `glob` patterns. – Shawn Dec 12 '20 at 00:40
  • This has been asked many times, search for "[tcl] file delete glob" and find e.g. https://stackoverflow.com/questions/54255592/how-to-rm-rf-in-tcl – mrcalvin Dec 12 '20 at 14:45

1 Answers1

1

The documentation for file delete says:

Trying to delete a non-existent file is not considered an error.

So you can just use file delete a.txt b.txt. There's no need to use glob in your example because you don't have any wildcard patterns to match.

If you did, the documentation for glob describes the -nocomplain option to avoid raising an error if no files match. Note the leading dash.

Finally, you can use argument expansion instead of that ugly looking eval. Something like:

file delete {*}[glob -nocomplain a*.txt]
Shawn
  • 47,241
  • 3
  • 26
  • 60