42 users online (0 members and 42 guests)  


  Results 1 to 3 of 3

Related

  1. #1
    AWHF's Avatar
    New User

    Status
    Offline
    Join Date
    Jun 2007
    Posts
    2

    read files in multiple directories?

    can we write a script to read files in multiple directory in once?
    let's say there are 3 directories:
    DIR1 DIR2 DIR3


    in the directories there are files:
    DIR1:-
    1)filea.txt
    2)fileb.txt
    3)filec.txt

    DIR2:-
    1)filex.txt
    2)filey.txt

    DIR3:-
    1)filem.txt
    2)filen.txt
    3)fileo.txt



    is that posible for us to write a single script to read all the files in these 3 directories? How?

  2. #2
    xmassey's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Apr 2007
    Posts
    100

    Re: read files in multiple directories?

    This isn't difficult to do...
    Your path to your file containing all the directories...
    $path = "http://site.co.uk/files/";

    an array storing all the directory names you want to check files within
    @array = ('direc1', 'direc2', 'direc3')
    go through each one and store all the files within...
    foreach (@array) {
    opendir (FILE, "$path$_") || die "Cannot Open: $path$_";
    @file_contents = readdir (FILE);
    closedir (FILE);
    foreach (@file) {
    ## DO WHAT YOU WANT TO EACH FILE
    }
    }

    Simple.

  3. #3
    xmassey's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Apr 2007
    Posts
    100

    Re: read files in multiple directories?

    Ill post code again without comments or syntax errors hehe
    Code:
    $path = "http://site.co.uk/files/";
    @array = ('direc1', 'direc2', 'direc3');
    foreach (@array) {
    opendir (FILE, "$path$_") || die "Cannot Open: $path$_";
    @file_contents = readdir (FILE);
    closedir (FILE);
    foreach (@file_contents) {
    ## DO WHAT YOU WANT TO EACH FILE
    }
    }
    Last edited by vinyl-junkie; 10-15-2007 at 06:03 AM. Reason: Added closing code tag



Tags for this Thread