Perl substitution working on some machines but not others?

T

therocket79

Hi,

I have a question about the following line of code:

$new_file =~ s/^$from_dir/$to_dir/;

I have 2 variables $from_dir and $to_dir which are different directory
path names. I also have the $new_file variable which is the full path
of a file. I am trying to substitute the first part of the file path
($from_dir) with a new file path ($to_dir), following on from this I
copy the original file to the new location.

An example:

$new_file = C:/directory1/file.txt
$from_dir = C:/directory1
$to_dir = C:/directory2

I would like the result of the substitution to be $new_file =
C:/directory2/file.txt, so I can then copy C:/directory1/file.txt to
C:/directory2/file.txt

However, this seems to work on some machines but fail on others. The
reason being the substitution doesn't seem to take place correctly and
I end up trying to copy something to the same location. Can anyone
help with why this is happening?
 
A

anno4000

Hi,

I have a question about the following line of code:

$new_file =~ s/^$from_dir/$to_dir/;

I have 2 variables $from_dir and $to_dir which are different directory
path names. I also have the $new_file variable which is the full path
of a file. I am trying to substitute the first part of the file path
($from_dir) with a new file path ($to_dir), following on from this I
copy the original file to the new location.

An example:

$new_file = C:/directory1/file.txt
$from_dir = C:/directory1
$to_dir = C:/directory2

I would like the result of the substitution to be $new_file =
C:/directory2/file.txt, so I can then copy C:/directory1/file.txt to
C:/directory2/file.txt

However, this seems to work on some machines but fail on others. The
reason being the substitution doesn't seem to take place correctly and
I end up trying to copy something to the same location. Can anyone
help with why this is happening?

Too little info. What is the exact code that does (or doesn't do) the
substitution? What is the content of $from_dir, $to_dir and $new_file
in a case where it works and in a case where it doesn't? Also name the
OS and its version for a working case and one that doesn't work.

Anno
 
T

therocket79

Hi,

Thanks for your response. I've managed to get it wokring now, it was
actually to do with forward and backslashes being included in file
paths inconsistently.

Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top