#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    6
    Rep 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!
  2. #2
  3. No Profile Picture
    freebsd
    Guest
    Devshed Newbie (0 - 499 posts)
    #!/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;
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2000
    Location
    Indiana
    Posts
    614
    Rep Power
    15
    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?
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    6
    Rep 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.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    Southern California
    Posts
    73
    Rep Power
    15
    <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).]
  10. #6
  11. No Profile Picture
    freebsd
    Guest
    Devshed Newbie (0 - 499 posts)
    >>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.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2000
    Location
    Indiana
    Posts
    614
    Rep Power
    15
    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).]
  14. #8
  15. No Profile Picture
    freebsd
    Guest
    Devshed Newbie (0 - 499 posts)
    >>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.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2000
    Location
    Indiana
    Posts
    614
    Rep Power
    15
    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.
  18. #10
  19. No Profile Picture
    freebsd
    Guest
    Devshed Newbie (0 - 499 posts)
    >>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.

IMN logo majestic logo threadwatch logo seochat tools logo