Reputation: 776
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
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
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