tcl

how to split path names.


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

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

"aug.txt" files or anyother ".txt" files at the end of the string should not be printed.


Solution

  • 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