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
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:
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:
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
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:
- tye (but my friends call me "Tye")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;
In reply to (tye)Re: copying files with system()
by tye
in thread copying files with system()
by RayRay459
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |