user3587879
user3587879

Reputation: 127

how to replace \ by \\ in tcl script string variable?

I am trying to replace the \ with \\ in a string variable which contains a network folder path. script is

regsub -all {'\'} $folderpath {\\} $folderpath

if it is other character i am able to replace, since its \, I am getting problem.

Upvotes: 0

Views: 3917

Answers (2)

Peter Lewerin
Peter Lewerin

Reputation: 13252

set folderpath [string map {\\ \\\\} $s]

is about five times faster than using regsub.

Note that the file command has several subcommands that can manage a path string regardless of what the separators look like.

Documentation: file, string

Upvotes: 2

Dinesh
Dinesh

Reputation: 16428

% set path {C:\Dinesh\Downloads\Movies\Friends}
C:\Dinesh\Downloads\Movies\Friends
% regsub -all {\\} $path {\\\\}
C:\\Dinesh\\Downloads\\Movies\\Friends
%

Upvotes: 0

Related Questions