Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Problems executing a copy command on win32

by DaWolf (Curate)
on Jan 14, 2004 at 22:12 UTC ( [id://321399]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings, brothers and sisters.

First of all let me humbly apologize for this question. I probably hate more asking this kind of "it's-simple-why-can't-you-see-it" question than you like answering it, but sometimes I just don't get it...

I'm developing an extremely simple app for a friend.

It's suppose to just get some files on dir1 and - if they don't exist on dir2 - copy them there.

So, here's this incredible thing:
#!/usr/bin/perl -w use strict; my $path1 = "C:\\foo\\dir1"; my $path2 = "C:\\foo\\dir2"; opendir DIR, $path1 || die "\n\nUnable to open $path1. Error: $!\n\n"; my @files = readdir DIR; closedir DIR; for (my $f = 0; $f < scalar(@files); $f++) { # test to see if it's a dir if ($files[$f] ne "." and $files[$f] ne ".." and $files[$f] !~ /\/ +/) { unless(-e "$path2\\$files[$f]") { exec("copy $path1\\$files[$f] $path2\\") || die "\n\nUnabl +e to copy $files[$f]. Error: $!\n\n"; }
Well, in dir1 I have two files: abc.xls and abd.xls When I run the app it copies only the first file (abc.xls)! If I replace the exec() line for a simple print it works perfectly...

Can someone please explain this to me?

Thanks a lot for your patience,

my ($author_nickname, $author_email) = ("DaWolf","erabbott\@terra.com.br") if ($author_name eq "Er Galvão Abbott");

Replies are listed 'Best First'.
Re: Problems executing a copy command on win32
by Paladin (Vicar) on Jan 14, 2004 at 22:19 UTC
    If you read the docs on the exec command, it says:
    The "exec" function executes a system command and never returns-- use "system" instead of "exec" if you want it to return.
    Basicly, exec replaces your process with the one you are execing, so you never make it back to your loop.
      Damn me, I'm really sorry.

      I've just took a glance on exec syntax and specification. Downvote me, I deserve it.

      Now a legitimate question: both files are empty (they were created by another app I've made that just creates an Excel document), so they are 0 Kb each.

      Windows refuses to copy them as if they don't exist. Is there a workaround for this?

      Thanks and sorry again,

      Thanks, Paladin.

      my ($author_nickname, $author_email) = ("DaWolf","erabbott\@terra.com.br") if ($author_name eq "Er Galvão Abbott");
        Now a legitimate question: both files are empty (they were created by another app I've made that just creates an Excel document), so they are 0 Kb each.

        Windows refuses to copy them as if they don't exist. Is there a workaround for this?

        P:\test>md test P:\test>copy con test\fred ^Z 1 file(s) copied. P:\test>dir test Directory of P:\test\test 14/01/2004 22:32 0 fred 1 File(s) 0 bytes P:\test>md test2 P:\test>copy test\fred test2 1 file(s) copied. P:\test>dir test2 Directory of P:\test\test2 14/01/2004 22:32 0 fred 1 File(s) 0 bytes

        ?


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        Timing (and a little luck) are everything!

Re: Problems executing a copy command on win32
by kutsu (Priest) on Jan 14, 2004 at 22:25 UTC

    Since Paladin answered your question, I offer this advice

    Instead of doing that unreliable test for directory or file, why not use:

    if(-d $files[$f]) #exist and directory #or if(-f $files[$f]) #exist and file #or if(-s $files[$f]) #exist and greater then 0

    Update: added first line & -s as that might help

    "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

      Thanks a lot for this valuable tip, kutsu, ++.

      Best regards,

      my ($author_nickname, $author_email) = ("DaWolf","erabbott\@terra.com.br") if ($author_name eq "Er Galvão Abbott");
Re: Problems executing a copy command on win32
by borisz (Canon) on Jan 14, 2004 at 22:23 UTC
    Use system instead of exec. exec never returns. Beside that consider to use the File::Copy module to copy the files.
    Boris
      In the past, I had problems with File::Copy on win32 when copying large files from ntfs to fat32. I now use Win32::CopyFile, and this works always on windows.(but it is not portable)
      ---------------------------
      Dr. Mark Ceulemans
      Senior Consultant
      BMC, Belgium
        Where can I find Win32::CopyFile? I have looked on CPAN and I don't see it there. Thanks.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://321399]
Approved by Zaxo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-25 14:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found