in reply to Re^2: Reading progress of "copy" executed asynchronously
in thread Reading progress of "copy" executed asynchronously

I was not aware Windows reports incorrect filesizes. Then you have to rely on copy's reporting. Use this pipe-open form instead:

use strict; use warnings; my $cmd = 'copy.exe 'from', 'to', '/z' |'; open(EXE, $cmd) || die "$!"; while(<EXE>){ print "read from program output: ".$_."\n"; } print "done.\n"; close(EXE);

Or use IPC::Run or any other IPC::* module mentioned here or there.

Replies are listed 'Best First'.
Re^4: Reading progress of "copy" executed asynchronously
by neWerminder (Novice) on Aug 10, 2018 at 08:26 UTC
    Hello

    As for code it wont work. i tried earlier on and it gives output only when its done but i need to monitor progress

    As for IPC ill check

      This works for me on Windows:

      use strict; use warnings; use feature 'say'; use POSIX ":sys_wait_h"; use Capture::Tiny ':all'; my $cmd = 'cmd /c copy a1GBfile a1GBfile_copy /z'; my $out = IO::File-> new_tmpfile; capture_stdout { my $pid = system( 1, $cmd ); while () { last if waitpid( $pid, WNOHANG ); undef local $/; seek $out, 0, 0; my $s = <$out>; next unless $s and $s =~ m/.*\D(\d+)%/s; say STDERR "progress so far: $1 percent"; sleep 1; } } stdout => $out; close $out; __END__ progress so far: 0 percent progress so far: 60 percent progress so far: 70 percent progress so far: 80 percent progress so far: 89 percent progress so far: 99 percent

      I'd prefer to use filehandle opened on Perl scalar, but Capture::Tiny isn't happy about the idea. + You can tee_stdout instead of capture_stdout, to see "normal" output in console, and replace say STDERR ... line with real logic of what you trying to do. Or, to see if something is still happening, just check -s $out for change instead of slurping.

      You want to say that from cmd.exe copy 'from' 'to' /z reports on progress as it does the copy but when run via Perl's pipe-open it does not update the output until it finishes? In some OSs I find my luck runnning out faster than in others ...