Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

windows path problem

by anu_1 (Acolyte)
on Aug 19, 2010 at 14:59 UTC ( [id://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 (Patriarch) 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?

      --
      TTTATCGGTCGTTATATAGATGTTTGCA

        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
    anu_2,

    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.

    Regards,
    Murugesan Kandasamy
    use perl for(;;);

      Thanks a lot

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (7)
As of 2024-04-18 14:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found