List directory contents

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • heaveninflames
    New Member
    • Oct 2008
    • 4

    List directory contents

    Hi all,

    I've searched the net for a while, and tried a lot of things. The script I wrote prints the contents of a directory. It works fine for a directory with no space in it, but with a space it doens't. I tried putting in qoutation marks, but that doesn't help. Anyone some ideas?

    Code:
    #!/usr/bin/perl -w
    my $dir = 'c:\Documents and Settings\\\';
    @files = <$dir*>;
    foreach $file (@files) {
    if (-d $file) {
      print "$file [DIR]\n";
      } else {
      my $filesize = -s $file;
      print "$file [$filesize]\n";
      }
    }
  • vijayarl
    New Member
    • Sep 2008
    • 65

    #2
    Hi there,

    Are you talking abt the directory name having space in it is it ???

    like this one :
    Code:
    dir name: xyz abc

    Regards,
    Vijayarl

    Comment

    • nithinpes
      Recognized Expert Contributor
      • Dec 2007
      • 410

      #3
      Try this:
      Code:
      @files = <"$dir*">; # $dir* within quotes
      - Nithin

      Comment

      • heaveninflames
        New Member
        • Oct 2008
        • 4

        #4
        Vijayarl: Yes

        Originally posted by nithinpes
        Try this:
        Code:
        @files = <"$dir*">; # $dir* within quotes
        - Nithin
        Nope, doesnt't work. There's no output at all now

        Comment

        • vijayarl
          New Member
          • Sep 2008
          • 65

          #5
          try this :

          Code:
          my $dir = 'C:/Performance_svap/INPUT FILES'; ### give your directory path here
          opendir(DIR, $dir) || die "can't opendir $dir: $! \n"; 
          @tmparray = readdir(DIR);
          print "@tmparray \n";
          Regards,
          Vijayarl

          Comment

          • vijayarl
            New Member
            • Sep 2008
            • 65

            #6
            hi,

            and even if you want can go through this post :

            http://bytes.com/forum/thread844454.ht ml

            jeff has clearly explained how script works & even it avoid reading . & .. (dot & dot dot) contents from the directory.

            have a try !!!

            Regards,
            Vijayarl

            Comment

            • heaveninflames
              New Member
              • Oct 2008
              • 4

              #7
              Yes!!! U made my day, thanks a lot!

              Comment

              Working...