in reply to Re: selcting all files in a given directory except......
in thread selcting all files in a given directory except......

names of the files are not been printed on the terminal when script is excecuted
  • Comment on Re^2: selcting all files in a given directory except......

Replies are listed 'Best First'.
Re^3: selcting all files in a given directory except......
by glide (Pilgrim) on Dec 06, 2007 at 10:18 UTC
    try this script ...
    #!/usr/bin/perl use strict; use warnings; my $dir = $ARGV[0] || $ENV{HOME}; my @files; if (-d $dir){ opendir (my $DIR, $dir); @files = grep {/^(?!\.{1,2}\z)/} readdir($DIR); closedir($DIR); } else { die("can't open the dir, $dir\n"); } foreach my $file (@files) { print "$file\n"; }
    save the script as myls.pl, and run the script like this
    $ perl myls.pl /home
    ps. the $ENV{HOME} exist in the windows environment??
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re^3: selcting all files in a given directory except......
by ian (Beadle) on Dec 06, 2007 at 09:48 UTC
    When you ran the preceding example,
    • What input did you give it?
    • What was the expected output?
    • What platform/perl are you running?
    -- Ian Tegebo
      i had excuted with ./script test test is directory with test1 test2 file names it didnt print any filenames 2)list the names of all the files in a directory, except for "." and ".." 3)cshell/unix
Re^3: selcting all files in a given directory except......
by Gangabass (Vicar) on Dec 06, 2007 at 09:51 UTC

    Did you enter dir name and press Enter key?

      ./script test(directory name) enter i had done tht
        This might seem pedantic, but it appears that you are entering the directory name as a parameter on the command-line. This is placed into array @ARGV (hence the comment above).

        You program is reading from the standard-input stream (which is the keyboard by default), without a prompt.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re^3: selcting all files in a given directory except......
by cdarke (Prior) on Dec 06, 2007 at 10:02 UTC
    Maybe the opendir is failing. Modify that line to read:
    opendir (my $DIR, $dir) || die "Unable to open $dir: $!";

    then try again.
      this is the code which i hav been excecuting...... still unable to find out........
      #!/usr/bin/perl use strict; use warnings; chomp(my $dir = <STDIN>); my @files; if (-d $dir){ opendir (my $DIR, $dir) || die "Unable to open $dir: $!"; @files = grep { m/^(?!\.{1,2}\z)/ } readdir($DIR); closedir($DIR); } foreach my $file (@files) { print "$file\n"; }