Building a string to contain a "/"

Building a string to contain a "/"

am 16.11.2007 22:12:52 von AndrewMcHorney

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`;

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


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Building a string to contain a "/"

am 17.11.2007 22:16:49 von Mathew

You have to escape it with another backslash. 'C:\\ /S'

Keep up with my goings on at http://theillien.blogspot.com

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`;
>
> 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
>
>

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Building a string to contain a "/"

am 18.11.2007 00:02:35 von Ron Bergin

On Nov 16, 1:12 pm, andrewmchor...@cox.net (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: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Building a string to contain a "/"

am 18.11.2007 00:47:08 von Gunnar Hjalmarsson

AndrewMcHorney wrote:
> I am trying to build a string that contains the following text "dir c:\
> /S"

Use single quotes:

my $command = '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`;

my @dir_list = `$command`;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/