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

I am trying to copy a file from my windows web server to a windows info server.

My destination file works great if no space in the directory name. But if I copy to a directory that has a space in its name it wont copy:
my $destinationFile = "\\\\myinfoserver\\abc\\the directory\\";
Here is my copy command:
system("copy $sourceFile $destinationFile > nul") == 0 || die "Databa +se did not copy: $!";
Please advise how I get this to copy using the system command. If I didnt have a space in the directory name it would work.

Replies are listed 'Best First'.
Re: Desination file with space in directory name
by Hue-Bond (Priest) on Jul 28, 2006 at 18:20 UTC

    Why don't you use File::Copy? It's as simple as:

    use File::Copy; copy "a b", "c d";

    --
    David Serrano

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Desination file with space in directory name
by swampyankee (Parson) on Jul 28, 2006 at 19:28 UTC

    system uses the shell. You've got two options: as several people have already suggested, use File::Copy (probably the best course) or quote the source and destination files. I believe this is as simple as

    $destinationFile = '"' . $destinationFile . '"';

    emc

    Outside of a dog, a book is man's best friend. Inside of a dog it's too dark to read.

    Groucho Marx
Re: Desination file with space in directory name
by BrowserUk (Patriarch) on Jul 28, 2006 at 18:36 UTC

    Try

    system( qq[copy "$sourceFile" "$destinationFile" > nul] ) == 0 || die "Database did not copy: $!";

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Desination file with space in directory name
by bobf (Monsignor) on Jul 30, 2006 at 04:42 UTC

    my $destinationFile = "\\\\myinfoserver\\abc\\the directory\\";
    Holy backslashes, Batman!

    Please take a look at File::Spec. It will require a few extra characters to construct a path name, but it will take care of the directory separators for you so you don't have to escape them manually. It will also do it portably, so if you ever run this on a different OS you won't have to scan through your code to check how the paths were constructed. It's got a great little set of methods for manipulating file and path names, and I've found it to be a good little module to know. It's also part of the core distribution, so there is no extra installation required. :-)