Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

-e on windows giving error

by Rudolf (Pilgrim)
on Nov 17, 2011 at 22:51 UTC ( [id://938695]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, Im having some trouble understanding the -e test on windows Im on win7 but I know my problem is on all windows. my objective here is to get the drive which my perl script lies in, but each time I preform a test windows gives me an annoying popup saying perl.exe No Disk .. if there is a better method I would appriciate your wisdom! Here is my code: Thank you!

sub get_drive{ if(-e 'A:\\extracted'){$drive = "A:";} elsif (-e 'B:\\extracted'){$drive = "B:";} elsif (-e 'C:\\extracted'){$drive = "C:";} elsif (-e 'D:\\extracted'){$drive = "D:";} elsif (-e 'E:\\extracted'){$drive = "E:";} elsif (-e 'F:\\extracted'){$drive = "F:";} elsif (-e 'G:\\extracted'){$drive = "G:";} elsif (-e "H:\\extracted"){$drive = "H:";} elsif (-e "I:\\extracted"){$drive = "I:";} elsif (-e "J:\\extracted"){$drive = "J:";} elsif (-e "K:\\extracted"){$drive = "K:";} elsif (-e "L:\\extracted"){$drive = "L:";} elsif (-e "M:\\extracted"){$drive = "M:";} elsif (-e "N:\\extracted"){$drive = "N:";} elsif (-e "O:\\extracted"){$drive = "O:";} elsif (-e "P:\\extracted"){$drive = "P:";} elsif (-e "Q:\\extracted"){$drive = "Q:";} };

Replies are listed 'Best First'.
Re: -e on windows giving error
by BrowserUk (Patriarch) on Nov 17, 2011 at 23:28 UTC
    my objective here is to get the drive which my perl script lies in,

    If you inspect the built-in variable, $0 (that's zero not oh), it will tell you the full path of the current script including the drive. Eg:

    print $0;; C:\Perl64\bin\p1.pl

    So, my( $drive ) = $0 =~ m[^(.)]; will give you the drive letter directly.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Oh man, thank you so much everyone! I have found my solution and learned a lot today, Im new to perlmonks and already find the community very welcoming and helpfull! thanks and best wishes, Rudy

        If for some obscure reason you do not use file associations to invoke your scripts, and are in the habit of using tortuous relative path constructions to find them, then a simple step would sort out the mess you (probably didn't) create:

        c:\test>perl ..\tmp\..\test\..\Perl64\site\.\lib\..\..\bin\p1.pl Perl> print scalar Win32::GetFullPathName( $0 );; c:\Perl64\bin\p1.pl

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The solution BrowserUk rarely works. It requires an absolute path when launching the script (common for some, rare for others), and then only if the absolute path starts with something of the form "D:" (extremely common).

        I've posted a more robust version here. It handles relatives paths, and even more exotic drive volume names.

Re: -e on windows giving error
by ikegami (Patriarch) on Nov 17, 2011 at 23:06 UTC

    I get no such popup.

    Who built your Perl? What version is it? (I used ActivePerl v5.14.0.) I think there's a system call that configures the behaviour. (I can't remember what it's called.) There could be differences between our builds in how it's called.

    Why kind of drive causes the popup? (I have a CD drive but no diskette drive.)

    By the way, ever hear of loops? They are used to avoid having long lists of nearly identical code.

    Update: Forgot to address your actual goal!

    my objective here is to get the drive which my perl script lies in

    use Path::Class qw( file ); my $vol = file($0)->absolute()->volume(); $vol = "$1:" if $vol =~ /^(?:\\\\\?\\)?([a-zA-Z])\z/; $vol = uc($vol) if $vol =~ /^[a-zA-Z]:\z/;
Re: -e on windows giving error
by runrig (Abbot) on Nov 17, 2011 at 23:13 UTC
      well I am using activePerl, and whenever a drive does not exist it causes the popup each time untill it gets to the drive that does exist. and yeah I will loop it haha, thanks I am trying to use SetErrorMode() from Win32API::File but I keep getting the error of undefined subroutine at the function call..
        Either use the fully-qualified name of the function, or import it in the use statement.
        # Either this: use Win32API::File; my $uOldMode = Win32API::File::SetErrorMode( $uNewMode ); # or this: use Win32API::File qw( SetErrorMode ); my $uOldMode = SetErrorMode( $uNewMode );

        I presumed you neither imported SetErrorMode nor used its full name?

        use Win32API::File qw( SetErrorMode SEM_NOOPENFILEERRORBOX ); SetErrorMode( SetErrorMode(0) | SEM_NOOPENFILEERRORBOX );
Re: -e on windows giving error
by Util (Priest) on Nov 17, 2011 at 23:18 UTC
    Reorder your letters to put 'A' at the end. This is an improvement, not a perfect solution; you will only get the pop-up if `extracted` is not found at all.
    sub get_drive { for my $letter ( 'B' .. 'Q', 'A' ) { return "$letter:" if -e "$letter:/extracted"; } return; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://938695]
Approved by ikegami
Front-paged by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-18 19:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found