On Nov 16, 1:12 pm, [EMAIL PROTECTED] (AndrewMcHorney) wrote:
> Hello
>
> I am trying to build a string that contains the following text "dir
> c:\ /S" so I can get a complete directory of all the files on drive C
> and put them into an array with the following line of code -
> @dir_list = 'dir c:\ /S`;
The backslash is the escape char.  If you need a literal \ in the
command, then you'll need to escape it by doubling it up i.e.,
my @dir_list = `dir c:\\ /S`;

>
> Right now I have the following working:
>
> However, it is now working:
>
> @dir_list = 'dir c: /S`; which gives me all the files in the
> directory that is being pointed to at the moment for C: and all the
> subdirectories.
>
> Andrew

Rather than using the backticks to execute the dir command which you
then need to parse, a better approach would be to use the File::Find
module (or one of its cousins).
http://search.cpan.org/search?query=file%3A%3Afind&mode=all

use strict;
use warnings;
use File::Find;

my @file_list;
my $dir = 'c:/';

find(\&file_listing, $dir);

sub file_listing {
   return if -d;  # skip over directory entries

   # add file name onto the array
   push @file_list, $_;

   # or add filename with path info onto array
   #push @file_list, $File::Find::name;
}

print scalar @file_list " files in/under $dir;


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to