I'm interested in writing a small script that will detect if a CD or CompactFlash card has been inserted into the system and perform some action when that occurs. I've tried a basic poll mechanism and have had some luck dismissing the "There is no disk in drive foo" dialog boxes with Win32::GuiTest, but even this can be annoying if you happen to be trying to type at the same time the dialog boxes are coming up.
Win32::ChangeNotify does not seem to be the answer since it pops up the dialog boxes as well.
I haven't done much on Windows doing Windows-specific tasks, so I was hoping that someone would have some wisdom out there to enlighten me. I just upgraded to ActiveState 5.8.2
Here's my code thus far:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Win32::GuiTest qw(FindWindowLike GetWindowText GetForegroundWindow + SetForegroundWindow SendKeys PushButton); use Thread; my $info; sub waitForSeconds { select (undef, undef, undef, $_[0]); } sub HandleDialog { my $max = shift; my $found = 0; my $tries = 0; while (($found < 2) && ($tries++ < $max)){ waitForSeconds(.2); my ($dialog) = FindWindowLike(0, "^perl.exe", ""); if ($dialog){ print "Found dialog!\n"; SetForegroundWindow($dialog); $found++; PushButton("Continue"); } else { print "Did not find dialog\n"; last; } } } sub checkdir { my $dir = shift; my $t = Thread->new(\&HandleDialog, 10); my @data = stat($dir); $t->join(); if (@data){ print Dumper(\@data); } else { warn "Could not stat $dir"; return; } opendir(DH, $dir) || warn "Could not open $dir"; while ((my $entry = readdir(DH))){ print "Got $entry from $dir\n"; } closedir(DH); } foreach my $arg (@ARGV){ checkdir($arg); }
In reply to Windows drive change notifications by bschmer
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |