If the write process locks the file, you can try to open it in read-write mode and see if it works with ERROR_SHARING_VIOLATION as GetLastError (access via the special variable Perl $^E ).
For instance:
#! /usr/bin/perl use warnings; use strict; sub usage { "Usage: $0 file ..\n" } die usage unless @ARGV; foreach my $path (@ARGV) { print "$path: "; if (open my $fh, "+<", $path) { print "available\n"; close $fh; } else { print $^E == 0x20 ? "in use by another process\n" : "$!\n"; } }
Sample output from Dir100526Lt.pdf opened by an Adobe reader:
C: \ Users \ Greg \ Downloads> check-lock.pl Dir100526Lt.pdf setup.exe
Dir100526Lt.pdf: in use by another process
setup.exe: available
Keep in mind that at any time when you first check the status and then later act on the basis of the result of this test, you create a race condition. It seems that the worst thing that can bite you in your application is the following unsuccessful sequence:
- check video for availability as above
- Answer: available!
- meanwhile, the recorder starts and blocks the video
- in your program, you are trying to move the video, but it does not work with violation of the exchange.
source share