Amol Patil
Amol Patil

Reputation: 248

How to pass network filepath containing single forward slash as argument to tcl proc?

In TCL, while passing file path to procedure the single slash is treated as escape character hence it gets removed at all places. If I use {} to pass argument later in the procedure I could not convert it back to normal path for any replacement operation. Split function is not correctly splitting the string from curly braces.

Things Tried: I am trying to pass a full file path containing single and double backword slash(\) to tcl procedure to replace first part of path to different path. I tried following 1. Tried to pass the shared dir path as it is result : all backslash gone 2. Tried to send the file path in curly braces result : the string is passed as it is but its not manipulable in proc. If I try to split the path then it gives weird result. Splits at random locations and random characters are missing. 3. with second use case I also tried file split but its still giving same result as use case 2. 4. Also tried file split function but it requires writting a for loop so I will keep it as workaround but is there any simple procedure. or am I doing something wrong?

Code:

proc modpath {strPath} {
   puts "$strpath"     REM result /HuL0GBMV4087.example.comyourPLMB419CATEnvDevPerVisulisationV6R2017.txt
}

set fDir "\\HuL0GBMV4087.example.com\yourPLM\B419\CATEnv\Dev\Per\Visulisation\V6R2017.txt"
modpath $fDir

If I passusing curly braces

proc modpath {strPath} {
   puts "$strpath"     REM result "//HuL0GBMV4087.example.com/yourPLM/B419/CATEnv/Dev/Per/Visulisation/V6R2017.txt"
set PathInfo [split $strCurrentCockpitPath "B419"]
puts "PathInfo : $PathInfo"

REM result PathInfo : {"//HuL0G} MV 087.example.com/yourPLM/ {} {} {} /CATEnv/Dev/Per/Visulisation/V6R20 7.txt\"
}

set fDir {"\\HuL0GBMV4087.example.com\yourPLM\B419\CATEnv\Dev\Per\Visulisation\V6R2017.txt"}
modpath $fDir

I expect if I can pass the arugument using curly braces then split function should work properly to give me normal string split output. I have tried many variations with split as well but could not bring it to work correctly.

Upvotes: 0

Views: 641

Answers (3)

Alex P
Alex P

Reputation: 96

To split by strings, you can use textutil::split::splitx command as well.

For example:

package require textutil::split
textutil::split::splitx $fDir "B419"

Upvotes: 0

Donal Fellows
Donal Fellows

Reputation: 137567

The \ character is special in Tcl source code, as it used as the start of an escape sequence (e.g., \n is the escape sequence for a newline, \\ is the backslash character itself). This means that if you want such a character in a value that originates in the text of your script, you need to either escape it (with itself) or put the whole string in {curly braces}, which disable all substitutions within. (Double quotes only affect the interpretation of whitespace.)

set fDir "\\\\HuL0GBMV4087.example.com\\yourPLM\\B419\\CATEnv\\Dev\\Per\\Visulisation\\V6R2017.txt"
set fDir {\\HuL0GBMV4087.example.com\yourPLM\B419\CATEnv\Dev\Per\Visulisation\V6R2017.txt}

You should not need to ever do special escaping of strings from other places. Tcl does not do double substitutions unless told to explicitly, so unwanted substitutions are always indicative of a bug in you code.


If you are creating URLs, use the other slash, /.

Upvotes: 0

Brad Lanam
Brad Lanam

Reputation: 5723

The split command splits on characters, not a string. It is splitting on 'B', '4', '1', and '9' as requested, and the return value is correct.

You can use a regexp command to get what you want:

# substitute \\ for / if the path contains backslashes
set pathinfo [regexp -inline {^(.*)/B419/(.*)$} $strpath]

Upvotes: 0

Related Questions