SunQuest
           Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Try It Free
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old October 22nd, 2000, 07:36 PM
DrMuziK DrMuziK is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2000
Posts: 6 DrMuziK User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I am trying to write a subroutine that will be passed a directory, search through it for all files ending in HTML, and then search through all the subdirectories looking for the same thing. I have figured out how to find the correct files in the directory that is passed to my subroutine, but I have no idea how to get into the subdirectories.

I am new at perl, and all help is appreciated!

Reply With Quote
  #2  
Old October 22nd, 2000, 08:57 PM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
#!/usr/local/bin/perl

$target_dir = "/path/to"; #no trailing slash

print "Content-type: text/htmlnn";
@du = `du $target_dir`;
foreach $line (@du) {
($size,$dir) = split (/s+/,$line);
@ls = `ls -A $dir`;
foreach $file (@ls) {
@files = split (/s+/,$file);
foreach $result (@files) {
next if (-d "$dir/$result");
if ($result =~ /.html$/) {
print "$dir/$result<br>n";
}
}
}
}
exit;

Reply With Quote
  #3  
Old October 24th, 2000, 12:34 AM
JonLed JonLed is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2000
Location: Indiana
Posts: 614 JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 h 49 m 49 sec
Reputation Power: 9
I still contend that using the system command's 'ls' and 'du' is very cheap of you. What if he's on a Windows system?

Reply With Quote
  #4  
Old October 24th, 2000, 08:17 AM
DrMuziK DrMuziK is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2000
Posts: 6 DrMuziK User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I actually am on a windows system... right now, I'm getting the first part to work by opening a DIRHANDLE with the directory passed in, and using @files = readdir DIRHANDLE to get everhything into my array. But again, the subdirectories are stumping me.

Reply With Quote
  #5  
Old October 24th, 2000, 04:43 PM
vpopper's Avatar
vpopper vpopper is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: Southern California
Posts: 73 vpopper User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 24 sec
Reputation Power: 9
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
$BASEDIR = "path/to/basedir";

chdir $BASEDIR or die "Cannot cd to $BASEDIR: $!n";
traverse($BASEDIR,(stat('.'))[3]);

# @FILES now contains a list of file paths.


sub traverse {
my ($dir,$nlink) = @_;
my (@filenames,@subdirs);

opendir DIR, '.' or die "cannot opendir $dir: $!n";
@filenames = grep { !/^..?$/ && !(-l $_) } readdir DIR;
closedir DIR;

for (@filenames) {
next unless stat($_) && -r _;
-d _ && push(@subdirs,$_);
-f _ && -s _ && -T _ && m{..?html?$} && push(@FILES,"$dir/$_");
}

# has subdirs
if ($nlink != 2) {
for (@subdirs) {
chdir $_ or die "cannot cd to $_: $!n";
traverse("$dir/$_",(stat('.'))[3]);
chdir '..';
}
}
}

[/code]

[This message has been edited by vpopper (edited October 24, 2000).]

Reply With Quote
  #6  
Old October 24th, 2000, 07:55 PM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
>>I still contend that using the system command's 'ls' and 'du' is very cheap of you

You might think that someone doing something you don't know well is cheap, I don't blame you for being a kiddie. The 'du' and 'ls' I used is not a function of Perl since they don't work on cross-platform but it's so-called the Perl Hacks to support something Perl is incapable of doing or with a better speed. If you ever worked with thousands of dirs, you would see the significant differences.

Why don't you be a SMART *** and post your DAMN code here then?
Kid, you probably thought I don't know how to use opendir but in fact I do and I bet you weren't even born when I used that. I didn't use it any longer in my codes since in my experience I found it's relatively slow comparing to the system command hacks that I used.

>>What if he's on a Windows system?

Then too bad, DrMuzik can't take advantage of the system commands and have to stick with the standard opendir function in Perl.

Reply With Quote
  #7  
Old October 24th, 2000, 08:05 PM
JonLed JonLed is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2000
Location: Indiana
Posts: 614 JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 h 49 m 49 sec
Reputation Power: 9
Haha. Man, that's the cheapest shot I've ever seen anybody ever taken for no reason. I wasn't implying that you don't know what you're doing, I was just saying that since ls is a *nix command that it's not compatible across all platforms (which happened to be the exact ****ing case here).

I don't doubt your perl knowledge. I know you've got all commands and modules down.

And since this question's been asked about 10 times (and I've said the same thing to you every time), I'm sure you can search through and find a piece of my code... no need to put it here again.

You should honestly be ashamed of yourself for saying such stupid things. I had some respect for you until now.

And another note: I know all the ****ing *nix commands and all there uses, so don't try to give me that ****.

[edit starts here]
BTW, You'd have to be reading millions of directories before you would notice any difference in speed. I did a benchmark test and it ran through your function as well as the real way of doing it 10 million times; both took under a second (though you code was slightly faster, but that was never the question).

I was born way before perl was invented, so I doubt you've been using any perl commands longer than my life span.

Another thing... using system commands is not a hack of any kind... using back ticks is not some kind of unknown magic that your l33tz0r self figured out.

[This message has been edited by JonLed (edited October 24, 2000).]

Reply With Quote
  #8  
Old October 27th, 2000, 04:38 AM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
>>using back ticks is not some kind of unknown magic that your l33tz0r self figured
out

Then stop ****ING complaining whenever I used backticks

>>I was just saying that since ls is a *nix command that it's not compatible across all platforms (which happened to be the exact ****ing case here)

Then stop your ****ING saying someone is cheap because of using system commands. If you don't like my codes, just say you don't like it. Saying someone is cheap is DMAN RUDE in the first place.

Further, DrMuzik didn't mention whether he is running win32 or some kind of UNIX, so one would assume he is not running it on win32 based on the majority of OS Perl runs on.

Reply With Quote
  #9  
Old October 27th, 2000, 09:32 PM
JonLed JonLed is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2000
Location: Indiana
Posts: 614 JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 h 49 m 49 sec
Reputation Power: 9
I I'm not complaining because of the use of the back ticks, but rather the context which you're using them.

You're taking the use of the word 'cheap' way to seriously.

Any code you give somebody should be as OS compatible as possible.. that's all I mean by any of this.

Reply With Quote
  #10  
Old October 27th, 2000, 09:49 PM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
>>You're taking the use of the word 'cheap' way to seriously.

Yes, very seriously. Devshed is a public forum online and is accessible to everywhere in the world. "cheap" might mean nothing to your own country and culture but it's very offensive to me.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Help with searching subdirectories


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway