Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl scoop

Status
Not open for further replies.

jodugg

Programmer
Joined
Jun 22, 2004
Messages
9
Location
US
I need to write a scoop in PERL that stores all directories (UNIX machine) as an array, then renames them to "pull_<dir_name>. The problem is, the working dirtectory contains both directories and files. How can I obtain a list of ONLY the directories? ls -lrt | grep ^d works, but it also returns other useless information.



Thanks in advance!
 
A scoop?

Mike

To err is human,
but to really foul things up -
you require a man Mike.

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
I haven't heard the term "scoop" before, either.
How's this?
Code:
#!perl
use strict;
use warnings;
use File::Copy;

my ($dir, $suffix) = (shift, shift);
die qq(Directory and suffix must be specified on command line!\n)
    unless defined($dir) && defined($suffix);
chomp($dir, $suffix);
[b]my $DH;
opendir($DH, "$dir") || die qq(Couldn't open directory "$dir"!\n);
my @subdirs = grep {-d && !/^\./} readdir($DH);
closedir($DH) || die qq(Couldn't close directory "$dir"!\n);[/b]
for (@subdirs) {
    my $result = move("$dir/$_", "$dir/$suffix$_");
    unless ($result) {
        warn qq(Couldn't move "$dir/$_" to "$dir/$suffix$_"!\n);
    }
}
Take a look at perldoc -f -X for a list of filetest operators. -d tests if its argument is a directory.

HTH

 
TIMTOWTDI using File::Find::Rule:
Code:
use File::Find::Rule;

# directory to start in
my $startdir = '.';

my @dirs = File::Find::Rule->directory->maxdepth( 1 )->mindepth( 1 )->in( $startdir );
 
scoop, that's irish for pints innit?
--Paul

cigless ...
 
<OT>
Lol

Technically, the Irish for "pint" is "pionta", though we often refer to them as "scoops" (I've no idea where the term came from) - in fact it's usually pronounced "shcoops" for some reason.

Speaking of which, ta me chun dul faio choinne pionta anois!

</OT>
 
is mise fasta ...
--Pól

cigless ...
 
Oops - preaching to the converted I see!
 
Ná bígí ag caint faoi piontaí maidin dé luain!
 
Ok.... [mike is *really* lost, as opposed to just confused, now]

Mike

To err is human,
but to really foul things up -
you require a man Mike.

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Tony is basically telling us not to be talking about pints of a monday morning
--Paul

cigless ...
 
Oh quite right. Can't be doing with *that* sort of thing...

Mike

To err is human,
but to really foul things up -
you require a man Mike.

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Indeed, as Father Ted would put it, on a note of censorship

"Down with this sort of thing"

--Paul

cigless ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top