Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

How to read files in a directory, directory path to be given through CMD

by ckj (Chaplain)
on Jun 11, 2012 at 18:23 UTC ( #975612=perlquestion: print w/ replies, xml ) Need Help??
ckj has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I know for some of you it's stupid one but I am really stucked here. I want to code something where after running script it should ask for path from the user, after entering path and hitting enter it should read all the files listed in that directory line by line.
@files = <*>; =foreach $file (@files) { if (-f $file) { print "This is a file: " . $file."\n"; } if (-d $file) { print "\n\nThis is a directory: " . $file."\n"; } }

Comment on How to read files in a directory, directory path to be given through CMD
Download Code
Re: How to read files in a directory, directory path to be given through CMD
by kennethk (Monsignor) on Jun 11, 2012 at 18:55 UTC
    If I intuit your intent properly, there are two changes you need to make:
    1. =foreach needs to be foreach. You have made a syntax error that makes the interpreter think you are entering a documentation mode (see perlpod), thus no useful error message.
    2. Rather than <*>, which operates as a glob, you mean <>, which reads off STDIN.
    So a functional version of your code might be
    @files = <>; chomp @files; # Since there are trailing newlines foreach $file (@files) { if (-f $file) { print "This is a file: " . $file."\n"; } if (-d $file) { print "\n\nThis is a directory: " . $file."\n"; } }
    or, if I wrote it,
    use strict; use warnings; my @files = <>; chomp @files; foreach my $file (@files) { if (-f $file) { print "This is a file: $file\n"; } if (-d $file) { print "\n\nThis is a directory: $file\n"; } }
    What materials are you using to learn Perl? There are a number of great resources, including some free ones. For thorough coverage of available learning resources, see http://learn.perl.org.

    Update: Corrected omitted chomp.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Sorry, but have you run your code? I ran it and on entering path name after running my perl script it says :
      C:\Perl64\bin>perl t1.pl C:\Perl64\bin Can't do inplace edit: C:\Perl64\bin is not a regular file at t1.pl li +ne 20.
        Ahh, so you do not intend STDIN, but rather @ARGV for your input. In that case, you might use
        use strict; use warnings; foreach my $file (@ARGV) { if (-f $file) { print "This is a file: $file\n"; } if (-d $file) { print "\n\nThis is a directory: $file\n"; } }

        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

        Can't do inplace edit: C:\Perl64\bin is not a regular file at t1.pl line 20.

        How can you possibly be getting a error on line 20 of kennethk's code, when neither version of the code he posted has 20 lines?


        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 start of some sanity?

Re: How to read files in a directory, directory path to be given through CMD
by stevieb (Hermit) on Jun 11, 2012 at 19:11 UTC

    How about something like this:

    #!/usr/bin/perl use warnings; use strict; use File::Find; my $path; if ( $ARGV[0] ){ $path = $ARGV[0]; } else { print "Enter path: "; chomp (my $path = <STDIN> ); } find( \&wanted, $path ); sub wanted { print "File: $_\n" if -f; print "Dir: $_\n" if -d; }

    Output:

    steve@ubuntu:~/devel/repos/scripts/file_find$ ./find.pl Enter path: test Dir: . File: b.txt File: a.txt Dir: .hidden Dir: a Dir: b

    Update: Fixed so it allows either a command line arg, and if not present, prompts the user for the path.

    Update2: This code actually seems broken since I made the first update, but I don't have time to look at the issue. This is an informational warning only, until I get a chance to look closer.

      C:\Perl64\bin>perl t1.pl Enter path: C:\Perl64\bin Undefined subroutine &main::find called at t1.pl line 18, <STDIN> line 1. :( While I got the solution :
      $dir =<>; chomp($dir); opendir DIR, $dir or die "cannot open dir $dir: $!"; my @file= readdir DIR; print @file; closedir DIR;
      UPDATE: I tried with almost all the codes and most of them are working fine along with what I have pasted here. But the issue is that for some complex path it's not working.

        Post the contents of the script. My 'find' line is on line 18, not 23.

Re: How to read files in a directory, directory path to be given through CMD
by ansh batra (Friar) on Jun 11, 2012 at 19:39 UTC
    print "enter path\n"; $path = <>; chomp($path); opendir (DIR, $path) or die $!; while (my $file = readdir(DIR)) { if(-f $file) { print "this is a file--->".$file."\n"; } } closedir(DIR);
Re: How to read files in a directory, directory path to be given through CMD
by bulk88 (Priest) on Jun 11, 2012 at 22:29 UTC
    use strict; use warnings; my @files = `dir /b`; foreach(@files){ chomp($_); print $_."\n"; }
    Windows only.
Re: How to read files in a directory, directory path to be given through CMD
by fishmonger (Monk) on Jun 11, 2012 at 23:24 UTC
    #!/usr/bin/perl use 5.10.0; use strict; use warnings; print "Enter a directory: "; chomp(my $dir = <>); $dir =~ s!(/|\\)$!!; while (my $entry = <$dir/*>) { my $type = -f $entry ? 'a file' : -d $entry ? 'a dir' : 'unknown'; say "$entry is $type"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://975612]
Approved by kennethk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2014-04-19 15:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls