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

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.

Replies are listed 'Best First'.
Re^2: How to read files in a directory, directory path to be given through CMD
by ckj (Chaplain) on Jun 11, 2012 at 19:15 UTC
    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.