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

Fellow Monks, Can anyone tell me why this doesn't work. or suggest something better please. Thnx. Ray
# Ray Espinoza # GetLogFiles.pl # This script will get the resonate log files from cgi346 and copy the +m to a folder on my machine. ###################################################################### +#################################################### #!D:\perl\bin\perl -w $listFile = "list.txt"; open(LIST,$listFile) || die "Can't open $listFile : $!"; while(<LIST>) { chomp($entry = $_); push (@machines, $entry); print @machines; } close(IN); foreach $machine (@machines) { system("copy \\\$machine\\c$\\Program Files\\Resonate\\logs\agent- +dir.$machine\agent-log C:\\resologs"); } close(IN);

Replies are listed 'Best First'.
(tye)Re: copying files with system()
by tye (Sage) on Jul 20, 2001 at 03:09 UTC

    To put spaces in a file name on a command line in Win32, you have to surround the entire file name in double quotes. This all gets a bit complicated so I'll try to be very clear. Other problems include:

    • Not always escaping your \ with another \
    • Not escaping a $ that you wanted to be literal

    Anyway, let's shorten this just a bit to make it easier to talk about. So, this code: system("copy \\\$mach\\c$\\Pgm File\\Reso\\log\agent-dir.$mach\agnt-log C:\\reslog"); sends the following string to system: copy \$mach\cPgm File\Reso\log^Ggent-dir.opus\agnt-log C:\reslog because

    • "\\" is the single character \
    • "\$" is the single character $ so "\$mach" is the same as '$mach'
    • "c$\" is the same as 'c'.$\ and $\ is undef which becomes an empty string
    • "\P" is the single character P
    • "\a" is the "alert" or "bell", that is, the CTRL-G character (displayed as ^G above)
    • "$mach" is the same as $mach with we've defined to be "opus" for this example
    But the string we want to send to system is: copy "\\opus\c$\Pgm File\Reso\log\agent-dir.opus\agnt-log" C:\reslog where we need the quotes (") because of the space between "Pgm" and "File". Otherwise, the "copy" command would think that it had gotten three arguments on the command line instead of two.

    So to put a " inside of a double-quoted string, we use \". So let's replace each \ with \\, opus with $mach, $ with \$, " with \", and then put the whole thing inside quotes: "copy \"\\\\$mach\\c\$\\Pgm File\\Reso\\log\\agent-dir.$mach\\agnt-log\" C:\\reslog"

    Now, we could make this a bit simpler by using single quotes instead of double quotes. Now, single quotes only have two special characters inside of them: ' and \. So we'd only need to replace each \ with \\ (and we never use ' so we don't have to replace it with \'). One wrinkle is that '$mach' is the same as "\$mach" but we want to use the value of the $mach variable, not the literal string '$mach'. But we can use string concatenation (.) to handle that: 'copy "\\\\' . $mach . '\\c$\\Pgm File\\Reso\\log\\agent-dir.' . $mach . '\\agnt-log" C:\\reslog' Now, you'll often hear that you don't have to double \ inside of single quotes. That is somewhat true. For simplicity, double quotes interpret "\P" as "P" so that you can always use \ to escape the next character inside of double quotes without having to remember exactly which characters require this or not. Also for simplicity, single quotes interpret '\P' as '\\P' since it is easy to remember the mear two characters that require escaping inside of single quotes.

    The problem with not doubling \ inside single quotes is that there are two cases where it doesn't work:

    1. At the end of the string ('c:\temp\' is an unterminated string constant)
    2. And when you want more than one \ in-a-row in your string
    and if you get in the habit of not doubling \ inside of single quotes, you'll probably end up trying this: 'copy "\\' . $mach . '\c$\Pgm File\Reso\log\agent-dir.' . $mach . '\agnt-log" C:\reslog' which gives you this string: copy "\opus\c$\Pgm File\Reso\log\agent-dir.opus\agnt-log" C:\reslog which is exactly what we wanted except for our double \ ("\\opus) now being single ("\opus). So you could use: 'copy "\\\\' . $mach . '\c$\Pgm File\Reso\log\agent-dir.' . $mach . '\agnt-log" C:\reslog' but I think that previous mistake would be very hard to spot when you where looking for why the code was failing and you'd end up wasting a lot of time trying to figure out the problem. So I avoid using '\x' when I mean '\\x' for this reason.

    But Perl has a huge number of quoting options and we've only scratched the surface. Other option is to use "double quote"-style quoting but using a delimiter other than the double quote ("). For example qq(x) is the same as "x". This has the advantage of letting get rid of the concatenations but we have to go back to escaping the one literal dollar sign ($) [but we don't have to go back to escaping the quotes (") since that is no longer our string delimiter]: qq(copy "\\\\$mach\\c\$\\Pgm File\\Reso\\log\\agent-dir.$mach\\agnt-log" C:\\reslog)

    Although Perl still offers lots of other choices of quoting, about the only one that gets much better than that is the only one where \ isn't special: uninterpolating so-called "here documents" (<<'END'). The problems (in this case) with strings specified via a "here document" is that they will always end in a newline and for uninterpolating "here documents", we can't just throw in $mach to get the value of that variable. So, although we can write:

    my $command= <<'COPY'; copy "\\$mach\c$\Pgm File\Reso\log\agent-dir.$mach\agnt-log" C:\reslog COPY
    we need to substitute in the value of $mach and strip the newline:
    my $command= <<'COPY'; copy "\\$mach\c$\Pgm File\Reso\log\agent-dir.$mach\agnt-log" C:\reslog COPY chomp $command; $command =~ s/\$mach/$mach/g; system $command;

    Now, life would be much simpler if we could just change the backslashes to forward slashes. Well, that works most places in Win32, but one of the exceptions is when sending things on the command line to tools that don't handle / as a path separator (usually because they use / for command-line options) and "copy" is such a command.

    So your best bet is to use File::Copy and forward slashes as others have discussed. But I wanted to try to help you get a little better handle on some of the complex and powerful Perl quoting methods.

    So, if there wasn't a good non-system alternative, I'd go with:

    my $source= "//$mach/c\$/Pgm File/Reso/log/agent-dir.$mach/agnt-log"; my $command= qq(copy "$source" C:/reslog); $command =~ s#/#\\#g; system $command;

            - tye (but my friends call me "Tye")
      Thanks a million, Tye, for breaking it down for me. I did feel linka lost yesterday with this whole thing. I really appreciate you taking the time to write this all out for me and helping me understand quoting in perl a lot better. ~Ray~
(ichimunki) Re: copying files with system()
by ichimunki (Priest) on Jul 19, 2001 at 23:24 UTC
    not all of your \ are \\ which may be causing some control characters to be sent to the shell. Also, you might consider File::Copy for copying files. You can then switch to "paths/like/this" which eliminates the need to escape the \'s at all. :)
Re: copying files with system()
by lshatzer (Friar) on Jul 19, 2001 at 23:25 UTC
      using this code:
      #!D:\perl\bin\perl use File::Copy; $listFile = "list.txt"; open(LIST, $listFile) || die "Cant open $listFile : $!"; while(<LIST>) { copy("\\\\$_\\c$\\testtxt.txt,C:\\resologs,"); } close(LIST);
      I get this error: Usage: copy(FROM, TO , BUFFERSIZE) at C:\scripts\perl\GetLogFiles2.pl line 9. Does anyone know what i am doing wrong?
        As ichimunki said in (ichimunki) Re: copying files with system(), you can just use regular unix type pathes such as copy("/testtxt.txt", "/resologs/");, to avoid having to escape the path for windows.

        update:
        Taken from CB: <tye> copy("file1,file2") is not the same as copy("file1","file2")
Re: copying files with system()
by cyocum (Curate) on Jul 19, 2001 at 23:46 UTC
    Also, I do not know where you get the filehandle IN. The only one I see opened is LIST. I could be being blind again though.
      That was a typo. sorry =)...i ment to use LIST.
        When dealing with errors in specific code, it's better to cut'n'paste rather than retyping, due to the whole typo problem. It's a pain to see a bunch of typos (not specifically your case) that are not a part of the code in question.
        -Syn0
Re: copying files with system()
by c (Hermit) on Jul 20, 2001 at 00:17 UTC
    Aside from using File::Copy, you may want to double check your escaped "\" and if your file is one entry per line, you wouldnt need to create an @array and then run through a foreach:
    #!D:\perl\bin\perl -w $listFile = "list.txt"; open(LIST, $listFile) || die "Cant open $listFile : $!"; while(<LIST>) { system("copy \\\\$_\\c$\\Program Files\\Resonate\\logs\\agent-dir.$_ +\\agent-log C:\\resologs"); } close(LIST);
      Thank you all for you help. It is much appreciated. ~Ray~