Vee
Vee

Reputation: 776

How do you validate file paths in TCL?

Given a string, I'd like to know if the string represents a valid file path. I've looked at [file isfile <string>] and [file isdirectory <string>] but it seems those two return false if the given string isn't pointing to an existing file/directory. I'm merely interested in the validity of the string.

Upvotes: 1

Views: 1063

Answers (2)

Schelte Bron
Schelte Bron

Reputation: 4813

Pretty much every string is a valid file path. Can you give some examples of what you consider invalid file paths?

You can use file pathtype to check if the path is absolute. Also file split and file normalize may be useful, depending on what you really need.

Upvotes: 2

Brad Lanam
Brad Lanam

Reputation: 5723

I don't know that there's a particular way to do that. I suppose you could try:

set fn /home/bll/mytest.txt
set fail true
if { [file exists $fn] } {
  set fail false
} else {
  try {
    set fh [open $fn w]
    set fail false
    file delete $fn
  } on error {err res} {
  }
}

Even if the above works, certain characters may cause issues if you use the filename in an external command line.

I strip the following characters simply to avoid possible command line problems:

 * : " ? < > | [:cntrl:]

I have this list as possibly problematical for unix:

 * & [] ? ' " < > | 

And this list for windows:

 * : () & ^ |  < > ' " 

Also, a trailing dot is illegal for windows directory names.

And slashes or backslashes can cause issues as those are directory separators.

Upvotes: 2

Related Questions