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

I'm running Perl on Win32 and have come across a strange problem. I know I'll probably get some replies saying to use some module instead of the method I am using but please help me figure out why this approach is failing.

I am trying to copy a file to another file and am using the 'system' function. This is the code: print "system returned ", system("copy", "\"$original_file\"", "\"$backup_file\""), "\n";

Now the weird thing is that this works on everyone's machine except for one person. He is running the same OS(win2K) but for some reason the copy is failing. The output he gets is this:

system returned 65280

I'm not sure if that return value is helpful at all and am not sure how else to really go about debugging this. Any help?

Thanks.

Replies are listed 'Best First'.
Re: Win32: Weird file copy failure using system
by halley (Prior) on Jan 06, 2005 at 20:13 UTC
    The COPY command is not a program in and of itself, but rather a built-in command implemented in CMD.EXE or other shell interpreter. There is no "COPY.EXE" file in MS-DOS or Windows. Therefore, the arg-list variety system(exe,args...) form of the command will not work. I don't know why some of your machines appear to succeed, and some fail outright.

    Try system($ENV{ComSpec}, '/c', 'copy', $orig, $target) or something like it. Or, as you expected, you should check out the module, File::Copy for a more portable solution.

    --
    [ e d @ h a l l e y . c c ]

      No, you don't need to do this. In Win32 Perl, system(@list) is identical to system("@list"). Both detect commands that are internal to cmd.exe or command.com and do what you described automaticly.

      - tye        

Re: Win32: Weird file copy failure using system
by meredith (Friar) on Jan 07, 2005 at 01:53 UTC

    The return value of system should be shifted to the right 8 bits ( $number>>8; ) to get the real return value. That winds up with 255. But, I can't find any info on the return codes for copy anyway.

    If there's some error message printed, you might add a "cmd /k " to the beginning, which should spawn a command window that will try the copy and go to a prompt, while your app waits. I don't have a Windows box nearby, so I can't test that. You might also try redirecting the output of the command into a file, but I also don't know if that works. Windows is too quirky for my tastes...

    mhoward - at - hattmoward.org
Re: Win32: Weird file copy failure using system (errors)
by tye (Sage) on Jan 07, 2005 at 03:11 UTC

    What error message does the COPY command print?

    It is much easier for the program to figure out why things failed if you do the copying from within Perl instead. The return value from system rarely says more than "the command failed" and if the command didn't print an error message, all you can do is guess.

    I'd use Win32API::File's CopyFile() or the portable File::Copy module.

    - tye        

Re: Win32: Weird file copy failure using system
by holli (Abbot) on Jan 06, 2005 at 20:15 UTC
    could be some kind of right related problem. what happens when the person enters the same command in the shell/dosbox?