Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    the Netherlands
    Posts
    215
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sub calling itself, problem with opendir()

    Hi all,

    Hopefully someone can help me with this. I have a script to read some nested directories. I have a sub to open and read the directories, but it stops after one dir I think it's because DIR isn't private.
    Here is my script:
    PHP Code:
    #!c:\perl\bin\perl

    use strict;

    print 
    "Reading the files\n";
    checkdir(".",0);

    sub checkdir() {
        
    my $dir shift;
        
    my $orgindent my $indent shift;
        
    my $indentString;


        
    opendir(DIR"$dir") || die("Unalbe to open " $dir);
        
        while(
    $indent--) {
            
    $indentString .= "-";
        }

        while(
    my $item readdir(DIR)) {
            
    next if($item =~ /^./);
            if(-
    d $item) {
                print 
    "$indentString $item\n";
                
    checkdir($dir "/" $item$orgindent+1);
            } else {
                print 
    "$indentString $item*\n";
            }
        }

    I'm just starting with the script so there might be more problems I first want it to search through more directories before I fix other probs.

    Greetz, Chris
    My Website
    010100010011110101110100011011110111000001101000

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    the Netherlands
    Posts
    215
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Never mind, I fixed it with an eval()

    For those who are curious to the script:
    PHP Code:
    #!c:\\perl\\bin\\perl

    use File::Copy;

    local $extToCopy "ttf";
    local $copyTo "C:/Windows/Fonts";

    print 
    "Reading the files and copying\n";
    checkdir(".",0);

    sub checkdir() {
        
    my $dir shift;
        
    my $orgindent my $indent shift;
        
    my $indentString;

        eval(
    'opendir(DIR' $orgindent ', "$dir") || die("Unalbe to open " . $dir);');
        
        while(
    $indent--) {
            
    $indentString .= "-";
        }

        while(
    my $item = eval('readdir(DIR' $orgindent ')')) {
            
    next if($item =~ /^./);
            if(-
    d $item) {
                print 
    "$indentString$item\n";
                
    checkdir($dir "/" $item$orgindent+1);
            } 
    elsif($item =~ /.ttf$/i) {
                print 
    "$indentString $item copying\n";
                
    copy($dir "/" $item$copyTo);
            } else {
                print 
    "$indentString $item\n";
            }
        }

    Greetz, Chris
    My Website
    010100010011110101110100011011110111000001101000


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •