Printing folder contents

Discussion in 'Perl Misc' started by Guest, Oct 27, 2003.

  1. Guest

    Guest Guest

    I'.m trying to make a simple program which would print the contents of c:\
    folder.
    (files and first sub-folders). Am i at the right track? (this doesn't work)

    #!/usr/bin/perl
    use strict;
    print "Content-Type: text/plain\n\n";
    my @folder = <c:\*>;
    foreach my $file (@folder)
    {
    print "$file\n";
    }
    # end
     
    Guest, Oct 27, 2003
    #1
    1. Advertising

  2. Guest

    Guest Guest

    And printing enviromental variables

    I'd also like to print out the
    enviromental variables. I understand that, keys %ENV i somehow related but
    how to use it?
     
    Guest, Oct 27, 2003
    #2
    1. Advertising

  3. Guest

    Ben Morrow Guest

    <mh> wrote:
    > I'.m trying to make a simple program which would print the contents of c:\
    > folder.
    > (files and first sub-folders). Am i at the right track? (this doesn't work)
    >
    > #!/usr/bin/perl
    > use strict;


    Good; you want to add
    use warnings;
    to that.

    > print "Content-Type: text/plain\n\n";


    Why? Perl != CGI.

    > my @folder = <c:\*>;


    When using Perl, always use / rather than \ for paths.

    > foreach my $file (@folder)
    > {
    > print "$file\n";
    > }
    > # end


    The usual Perl way to indicate end-of-program is
    __END__
    because perl understands that as well.

    Ben

    --
    For the last month, a large number of PSNs in the Arpa[Inter-]net have been
    reporting symptoms of congestion ... These reports have been accompanied by an
    increasing number of user complaints ... As of June,... the Arpanet contained
    47 nodes and 63 links. [ftp://rtfm.mit.edu/pub/arpaprob.txt] *
     
    Ben Morrow, Oct 27, 2003
    #3
  4. Guest

    Guest Guest

    Thank you, the problem was only this

    <c:\*>; => <c:\\*>;






    > > #!/usr/bin/perl

    >
    > I assume you are running this script on a Windows machine. So the above

    line
    > doesn't make much sense.
    >
    > > use strict;
    > > print "Content-Type: text/plain\n\n";
    > > my @folder = <c:\*>;

    >
    > Escape the '\' like this
    > my @folder = <c:\\*>;
    >
    > > foreach my $file (@folder)
    > > {
    > > print "$file\n";
    > > }

    >
    > Does your script work at command prompt? Try that first before you go into
    > making a CGI.
    > HTH
    > Kasp.
    > --
    > "Accept that some days you are the pigeon and some days the statue."
    > "A pat on the back is only a few inches from a kick in the butt." -

    Dilbert.
    >
    >
     
    Guest, Oct 27, 2003
    #4
  5. Guest

    Guest Guest

    Re: And printing enviromental variables

    And another problem solved too, thanks a lot!



    >
    > > I'd also like to print out the enviromental variables.

    >
    >
    > You should start a new thread when starting a new topic.
    >
    >
    > > I understand that, keys %ENV i somehow related but how
    > > to use it?

    >
    >
    > The keys in %ENV are the names of the environment variables, while the
    > values of %ENV are the values of the environment variables corresponding
    > to those keys. Read more by doing
    >
    >
    > perldoc perlvar
    >
    >
    > So to print all environment variables and their values you could do
    > something like this:
    >
    >
    > print "$_ => $ENV{$_}\n" for keys %ENV;
    >
    >
    > --
    > Cheers,
    > Bernard
    >
     
    Guest, Oct 27, 2003
    #5
  6. Guest

    Nataku Guest

    Re: And printing enviromental variables

    The %ENV hash contains all of the environmental variables that Perl
    knows about. The key of this hash is the name of the variable ( like
    HOME ) and the value is, naturally, the value contained within that
    variable.

    So, to get all the env variables, you can access this hash in any one
    of the standard ways.

    foreach( my $key ( keys %ENV ) ){
    print ( "$key -> ".$ENV{$key}."\n" );
    }

    or

    while( my ($key, $value) = each %ENV ){
    print ( "$key -> $value\n" );
    }

    The second is preferred as the first will dump all of the keys into a
    list. Normally the ENV hash isnt that big, but for large hashes this
    could consume a significant portion of memory - while the second
    simply goes through one at a time.

    I highly reccomend reading through the perldoc, it contains answers to
    many a question like this.

    <mh> wrote in message news:<3f9d0922$>...
    > I'd also like to print out the
    > enviromental variables. I understand that, keys %ENV i somehow related but
    > how to use it?
     
    Nataku, Oct 27, 2003
    #6
  7. Guest

    Roy Johnson Guest

    <mh> wrote in message news:<3f9d0827$>...
    > I'.m trying to make a simple program which would print the contents of c:\
    > folder.
    > (files and first sub-folders). Am i at the right track? (this doesn't work)
    >
    > #!/usr/bin/perl
    > use strict;
    > print "Content-Type: text/plain\n\n";
    > my @folder = <c:\*>;
    > foreach my $file (@folder)
    > {
    > print "$file\n";
    > }
    > # end


    How does it fail? What it should do is load up @folder with a listing
    of the files in c:\, and then print the listing.
     
    Roy Johnson, Oct 27, 2003
    #7
  8. Roy Johnson <> wrote:
    ><mh> wrote in message news:<3f9d0827$>...


    >> my @folder = <c:\*>;


    > How does it fail? What it should do is load up @folder with a listing
    > of the files in c:\,



    <glob> is "double quotish", so he has the equivalent of

    my @folder = glob "c:\*";

    which is the same as:

    my @folder = glob 'c:*'; # _no_ slashes

    when he wants either:

    my @folder = glob "c:\\*";

    or, better:

    my @folder = glob "c:/*";

    or, best:

    my @folder = glob 'c:/*';


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 27, 2003
    #8
  9. et>, <mh> says...
    > I'.m trying to make a simple program which would print the contents of c:\
    > folder.
    > (files and first sub-folders). Am i at the right track? (this doesn't work)
    >
    > #!/usr/bin/perl
    > use strict;
    > print "Content-Type: text/plain\n\n";
    > my @folder = <c:\*>;
    > foreach my $file (@folder)
    > {
    > print "$file\n";
    > }
    > # end



    Your syntax looks good (according to "The Camel"). Please be more
    specific; what do you mean by "this doesn't work" ??

    If you provide more details then perhaps someone can help you.
     
    Master Web Surfer, Oct 28, 2003
    #9
    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. Stephen

    synchronize folder contents

    Stephen, Jan 5, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    912
    P. Wills
    Jan 6, 2006
  2. Don Adams
    Replies:
    1
    Views:
    625
    Martin Honnen
    Mar 5, 2004
  3. Kamarulnizam Rahim
    Replies:
    4
    Views:
    247
    Robert Klemme
    Jan 28, 2011
  4. Amit
    Replies:
    0
    Views:
    223
  5. Replies:
    0
    Views:
    217
Loading...

Share This Page