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

Hi all- I'm on Windows XP and I'm trying to copy a file from a server resource, named \\serv123 to my local machine. The path to the file in question is \\serv123\logs\log1.txt and I can't for the life of me get the simple script below to work. It errors saying there is no such file or directory. I copy the path into Start>Run and it finds it just fine. Server doesn't require me to add any extra credentials.
#!/usr/bin/perl use File::Copy; my $original_file = '\\serv123\logs\log1.txt'; my $new_file = "C:\Documents and Settings\user\desktop\newfile.log"; copy($original_file, $new_file) or die "Error: $!";

Replies are listed 'Best First'.
Re: Is there a trick to read file from \\server?
by roboticus (Chancellor) on Aug 19, 2011 at 20:02 UTC

    You forgot to use single quotes on the $new_file. However, Windows is happy with forward slashes, so you don't have to worry about the escaping and single-quoting if you just use:

    #!/usr/bin/perl use File::Copy; my $original_file = '//serv123/logs/log1.txt'; my $new_file = "C:/Documents and Settings/user/desktop/newfile.log"; copy($original_file, $new_file) or die "Error: $!";

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Is there a trick to read file from \\server?
by ikegami (Patriarch) on Aug 19, 2011 at 21:00 UTC

    No trick, you just assigning the wrong values to $original_file and $new_file.

    To $original_file, you assign the string

    \serv123\logs\log1.txt

    To $new_file, you assign the string

    C:Documents and SettingsSerdesktop ewfile.log

    Use use strict; use warnings;!!!

    The following string literals will produce the strings you want:

    my $original_file = '\\\\serv123\\logs\\log1.txt'; my $new_file = "C:\\Documents and Settings\\user\\desktop\\newfile.log +";
Re: Is there a trick to read file from \\server?
by grantm (Parson) on Aug 20, 2011 at 07:15 UTC

    Have you tried:

    #!/usr/bin/perl use File::Copy; my $original_file = '//serv123/logs/log1.txt'; my $new_file = "C:/Documents and Settings/user/desktop/newfile.log";

    Many years ago when I had to use Windows I found Perl was happy to accept / as a directory separator. I'm not 100% sure it works with UNC paths but it certainly used to work with the likes of C:/Temp and was a lot easier than doubling up the backslashes everywhere.