Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

windows path problem

by anu_1 (Acolyte)
on Aug 19, 2010 at 14:59 UTC ( #856049=perlquestion: print w/replies, xml ) Need Help??

anu_1 has asked for the wisdom of the Perl Monks concerning the following question:

How to replace single backslash in windows path to double backslashes $path="c:\abc\xyz" == > "c:\\abc\\xyz" I tried ~s/\\/\\\\/g but it didn't work Thanks for help

Replies are listed 'Best First'.
Re: windows path problem
by toolic (Bishop) on Aug 19, 2010 at 15:08 UTC
    Neither did you show actual Perl code, nor did you describe how it did not work for you. This will perform the substitution you requested:
    use strict; use warnings; my $path = 'c:\abc\xyz'; $path =~ s/\\/\\\\/g; print $path; __END__ c:\\abc\\xyz

    The double quotes interpolate. If you use strict and warnings, you will get a warning message.

    On a side note, when posting, just include code within code tags, not all of your text.

      Thanks for solving the problem.
Re: windows path problem
by ikegami (Pope) on Aug 19, 2010 at 15:34 UTC

    I'm not sure why you'd need to do that. Normally, you need to double the slashes in a string literal so it produces a string with single backslashes.

    >perl -le"print qq{c:\\abc\\xyz};" c:\abc\xyz

    You do not want to pass doubled slashes to open.

      But you might want double quotes if you're going to be using the shell, as in
      system( "dir $path" );
      rather than bypassing the shell
      system( 'dir', $path )';
      Disclaimer: I'm a Unix guy. This would be true on Unix; does "MS-DOS 2010" still have a shell?


        No, you wouldn't. "\" is not special to the cmd shell, and it doesn't use "\" for escaping.
Re: windows path problem
by murugu (Curate) on Aug 19, 2010 at 15:33 UTC

    Your substituion regex is correct but as you have given $path value in double quotes it got interpolated. Change it to single quotes and it will work fine.

    Murugesan Kandasamy
    use perl for(;;);

      Thanks a lot

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://856049]
Approved by toolic
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2021-12-02 21:36 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (25 votes). Check out past polls.