0

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.

4
Contributors
6
Replies
7
Views
4 Years
Discussion Span
Last Post by 2teez
0

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

1
#!/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"
Votes + Comments
Works
0

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?

0

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.

1

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.

1

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 by 2teez

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.