views:

389

answers:

3

My current thinking: I need a certain module that will let me access the USB device. Also, I need some kind of deamon thing that will notify my script of any incoming USB connection event. And then I simply use some regexp to find the file I want to copy and then do the copying, maybe with some file copy module.

But I searched CPAN with the keyword "USB" only to find there're indeed very few relevant modules around and that seemingly most promising module that is called Device::USB says it does not support Windows system. I'm running Windows XP SP3, btw.

I assume this sort of automation shouldn't be too hard with Perl. But I'm just stuck there.

Any ideas? or any other suggestions? Thanks :)

UPDATE

Things are not as easy as expected. I'm sure I have a lot more to learn. But based on my existing Perl knowledge, I've managed to write a make-do script. I'm here to learn. Please kindly comment where I can improve my code. Thanks like always :)

use warnings;
use strict;
use File::Find;
use File::Copy;

my $from = 'I:/'; #Volume letter for USB drive in my OS
my $to = 'E:\Copy-to';

while (1) {
    if (-e $from) {
        last;
    }else{
        sleep(30);
    }
}

my @files;
find(sub{ push @files, $File::Find::name if /pdf$/}, $from); #Find the PDF files in the USB drive for Copying experiment

for (@files){
    copy ($_,$to);
}
+1  A: 

The worst case is to poll your volumes to look for it (look for attached drives every few seconds). Something like this is rather difficult in Perl, unless you're willing to get your hands dirty & hook into the Windows event system.

The ideal is, obviously, hooking into Window's native USB notification subsystem from C. If your stick has CD-ROM emulation, you can put an autorun script on it. I think you're better off sticking with C, C++ or even C# for this type of thing. Perl is a second class citizen on Windows.

Pestilence
The amazing Win32::API will let you work with just about any DLL. http://search.cpan.org/dist/Win32-API/API.pm With that module and an understanding of the appropriate Win32 notfication API, no big deal.
daotoad
Another option would be Inline::C. http://search.cpan.org/dist/Inline/C/C.pod Here you can wrap the C API call in a subroutine callable from the other Perl code.
daotoad
These guys are right. If you mix a bit of C with this, it's totally doable.
Pestilence
@Pestilence and @daotoad, thank you for telling me this. Well, it looks likes things are not as easy as I was expecting. I'll try and see what I can do. Thanks guys :)
Mike
A: 

If someone (probably a Perl learner like me who likes to play around with Perl on Windows box) should stumble upon this post, I would like s/he see a nice solution to the core part of my problem that was generously provided by Corian from Perlmonks and would not be misled like when I first looked at @Pestilence (no offence :))to believe that Perl cannot handle the task in question beautifully. The truth is Perl is fully capable of handling this kind of job and do it very easily.

The following code is pasted (verbatim) from Corian's answer to my question posted at Perlmonks. The source is here

#!/usr/bin/perl -w
package main;
use strict;
use DBI;
use Data::Dumper;

my $dbh = DBI->connect('dbi:WMI:');

my $sth = $dbh->prepare(<<WQL);
    SELECT * FROM __instanceoperationevent
    WITHIN 5
    WHERE TargetInstance ISA 'Win32_LogicalDisk'
    and TargetInstance.Name != 'C:'
WQL
$sth->execute();

# loops forever
while (defined (my $row = $sth->fetchrow_arrayref())) {
    my $ev = $row->[0];

    # Windows XP only :(
    print "Class\t",$ev->Path_->Class,"\n";
    for (qw(Name DeviceID Caption MediaType InterfaceType)) {
        print join "\t", $_, ($ev->TargetInstance->{$_} || '<none>');
        print "\n";
        if ($ev->Path_->Class eq '__InstanceCreationEvent') {
            print "Device (letter) $ev->{TargetInstance}->{Name} added\n";
        }
    }
}

All credits go to Corian and I'd like to make it CW.

Mike
A: 

sir i use NOKIA 6233 IN A NOT WORK IN OPERAMINI IN DOWNLOAD TIME PLS SOL

PANKAJ