Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can we use "\" in setting a string? 2

Status
Not open for further replies.

RajVerma

Programmer
Jun 11, 2003
62
DE
hi all,
is it possible to set a string that has a backward slash in it like this?
set str "d:\drive\"
it's not accepting. but I have to use this \ in my string. wat shud I do?
regards.
 
well anyways, I declared it this way.
set string1 [format "%s" "d:\exchange\"]
regsub -all {/} $string1 {\\} string2
I hope there shud be a better way.
Rajverma.
 
That won't work. The regsub statement changes "/" to "\". In order to set the string the way you want you need double slashes "\\". Tcl interprets the first "\" as the escape sequence designator. "\\" is expanded to a literal "\".

Bob Rashkin
rrashkin@csc.com
 
Do you know that you can very often use / in place of \ in the name of files?

HTH

ulis
 
As ulis pointed out, if you're going to be passing these path references to Tcl commands, you don't need to convert from "/" to "\". Here's what the Tcl filename(n) reference page has to say on the subject:

"All Tcl commands and C procedures that take file names as arguments expect the file names to be in one of three forms, depending on the current platform. On each platform, Tcl supports file names in the standard forms(s) for that platform. In addition, on all platforms, Tcl supports a Unix-like syntax intended to provide a convenient way of constructing simple file names. However, scripts that are intended to be portable should not assume a particular form for file names. Instead, portable scripts must use the file split and file join commands to manipulate file names (see the file(n) manual entry for more details)."

And for those instances where you really do need to convert to your platform's native version of a path (for example, displaying a path to a user, writing it into a file that might be read by other programs, etc.), use the file nativename command.

- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
hi all, I'm v v sorry for the confusion. Actually I wanted to use "\" in my string(eg: set string "d:\exchange\xxx" ) to pass it to a windows machine, but it's not working. so I did this way.

set string1 [format "%s" "d:/exchange/xxx"]
regsub -all {/} $string1 {\\} string2

I did a mistake in my first question, where I said that I used,
set string1 [format "%s" "d:\exchange\xxx"]
regsub -all {/} $string1 {\\} string2.

thanq for the suggestions anyways.
Raj.
 
As pointed by Ken, making a native name should be done with the [file nativename] command:
Code:
  puts [file nativename c:/windows]
->
c:\windows
HTH

ulis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top