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

I am making backups to two alternating USB sticks. The problem is that the sticks get different device letters although I use the same connection. This means that before making the backup I must check the letter on Explorer (I use Windows 8.1), then I have to feed it to the Perl script by hand; I want the script to find the letter all by itself.
I tried the following code but then I got a pop-up window telling me to insert a device:

my $dev = ''; $dev = 'G' if( -d 'G:\\af' ); $dev = 'H' if( -d 'H:\\af' );

Replies are listed 'Best First'.
Re: Find device letter
by Corion (Patriarch) on Mar 20, 2014 at 20:46 UTC

    You can tell Windows not to pop up that window:

    BEGIN { if ($^O =~ /\bMSWin32\b|\bcygwin\b/) { require Win32::API; Win32::API->import(); Win32::API->Import('kernel32', 'SetErrorMode', 'I', 'I'); my $errormode = SetErrorMode(1); # fetch old error mode SetErrorMode(1 | $errormode); # no AbortRetryFail messages any +more }; }; # Now, Windows won't ask you to insert a drive into an empty device

      FYI, it is a bit easier to use the SetErrorMode() that is provided more directly via Win32API::File (a module that is even "core").

      use Win32API::File qw< SetErrorMode SEM_FAILCRITICALERRORS >; SetErrorMode( SEM_FAILCRITICALERRORS() | SetErrorMode(0) );

      - tye        

Re: Find device letter
by wind (Priest) on Mar 21, 2014 at 01:02 UTC
    Can also work off the list of available drives: (tye)Re: List of Mounted Drives on Win32
    use strict; use Win32API::File qw(getLogicalDrives GetVolumeInformation ); my @drives = getLogicalDrives(); for my $d (@drives) { my @x = (undef)x7; GetVolumeInformation( $d, @x ); print "$d $x[0] ($x[5])\n"; }
    - Miller