Hi Wind,

Nice coding. Thanks for the tidy up.

I changed it a little. I changed 'die' to 'warn', because I wanted all the possibilities tested, and I only 'closed' where the open had been successful. I didn't know you could write "for .." like that and I've never got around to using the "? .. : .." operator. So thanks for all that.

So:

#!/usr/bin/perl -w use strict; for my $i (0..5) { my $dir = $i ? "/media/disk-$i/" : "/media/disk/"; if (opendir my $dh, $dir){ print $dir," opened successfully.\n"; closedir $dh; } else { warn "Can't opendir $dir: $!"; } }

So I inserted a USB-drive, ran the code and I got (Sorry, my machine is set to Portuguese. "Arquivo ou diretório não encontrado" means "file or directory not found ... ". ):

Can't opendir /media/disk/: Arquivo ou diretório não encontrado at /ho +me/image/Documents/Endoscopia/Dir_Test.pl line 10. Can't opendir /media/disk-1/: Arquivo ou diretório não encontrado at / +home/image/Documents/Endoscopia/Dir_Test.pl line 10. Can't opendir /media/disk-2/: Arquivo ou diretório não encontrado at / +home/image/Documents/Endoscopia/Dir_Test.pl line 10. Can't opendir /media/disk-3/: Arquivo ou diretório não encontrado at / +home/image/Documents/Endoscopia/Dir_Test.pl line 10. Can't opendir /media/disk-4/: Arquivo ou diretório não encontrado at / +home/image/Documents/Endoscopia/Dir_Test.pl line 10. Can't opendir /media/disk-5/: Arquivo ou diretório não encontrado at / +home/image/Documents/Endoscopia/Dir_Test.pl line 10.

before I used the dolphin file-manager and afterwards I got:

/media/disk/ opened successfully. Can't opendir /media/disk-1/: Arquivo ou diretório não encontrado at / +home/image/Documents/Endoscopia/Dir_Test.pl line 10. Can't opendir /media/disk-2/: Arquivo ou diretório não encontrado at / +home/image/Documents/Endoscopia/Dir_Test.pl line 10. Can't opendir /media/disk-3/: Arquivo ou diretório não encontrado at / +home/image/Documents/Endoscopia/Dir_Test.pl line 10. Can't opendir /media/disk-4/: Arquivo ou diretório não encontrado at / +home/image/Documents/Endoscopia/Dir_Test.pl line 10. Can't opendir /media/disk-5/: Arquivo ou diretório não encontrado at / +home/image/Documents/Endoscopia/Dir_Test.pl line 10.

So It looks as though I need to do something else. In fact if I do $dir I get the same result:

$ dir /media/disk/ dir: impossível acessar /media/disk/: Arquivo ou diretório não encontr +ado

Thanks again for a great post.

Regards

Steve


In reply to Re^2: Opening a USB drive for storing info. by Steve_BZ
in thread Opening a USB drive for storing info. by Steve_BZ

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.