Hi,

I have a sytem path like "C:\Users\ABC\XYZ\1.txt". I need to convert this to 'C:\Users\ABC\XYZ\1.txt'. Any help is appretiated. Thank you.

I need to unlink the file. Hence I need the path to be in 'C:\Users\ABC\XYZ\1.txt' format.

#!/usr/bin/perl
use warnings;
use strict;

my $path = <DATA>;
chomp($path);
print 'Original path: ', $path, "\n";

$path =~ s/"/'/g;#Replace all " with '
print 'Changed path: ', $path, "\n";

__DATA__
"C:\Users\ABC\XYZ\1.txt"
Comments
Works

Thanks for the reply d5e5. But I'm not able to unlink the file specified by that path.
If I say,

$path = 'C:\Users\ABC\XYZ\1.txt';
unlink $path; #this works

How do I unlink something like this,
$path = "C:\Users\ABC\XYZ\1.txt";
unlink $path; #this does not work

I thought it would solve the problem once I use your solution, but it didnt. Any thoughts?

Sorry, I'm stumped. After replacing the " with ' I don't see any difference in the paths. I don't use Windows so can't test unlinking in Windows.

I just tested this on Windows and the replacement works without a problem. However not escaping the '\'s when using '"' seems to cause a lot of problems. Try escaping all your '\'s.

Hi d5e5,
Long time. Nice solutions. It worked just as you intended. Tested it on Window OS using both ActivePerl and StrawberryPerl.

Hi replic,
Nice work but I didn't get your point here.

However not escaping the '\'s when using '"' seems to cause a lot of problems. Try escaping all your '\'s.

I don't think one really have to do that in this solution or to make this work. It works for me anyway without escaping the '\'s.

Hi Aditya_MUHAHA,
Must the $path contain the single quote before "unlinking them"? Or you need a functioning script?
Like, I said above d5e5 script worked when in the context in which you used it, only that
the content of the path variable 'C:\Users\ABC\XYZ\1.txt' is not seen.
To get the this gist use d5e5 like so:

#!/usr/bin/perl
use warnings;
use strict;

my $path = <DATA>;
chomp($path);
print 'Original path: ', $path, "\n";

$path =~ s/"/'/g;#Replace all " with '
print 'Changed path: ', $path, "\n";

unlink $path or die "No file to unlink: $!";

__DATA__
"C:\Users\ABC\XYZ\1.txt"

You should have the message "No file to unlink ....."
Why? because there is no file as '....', the path has single quote '' to it.
So if you then change the line:

$path =~ s/"/'/g; to $path =~ s/"//g;

then see your request granted. The code below works.

#!/usr/bin/perl
use warnings;
use strict;

my $path = <DATA>;
chomp($path);
print 'Original path: ', $path, "\n";

$path =~ s/"//g; 
print 'Changed path: ', $path, "\n";

unlink $path or die "No file to unlink: $!";

__DATA__
"C:\Users\ABC\XYZ\1.txt"

Edited 4 Years Ago by 2teez

This article has been dead for over six months. Start a new discussion instead.