开发者

how to split path names.

开发者 https://www.devze.com 2023-03-30 08:28 出处:网络
the input isC:\\test\\deva\\tcl\\newfiles\\aug.txt the output should be \"test\" \"deva\" \"tcl\" \"newfiles\"

the input is C:\test\deva\tcl\newfiles\aug.txt

the output should be "test" "deva" "tcl" "newfiles"

"aug.txt" files or anyother "开发者_如何学JAVA.txt" files at the end of the string should not be printed.


Reverting back to my original solution and adding some bits...

Assuming this is a filepath not a random string that happens to need to be split \

File split does almost what you want, it returns the path split up as a list . you also want to use lrange to select everything but the volume i.e something like (untested)

lrange [file split $path] 1 end-1

so you don't have c:\ which should be the first element in the list returned by file split

Additionally you may want to use file dirname first if there is any chance you will get directory path instead of a filename e.g. same caveats re testing

lrange [file split [file dirname $name]] 1 end


[split] combined with [lrange] can do what you want but in a non-portable way.

One way to make this more portable would be to use the result of calling [file separator] for splitting instead of bare "\". But since "/" are also okay both in Tcl and Windows the real way to go portably would be to repeatedly call [file dirname] on the string and extract the last component of the returned pathname using [file tail].

For more info read this, this and this.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号