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:

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

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")

In reply to (tye)Re: copying files with system() by tye
in thread copying files with system() by RayRay459

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.