This tool would be cool if it worked flawlessly, and I am shamelessly fishing for help. Rather than Posting in SOPW I'm showing what I've got here so as not to duplicate the same code in two different parts of the Monastery.

Windows autoplay will run an application on a USB drive if set up there, but I don't want to. I want to leave autoplay alone so that it just harmlessly opens an explorer window when the drive is mounted. I have one drive (the "Dragon" drive referenced in the code) with a very cool free image program, the FastStone Image Viewer, portable edition, installed on it. When I had it plugged into a USB expansion doohicky I plugged another USB drive directly into a slot on the computer, and to my shock and amazement Windows "bumped" the already-inserted drive to a different volume letter! So I said to myself, "I haven't done any really specific Win32 perl scripting for a while, let me see if I can write code that will check for a drive with specific characteristics then, in this case, exec a command from the perl code to fire up the image viewer." Without knowing the drive letter.

What's really strange is that the code doesn't seem to iterate through the array @rmvbl unless I use reverse on it! It's the weirdest thing, I swear this code nearly had me tearing out my hair. Thus all the lines marked "# debugging".

To actually run the code you'll have to adapt it to a drive you have on hand and check for a characteristic that Win32::DriveInfo::VolumeInfo() can detect; the vars that receive the retvals of that call are named in a pretty self-explanatory way.

EDIT

I needed a few days to find other ways to mess up and not look at this code, and it came to me. I was returning undef inside the drive iteration loop instead of below it, where it needed to be.

One thing to note: the variable $VolumeName will not have a value for every drive. It's actually a property of the filesystem, not of the entire drive. A small distinction but important. Anyhow, I think that MS Windows-formatted USB drives won't have this property (I could be wrong). This drive, I had formatted on Gnu/Linux.

One final mystery remains unsolved, and maybe a reader knows something pertaining. The call to exec is never supposed to return if successful, according to Perl's documentation, it is just supposed to completely separate itself from the parent process. When I run this code from a terminal commandline , however, it hangs around until I close the spawned child. Why?

Here's the final code, corrected:

#!/usr/bin/env perl # Last modified: Sat Jun 07 2025 01:57:01 PM -04:00 [EDT] use strict; use v5.18; use utf8; use warnings; use Win32::DriveInfo; =head1 NAME DriveFinder =head1 SYNOPSIS To be executed via a desktop shortcut. Command in shortcut: C:\perl\perl\bin\perl.exe "C:/Program Files/DriveFinder" =cut sub survey { my @rmvbl = grep { Win32::DriveInfo::DriveType($_) == 2 ? $_ : undef } Win32::DriveInfo::DrivesInUse(); for my $drv (@rmvbl) { my ( $VolumeName, $VolumeSerialNumber, $MaximumComponentLength, $FileSystemName, @attr ) = Win32::DriveInfo::VolumeInfo($drv); return $drv .":" if $VolumeName eq "FirstFS"; } return undef; } my $DriveVol = &survey; # Maybe chdir to C:\Users\somia\OneDrive\Pictures? - makes no differen +ce. no warnings 'uninitialized'; while ( !exec($DriveVol.'/FS/FSViewer80/FSViewer.exe') ) { say qq[Plug the USB "Dragon" key drive into a USB slot],q[]; sleep 4; $DriveVol = &survey; } __END__ =pod =head1 Drive Types on Win32 0 - the drive type cannot be determined. 1 - the root directory does not exist. 2 - the drive can be removed from the drive (removable). 3 - the disk cannot be removed from the drive (fixed). 4 - the drive is a remote (network) drive. 5 - the drive is a CD-ROM drive. 6 - the drive is a RAM disk. =cut # vim: ft=perl et sw=4 ts=4 :
Jun 02, 2025 at 22:41 UTC

A just machine to make big decisions
Programmed by fellows (and gals) with compassion and vision
We'll be clean when their work is done
We'll be eternally free yes, and eternally young
Donald Fagen —> I.G.Y.
(Slightly modified for inclusiveness)


In reply to Almost cool: removable drive "finder" instead of windows autoplay by Intrepid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.