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

I have a perl program executed through a tk gui on windows. The program currently moves a folder. I am expanding its functionality to copy the folder and contents while retaining all folder/file permissions/time stamps.

I was using the http://www.perlmonks.org/?node_id=804509 example but it is not working. When executed the program exits with error "child exited with value 1" and the new folder is not created. If I run the program with $self->{bareboard} set to 0 it runs the else statement and renames the folder correctly so my $self->{publicDir} and $oldPublic variables are correct.

Any help would be greatly appreciated!
if ( $self->{bareboard} ) { my @args = ('cp', '-cdpPr', $self->{publicDir}, $oldPublic); system(@args); if ($? == -1) { DDialog->new( "ERROR", "failed to execute: $!\n" ); DDialog->destroy(); } elsif ($? & 127) { my $t1 = ($? & 127); my $t2 = ($? & 128) ? 'with' : 'without'; DDialog->new( "ERROR", "died w sig $t1, $t2 coredump\n" ); DDialog->destroy(); } else { my $t1 = $? >> 8; DDialog->new( "ERROR", "child exited with value $t1\n" ); DDialog->destroy(); } } else { rename $self->{publicDir}, $oldPublic; }

Replies are listed 'Best First'.
Re: Copy Folder Preserve Attributes
by NetWallah (Canon) on Dec 08, 2017 at 20:08 UTC
    the -c option is invalid:
    >: cp -cdpPr blah blah.1 cp: invalid option -- 'c'
    Update: Doh! Missed the part where the OP said it was tk on windows.

                    All power corrupts, but we need electricity.

      Thanks for the feedback! I tried removing -c but it doesn't work. Even if I remove all of the flags except for -r it still fails.

      Your comment got the creative juices flowing in a different direction though. I ended up changing the command to use robocopy instead of cp and it finally worked.
      my @args = ('robocopy', '/mir', $self->{publicDir}, $oldPublic);
      Most of my perl code is written for linux so maybe this was a rookie mistake trying to run cp using a system command in a perl program run in windows.

        Since cp is not a builtin Windows command, it wasn't going to work, no matter what options you passed to it, unless you had something like gnuwin32 CoreUtils installed.

        When I ran a stripped down version of your code, with or without use warnings; use strict; (in my instance, calling a non-existent executable zzzcp, since I have CoreUtils' cp in my path), STDERR informed me that

        'zzzcp' is not recognized as an internal or external command, operable program or batch file.
        Thus, your tk gui is apparently not trying to capture your script's STDERR and log it somewhere useful. You may want to consider doing that, because if you had been, the error would have been with the rest of your error log. This may help you catch future errors, especially if you're shelling out for more than just the recursive copying. Search for "redirect" in open to see how to redirect STDERR to a file, and "in-memory files" in case you wanted to redirect STDERR to a string, so that you can then use your DDialog methods to do the logging.