Closed Thread Icon

Topic awaiting preservation: What, you think files grow on Directory Trees? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12314" title="Pages that link to Topic awaiting preservation: What, you think files grow on Directory Trees? (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: What, you think files grow on Directory Trees? <span class="small">(Page 1 of 1)</span>\

 
Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 07-16-2002 17:12

ok, I've got a bunch of file strewn across a directory and several subdirectories and I want to use Perl (not CGI- offline stuff) to locate each file and run some regexp's on the contents of each file.

...But I'm getting ahead of myself...

the main issue being the 'How do I locate every file in the file tree?'


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 07-16-2002 18:04

Just did this actually

The module you want to use is

File::Find
for a recursive File Find down a directory tree.

basic usage:

code:
#!/path/to/perl -w

ues File::Find;
@DIRLIST = qw(/path/to/root/directory /any/other/paths);

sub do_somthing {
#What ever you want to do to each file here. I'll leave it up to you
}

find(\&do_something, @DIRLIST);





.:[ Never resist a perfect moment ]:.

[This message has been edited by bitdamaged (edited 07-16-2002).]

[This message has been edited by Petskull (edited 07-16-2002).]

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 07-16-2002 23:19

Ha! look what I did... now to do something useful..

use File::Find;
@DIRLIST = qw(C:\thedir C:\thedir\thesub1 C:\thedir\thesub2);
sub do_something {
print "$File::Find::name \n";
}

find(\&do_something, @DIRLIST);



Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 07-17-2002 04:34

This is what I just did. I needed to wrap up a large chunk of a website and redo all the paths to one's relative to a new server

code:
#!/usr/bin/perl -w

use File::Find;

@DIRLIST = qw(/my/path/to/files);
sub process_file {

# I'll need this later becasue in the loop the magic $_ variable will change
$old = $_;
# Just to files not directories
unless (-d $old) {
# A name for the temp file we will write to
$new = "$_~";
# Open the orginal file for reading
open(OLD, "< $old") or die "can't open $old: $!\n";
# Open the new one for writng
open(NEW, "> $new") or die "can't open $new: $!\n";
# Go through the file and run our regexp
while (<OLD> ) {
$_ =~ s#old/path/to/change?#new/pat/to/use#ig;
# Write to the new file
print NEW $_;
}
# Close the file handles
close(OLD) or die "can't close $old: $!\n";
close(NEW) or die " can't close $new: $!\n";
# Rename the new file to the old one (this will overwrite the old file);
rename($new, $old) or die "can't rename $new to $old: $!";

}
}
find(\&process_file, @DIRLIST);





.:[ Never resist a perfect moment ]:.

[This message has been edited by bitdamaged (edited 07-17-2002).]

Rahly
Bipolar (III) Inmate

From: Michigan
Insane since: Jul 2002

posted posted 07-18-2002 01:00

omg bit... comments?... you should DIE now! hehehe ;-)

Rahly

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 07-18-2002 07:12

Oh I commented it for Petskull

I'm probably one of the worst when it comes to commenting all my code like that. especially a one off script like this.



.:[ Never resist a perfect moment ]:.

« BackwardsOnwards »

Show Forum Drop Down Menu