using Perl, How to search for files in my computer

using Perl, How to search for files in my computer

am 11.06.2011 17:14:01 von eventual

--0-767027752-1307805241=:20005
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

Hi,=0AI am using windows7.=0AUsing Perl, how do I=A0search for files in my =
computer, eg to search in c:\ and all its sub-directories.=0AI only know ho=
w to use glob to search from a particular location.=0AThanks
--0-767027752-1307805241=:20005--

Re: using Perl, How to search for files in my computer

am 11.06.2011 17:31:39 von Shawn H Corey

On 11-06-11 11:14 AM, eventual wrote:
> Hi,
> I am using windows7.
> Using Perl, how do I search for files in my computer, eg to search in c:\ and all its sub-directories.
> I only know how to use glob to search from a particular location.
> Thanks

You can use File::Find, see `perldoc File::Find`. It is a standard
module and is installed along with Perl. For a list of all the
standards pragmatics and modules, see `perldoc perlmodlib`.


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

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

Re: using Perl, How to search for files in my computer

am 12.06.2011 12:40:05 von eventual

--0-189775536-1307875205=:45680
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

Thanks,=0AI've read perldoc File::Find but I dont understand.=0ASo If I wis=
h to search for "mp3" in d:\ and all its sub-directories, and to print it o=
ut if found,=0AHow should I write?  Thanks =0AFrom: Shawn H Corey =
=0ATo: beginners@perl.org=0ASent: Saturday, June 11,=
2011 11:31 PM=0ASubject: Re: using Perl, How to search for files in my com=
puter On 11-06-11 11:14 AM, eventual wrote:=0A> Hi,=0A> I am using win=
dows7.=0A> Using Perl, how do I search for files in my computer, eg to sear=
ch in c:\ and all its sub-directories.=0A> I only know how to use glob to s=
earch from a particular location.=0A> Thanks You can use File::Find, s=
ee `perldoc File::Find`.=A0 It is a standard module and is installed along =
with Perl.=A0 For a list of all the standards pragmatics and modules, see `=
perldoc perlmodlib`. =0A-- Just my 0.00000002 million dollars worth,=
  Shawn Confusion is the first step of understanding. Progra=
mming is as much about organization and communication=0Aas it is about codi=
ng. The secret to great software:=A0 Fail early & often. Eliminat=
e software piracy:=A0 use only FLOSS. -- To unsubscribe, e-mail: begin=
ners-unsubscribe@perl.org=0AFor additional commands, e-mail: beginners-help=
@perl.org=0Ahttp://learn.perl.org/
--0-189775536-1307875205=:45680--

Re: using Perl, How to search for files in my computer

am 12.06.2011 13:15:34 von Sayth Renshaw

On Sun, Jun 12, 2011 at 8:40 PM, eventual wrote:
> Thanks,
> I've read perldoc File::Find but I dont understand.
> So If I wish to search for "mp3" in d:\ and all its sub-directories, and =
to print it out if found,
> How should I write?
> Thanks
>
>
> From: Shawn H Corey
> To: beginners@perl.org
> Sent: Saturday, June 11, 2011 11:31 PM
> Subject: Re: using Perl, How to search for files in my computer
>
> On 11-06-11 11:14 AM, eventual wrote:
>> Hi,
>> I am using windows7.
>> Using Perl, how do I search for files in my computer, eg to search in c:=
\ and all its sub-directories.
>> I only know how to use glob to search from a particular location.
>> Thanks
>
> You can use File::Find, see `perldoc File::Find`.=A0 It is a standard mod=
ule and is installed along with Perl.=A0 For a list of all the standards pr=
agmatics and modules, see `perldoc perlmodlib`.
>
>
> -- Just my 0.00000002 million dollars worth,
> =A0 Shawn
>
> Confusion is the first step of understanding.
>
> Programming is as much about organization and communication
> as it is about coding.
>
> The secret to great software:=A0 Fail early & often.
>
> Eliminate software piracy:=A0 use only FLOSS.
>
> -- To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/

I am only learning myself. But from the docs this is the format we
should follow.

use File::Find;
finddepth(\&wanted, @directories_to_search);
sub wanted { ... }

So then I didn't fully understand the docs either, but found this at
perlmonks http://www.perlmonks.org/?node_id=3D217166 and the File::List
here at CPAN http://search.cpan.org/~dopacki/File-List-0.3.1/List/List.pm

My Stab at it


use File::List;

my $mp3 =3D new File::List("C:\MyMusic\");
my @musicFiles =3D @{ $mp3->find("\.mp3\$") };
@musicFiles =3D sort(@musicFiles);
foreach (@musicFiles)
{
print " $_ ";
}

Well something like that.

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

Re: using Perl, How to search for files in my computer

am 12.06.2011 13:28:24 von Sayth Renshaw

Actually got it working. You would need to use File::Find to search
sub directories however.

#!\usr\bin\perl
use warnings;
use strict;
use diagnostics;
use File::List;

my $mp3;
my @musiFiles;
$mp3 = new File::List("C:/Users/RenshawFamily/maven/Music/Foo Fighters");
my @musicFiles = @{ $mp3->find("\.mp3\$") };
@musicFiles = sort(@musicFiles);
foreach (@musicFiles)
{
print " $_ ", "\n";
}

Prints out all mp3 files and including the path to the file for example ouput.
C:/Users/RenshawFamily/maven/Music/Foo Fighters/Echoes, Silence,
Patience & Grace/Ballad of the Beaconsfield Miners.mp3
C:/Users/RenshawFamily/maven/Music/Foo Fighters/Echoes, Silence,
Patience & Grace/But, Honestly.mp3

Sayth

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

Re: using Perl, How to search for files in my computer

am 12.06.2011 13:35:14 von Shawn H Corey

On 11-06-12 06:40 AM, eventual wrote:
> I've read perldoc File::Find but I dont understand.
> So If I wish to search for "mp3" in d:\ and all its sub-directories, and to print it out if found,
> How should I write?

Try:

my @Music_files = ();

use File::Find;
find( \&want_mp3, 'd:\\' );
print "$_\n" for sort @Music_files;

sub want_mp3 {
push @Music_files, $File::Find::name if $_ =~ /\.mp3$/;
}


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

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

Re: using Perl, How to search for files in my computer

am 12.06.2011 13:49:14 von Shawn Wilson

--00248c0ee4065ddecd04a58264be
Content-Type: text/plain; charset=ISO-8859-1

On Jun 11, 2011 11:15 AM, "eventual" wrote:
>
> Hi,
> I am using windows7.
> Using Perl, how do I search for files in my computer, eg to search in c:\
and all its sub-directories.
> I only know how to use glob to search from a particular location.
> Thanks

I haven't seen any mention of find2perl so I figured I'd put it out there.
Wouldn't use it for production work but its good to template off of and its
good to try different options and see what it would do.

--00248c0ee4065ddecd04a58264be--

Re: using Perl, How to search for files in my computer

am 12.06.2011 13:52:24 von Sayth Renshaw

> Try:
>
> my @Music_files =3D ();
>
> use File::Find;
> find( \&want_mp3, 'd:\\' );
> print "$_\n" for sort @Music_files;
>
> sub want_mp3 {
> =A0push @Music_files, $File::Find::name if $_ =3D~ /\.mp3$/;
> }
>
>
> --
> Just my 0.00000002 million dollars worth,
> =A0Shawn
>
> Confusion is the first step of understanding.
>
> Programming is as much about organization and communication
> as it is about coding.
>
> The secret to great software: =A0Fail early & often.
>
> Eliminate software piracy: =A0use only FLOSS.
>

That was good to see Shawn. I kept working on it but mine was far more
convoluted, and didn't like spaces in directory names.

#!\usr\bin\perl
use warnings;
use strict;
use diagnostics;
use File::List;
use File::Find;

my $mp3;
my @musicFiles;
my $dirList;
my $dir =3D ("c:/");
my @dirList =3D find(sub {print if -d}, $dir);
# old file line $mp3 =3D new
File::List("C:/Users/RenshawFamily/maven/Music/Foo Fighters");
foreach $dirList (@dirList) {
@musicFiles =3D @{ $dirList->find("\.mp3\$") };
}
@musicFiles =3D sort(@musicFiles);
foreach (@musicFiles)
{
print " $_ ", "\n";
}

Sayth

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

Re: using Perl, How to search for files in my computer

am 12.06.2011 14:47:33 von John Delacour

At 03:40 -0700 12/06/2011, eventual wrote:

>I've read perldoc File::Find but I dont understand.
>So If I wish to search for "mp3" in d:\ and all its sub-directories,
>and to print it out if found,
>How should I write?

Just put the directory in $dir:


#!/usr/local/bin/perl
use strict;
use File::Find;
my $dir = "$ENV{HOME}/music";
my @mp3list;
find(\&LIST_PATHNAMES, $dir);
for (@mp3list){
print "$_\n\n";
}
sub LIST_PATHNAMES{
push @mp3list, $File::Find::name if /\.mp3$/;
}


JD

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

Re: using Perl, How to search for files in my computer

am 12.06.2011 20:38:16 von byulga

Sayth Renshaw wrote:
>> Try:
>>
>> my @Music_files = ();
>>
>> use File::Find;
>> find( \&want_mp3, 'd:\\' );
>> print "$_\n" for sort @Music_files;
>>
>> sub want_mp3 {
>> push @Music_files, $File::Find::name if $_ =~ /\.mp3$/;
>> }
>>
>>
>> --
>> Just my 0.00000002 million dollars worth,
>> Shawn
>>
>> Confusion is the first step of understanding.
>>
>> Programming is as much about organization and communication
>> as it is about coding.
>>
>> The secret to great software: Fail early& often.
>>
>> Eliminate software piracy: use only FLOSS.
>>
>>
> That was good to see Shawn. I kept working on it but mine was far more
> convoluted, and didn't like spaces in directory names.
>
> #!\usr\bin\perl
> use warnings;
> use strict;
> use diagnostics;
> use File::List;
> use File::Find;
>
> my $mp3;
> my @musicFiles;
> my $dirList;
> my $dir = ("c:/");
> my @dirList = find(sub {print if -d}, $dir);
> # old file line $mp3 = new
> File::List("C:/Users/RenshawFamily/maven/Music/Foo Fighters");
> foreach $dirList (@dirList) {
> @musicFiles = @{ $dirList->find("\.mp3\$") };
> }
> @musicFiles = sort(@musicFiles);
> foreach (@musicFiles)
> {
> print " $_ ", "\n";
> }
>
> Sayth
>
>
Hi Sayth, et. al.,

At work I'm stuck on Winblows, and this is one of the ways Strawberry
Perl (portable install) saves me lots of time. There's nothing wrong
with the other solutions presented; I'm just adding to the variety... I
use these one-liners if I need a quick search while playing in DOS
(doesn't sort, just prints as it gets 'em):

perl -MFile::Find -wle "find( sub{ /\.mp3$/i and print;}, q(.));"

perl -MFile::Find -wle "find( sub{ /\.mp3$/i and print
$File::Find::name;}, q(.));"

The first I tend to use when I just want an "existence check" and I
don't need the directory info.

\Brian

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

Re: using Perl, How to search for files in my computer

am 12.06.2011 23:22:52 von Shawn Wilson

On Sun, Jun 12, 2011 at 14:38, Brian F. Yulga wr=
ote:
>
> Hi Sayth, et. al.,
>
> At work I'm stuck on Winblows, and this is one of the ways Strawberry Per=
l
> (portable install) saves me lots of time. =A0There's nothing wrong with t=
he
> other solutions presented; I'm just adding to the variety... =A0I use the=
se
> one-liners if I need a quick search while playing in DOS (doesn't sort, j=
ust
> prints as it gets 'em):
>
> perl -MFile::Find -wle "find( sub{ /\.mp3$/i and print;}, q(.));"
>

just curious, what's the difference (in speed or results) between this
and 'dis/a/s/b *.mp3'?
obviously File::Find and perl are more powerful, but that doesn't seem
like the right use case for it?

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

Re: using Perl, How to search for files in my computer

am 13.06.2011 00:22:58 von Brian Fraser

--0016e6d7847fb87fae04a58b3e67
Content-Type: text/plain; charset=UTF-8

use File::Find::Rule;

my @mp3_files = File::Find::Rule->file->name(qr/ \. mp3$ /x)->in("C:/");

Tada.

The problem with this (and all previous) solutions is that, if you have
filenames with non-English (bah, ascii/latin-1) characters, you'll get a
bunch of garbage instead. I'm not aware if there's a portable solution for
this at the moment - For windows, see [0]; For Linux (or well, Ubuntu at
least), some variation of this will probably make do (untested, but should
work):

use feature 'unicode_strings';
use Encode ();
use List::MoreUtils qw( any );

... # mp3 filename getting code of your choice

if ( any { /\P{ASCII}/ } @mp3_files ) {
@mp3_files = map { Encode::decode( "UTF-8", $_ ) } @mp3_files;
}

Handling Unicode is hard word[1] :(

Brian.

[0]
http://www.i-programmer.info/programming/other-languages/197 3-unicode-issues-in-perl.html
[1]
http://stackoverflow.com/questions/6162484/why-does-modern-p erl-avoid-utf-8-by-default

--0016e6d7847fb87fae04a58b3e67--

Re: using Perl, How to search for files in my computer

am 13.06.2011 00:29:15 von byulga

shawn wilson wrote:
> On Sun, Jun 12, 2011 at 14:38, Brian F. Yulga wrote:
>
>>
>> perl -MFile::Find -wle "find( sub{ /\.mp3$/i and print;}, q(.));"
>>
>>
> just curious, what's the difference (in speed or results) between this
> and 'dis/a/s/b *.mp3'?
> obviously File::Find and perl are more powerful, but that doesn't seem
> like the right use case for it?
>
>
Hi Shawn,

Good point... For a simple printing of results, 'dir' is noticeably
faster and gives the same results. Actually, before I learned how to
use File::Find, I used to pipe the output from 'dir' to perl to filter
results with a regex:

dir /s /b | perl -wnle "/regex/ and print;"

....essentially using perl as 'grep', and it's faster than the File::Find
solution (well, I don't have benchmarks to prove it, but File::Find
seemed slower to me). Although in my rather basic uses, the performance
difference is inconsequential, and either way beats using the Windows
"Search Companion".

\Brian

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