Simple opendir(), directory does not exist problem

Discussion in 'Perl Misc' started by theo22, Jan 19, 2004.

  1. theo22

    theo22 Guest

    Newbie question:
    Windows 2000, Perl v5.8.0 built for MSWin32-x86-multi-thread

    All I want to do is pass an argument to my script on the command line.
    The argument is the directory that gets passed to the opendir()
    function. When I run the script , I always get an error:
    Uncaught exception from user code:
    Directory D:\My Documents\ScriptTesting does not exist: No
    directory at D:\My Documents\ScriptTesting\GetDirList3.pl line 12.

    How can it say the directory does not exist if the file that it is
    referring to with the error, is in that directory? If I hard code the
    directory in the opendir() function, it works fine.

    Here is my code. You will notice it takes a directory and then spits
    it out to a .txt file. It works great except I can't pass the
    directory I want it to list through the command line:
    ******************************************
    #!/usr/bin/perl -w

    use strict;
    use diagnostics;

    #Tried a variable name for ARGV[0] and chomp as alternative, but it
    made no difference so I commented it all out.
    #my $DirPath=$ARGV[0];
    #chomp($DirPath);

    #Create directory handle so we can get a list of all the files in the
    directory.
    #opendir(MyDir, '$DirPath') || die "Directory $DirPath does not exist:
    $!";
    opendir(MyDir, '$ARGV[0]') || die "Directory $ARGV[0] does not exist:
    $!";

    #Create filehandle to dump list of directory files into a .txt file
    for reading.
    open(MyFile, ">My_Files.txt") || die "Cannot open My_Files.txt: $!";

    #Gets the list of only .txt files
    my @Files = grep (/\.txt$/, readdir MyDir);

    #Print each file in the directory to a .txt file. Prepend filenames
    with thier full path.
    foreach my $Files (@Files){
    $Files = "$ARGV[0]\\$Files";
    print MyFile "$Files\n";
    }

    print "My_Files.txt file created with list of all .txt files!\n";

    #Close directory and file handles.
    closedir(MyDir);
    close(MyFile);
    ****************************************

    Please Help. Here is the command that I used:
    C:>GetDirList3.pl "D:\My Documents\ScriptTesting"

    What am I doing wrong here?

    Thx!!
    theo22, Jan 19, 2004
    #1
    1. Advertising

  2. In article <>,
    theo22 <> wrote:
    :Windows 2000, Perl v5.8.0 built for MSWin32-x86-multi-thread

    :The argument is the directory that gets passed to the opendir()
    :function. When I run the script , I always get an error:
    :Uncaught exception from user code:
    : Directory D:\My Documents\ScriptTesting does not exist: No
    :directory at D:\My Documents\ScriptTesting\GetDirList3.pl line 12.

    :eek:pendir(MyDir, '$ARGV[0]') || die "Directory $ARGV[0] does not exist:
    :$!";

    Single-quotes don't interpolate, but double-quotes do. So you
    are trying to open a directory literally named $ARGV[0] and
    then complaining with a message that uses the -contents- of $ARGV[0]

    I suggest you just remove the single-quotes.

    If you choose to double-quote instead, then keep in mind that
    inside double-quotes, the backslashes in the string you provide
    are going to be interpreted as escape sequences. To use double-quotes
    you would have to quote-meta or use "\Q$ARGV[0]\E"
    --
    Disobey all self-referential sentences!
    Walter Roberson, Jan 19, 2004
    #2
    1. Advertising

  3. (theo22) wrote in news:6f1e0d54.0401191459.1cfc7a35
    @posting.google.com:


    > Please Help. Here is the command that I used:
    > C:>GetDirList3.pl "D:\My Documents\ScriptTesting"
    >
    > What am I doing wrong here?


    I did not look at the rest of your script, but you might want to try the
    following alternatives:

    C:>GetDirList3.pl "D:/My Documents/ScriptTesting"

    or

    C:>GetDirList3.pl "D:\\My Documents\\ScriptTesting"

    Sinan.

    --
    A. Sinan Unur
    (reverse each component for email address)
    A. Sinan Unur, Jan 19, 2004
    #3
  4. theo22

    Ben Morrow Guest

    (theo22) wrote:
    > opendir(MyDir, '$ARGV[0]') || die "Directory $ARGV[0] does not exist:
    > $!";


    This attempts to open a directory called $ARGV[0]. The double-quotes
    used for the die message interpolate the variable, those single quotes
    don't. You don't need to quote it at all; also, it is more usual to
    use uppercase for IO handles:

    opendir my $DIR, $ARGV[0] or die "Cannot open $ARGV[0]: $!";

    Ben

    --
    If you put all the prophets, | You'd have so much more reason
    Mystics and saints | Than ever was born
    In one room together, | Out of all of the conflicts of time.
    |----------------+---------------| The Levellers, 'Believers'
    Ben Morrow, Jan 19, 2004
    #4
  5. theo22

    Ben Morrow Guest

    -cnrc.gc.ca (Walter Roberson) wrote:
    > If you choose to double-quote instead, then keep in mind that
    > inside double-quotes, the backslashes in the string you provide
    > are going to be interpreted as escape sequences. To use double-quotes
    > you would have to quote-meta or use "\Q$ARGV[0]\E"


    Bzzzt! Wrong.

    A double quoted string != a regex.

    Ben

    --
    Heracles: Vulture! Here's a titbit for you / A few dried molecules of the gall
    From the liver of a friend of yours. / Excuse the arrow but I have no spoon.
    (Ted Hughes, [ Heracles shoots Vulture with arrow. Vulture bursts into ]
    /Alcestis/) [ flame, and falls out of sight. ]
    Ben Morrow, Jan 19, 2004
    #5
  6. theo22

    Tore Aursand Guest

    On Mon, 19 Jan 2004 14:59:57 -0800, theo22 wrote:
    > #opendir(MyDir, '$DirPath') || die "Directory $DirPath does not exist:
    > $!";
    > opendir(MyDir, '$ARGV[0]') || die "Directory $ARGV[0] does not exist:
    > $!";


    You need double quotes when dealing with variables, or - in this case - no
    quotes at all;

    opendir(MYDIR, $DirPath) or die "$!\n";
    opendir(MYDIR, $ARGV[0]) or die "$!\n";

    Try this script to see the difference;

    #!/usr/bin/perl
    #
    use strict;
    use warnings;

    my $variable = 'test';
    print '$variable' . "\n";
    print "$variable" . "\n";
    print $variable . "\n";


    --
    Tore Aursand <>
    "To cease smoking is the easiset thing I ever did. I ought to know,
    I've done it a thousand times." -- Mark Twain
    Tore Aursand, Jan 19, 2004
    #6
  7. theo22

    Uri Guttman Guest

    >>>>> "BM" == Ben Morrow <> writes:

    BM> -cnrc.gc.ca (Walter Roberson) wrote:
    >> If you choose to double-quote instead, then keep in mind that
    >> inside double-quotes, the backslashes in the string you provide
    >> are going to be interpreted as escape sequences. To use double-quotes
    >> you would have to quote-meta or use "\Q$ARGV[0]\E"


    BM> Bzzzt! Wrong.

    BM> A double quoted string != a regex.

    bzzzt! escape sequences exist in double quotish strings too. what do you
    call \n, \r, \t, etc?

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Jan 19, 2004
    #7
  8. theo22

    Ben Morrow Guest

    Uri Guttman <> wrote:
    > >>>>> "BM" == Ben Morrow <> writes:

    >
    > BM> -cnrc.gc.ca (Walter Roberson) wrote:
    > >> If you choose to double-quote instead, then keep in mind that
    > >> inside double-quotes, the backslashes in the string you provide
    > >> are going to be interpreted as escape sequences. To use double-quotes
    > >> you would have to quote-meta or use "\Q$ARGV[0]\E"

    >
    > BM> Bzzzt! Wrong.
    >
    > BM> A double quoted string != a regex.
    >
    > bzzzt! escape sequences exist in double quotish strings too. what do you
    > call \n, \r, \t, etc?


    Yes, of course. However,

    my $x = '\n\r\t';
    my $y = "$x";

    will not (as Walter suggested) give a string with any control
    characters in it. \Q$var\E *is* useful in regexen, as those escape
    sequences which are regexish rather than double-quoteish will be
    interpreted even if they came in from a variable.

    Ben

    --
    EAT
    KIDS (...er, whoops...)
    FOR
    99p
    Ben Morrow, Jan 19, 2004
    #8
  9. theo22

    Uri Guttman Guest

    >>>>> "BM" == Ben Morrow <> writes:

    BM> -cnrc.gc.ca (Walter Roberson) wrote:
    >> >> If you choose to double-quote instead, then keep in mind that
    >> >> inside double-quotes, the backslashes in the string you provide
    >> >> are going to be interpreted as escape sequences. To use double-quotes
    >> >> you would have to quote-meta or use "\Q$ARGV[0]\E"


    BM> my $x = '\n\r\t';
    BM> my $y = "$x";

    BM> will not (as Walter suggested) give a string with any control
    BM> characters in it. \Q$var\E *is* useful in regexen, as those escape
    BM> sequences which are regexish rather than double-quoteish will be
    BM> interpreted even if they came in from a variable.

    i agree walter said the wrong thing there. data is never interpolated,
    only literal \ and $ and such.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Jan 20, 2004
    #9
  10. theo22

    theo22 Guest

    Thank you all very much!!

    It was the single quote around my first use of ARGV[0] that screwed it
    up. When I removed it, it worked fine. I didn't notice that I didn't
    have any quotes around the second use of ARGV[0], which is why it was
    reporting the correct directory in the error message and confused me
    even more.

    Should be:
    opendir(MyDir, $ARGV[0]) || die "Directory $ARGV[0] does not exist:
    $!";

    NOT:
    opendir(MyDir, '$ARGV[0]') || die "Directory $ARGV[0] does not exist:
    $!";

    By the way, this command line entry worked with this solution:
    C:>GetDirList3.pl "D:\My Documents\ScriptTesting"


    Ben: I also changed my IO handles to all uppercase. Thank you!

    Tore: I ran your little test. It is somewhat more clear, however I
    don't see a difference between the double quotes and no quotes. The
    output is the same. Also, what is the period used for in that
    statement? I tried to take it out and the script of course failed. I
    guess it is the concatanation operator and it is combining the two
    arguments to the print statement? Hmmm... I've never seen that.
    Nice.
    theo22, Jan 20, 2004
    #10
  11. theo22

    Tore Aursand Guest

    On Tue, 20 Jan 2004 08:30:58 -0800, theo22 wrote:
    > Tore: I ran your little test. It is somewhat more clear, however I
    > don't see a difference between the double quotes and no quotes. The
    > output is the same.


    There is not difference, except that I guess Perl will _try_ to
    interpolate the value stored inside the double quotes. The lesson learned
    should be: Use double quotes when you _need_ to interpolate something. If
    you don't need to, don't use double quotes. If you're dealing with a
    variable, don't use _any_ quotes. IMO.

    > Also, what is the period used for in that statement? I tried to take it
    > out and the script of course failed. I guess it is the concatanation
    > operator and it is combining the two arguments to the print statement?


    You're right - it's the concatanation (?) operator. I almost constantly
    use it because of personal style;

    print "$variable\n";
    print $variable . "\n";

    The two lines above does excactly the same, but because my editor
    highlights everything inside double and single quotes, I prefer to keep
    variable names outside of quotes when possible.


    --
    Tore Aursand <>
    "Life is pleasant. Death is peaceful. It's the transition that's
    troublesome." -- Isaac Asimov
    Tore Aursand, Jan 20, 2004
    #11
  12. theo22 <> wrote:

    > Tore:



    Please form proper followups.

    If you have something to say about Tore's post, then say it in
    a followup to Tore's post.


    > I ran your little test. It is somewhat more clear, however I
    > don't see a difference between the double quotes and no quotes.



    I don't see any code at all, so it is unclear what are referring to.


    > The
    > output is the same.



    The output was the same this time, it could be different other times,
    as the Perl FAQ points out (assuming that the "quotes" you are
    referring to are like in the FAQ):

    What's wrong with always quoting "$vars"?



    > Also, what is the period used for in that
    > statement?



    What period?

    What statement?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 20, 2004
    #12
  13. theo22

    Anno Siegel Guest

    Tore Aursand <> wrote in comp.lang.perl.misc:

    [...]

    > You're right - it's the concatanation (?) operator. I almost constantly
    > use it because of personal style;
    >
    > print "$variable\n";
    > print $variable . "\n";
    >
    > The two lines above does excactly the same, but because my editor
    > highlights everything inside double and single quotes, I prefer to keep
    > variable names outside of quotes when possible.


    This is one thing that bothers me about so-called syntax hilighters.
    They all have limitations and get some things wrong. In the long run,
    there will be a tendency to adapt your style to suit your syntax
    hilighter's quirks. That is not a solid basis for a decision about
    programming style.

    Anno
    Anno Siegel, Jan 21, 2004
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brad
    Replies:
    1
    Views:
    26,304
    [MSFT]
    Jun 22, 2004
  2. Dave
    Replies:
    1
    Views:
    9,746
    Steven Cheng[MSFT]
    Oct 24, 2005
  3. Guest
    Replies:
    1
    Views:
    517
    Andrea D'Onofrio [MSFT]
    May 21, 2004
  4. Mike
    Replies:
    16
    Views:
    446
    Joe Smith
    Jan 19, 2004
  5. JJ
    Replies:
    3
    Views:
    298
    Josef Moellers
    Nov 25, 2004
Loading...

Share This Page