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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts

    Pulling every .txt file from a dir

    hey there,
    ok ive been working on quite a big script,
    and got to part i cant work out....
    the script should pull all the ,txt files form a dir and display them inbeded in html so there is one then next uisng <br> also should be alphabetical but im getting now where...the rest of the code fine..now i just need to finish it with this part of code can someone help me out here?
    thanks aload
    sir p

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    the Netherlands
    Posts
    215
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm working on it

    Here's what I got:
    Code:
    #!/usr/bin/perl
    
    opendir(DIR,".");
    @files = sort(grep(/\.php$/,readdir(DIR)));
    closedir(DIR);
    
    print "Content-type: text/html\n\n";
    
    $all_data = "";
    foreach $file (@files) {
    	open(FILE,"$file") || die("Could not find $file. $!");
    	@lines = <FILE>;
    	close(FILE);
    	foreach $line (@lines) {
    		$line =~ s/\n/\<br\>/g; # optional
    		$line =~ s/\</&lt;/g; # optional
    		$all_data .= "$line";
    	}
    	$all_data .= "<br>";
    }
    
    print $all_data;
    I hope it's useful to you.

    Mzzl, Chris
    Last edited by chrisvmarle; 06-20-2002 at 02:51 PM.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    hey thanks
    gives an error tho/
    the logs say file does not exsist ...this is not true or it would be 404...any ideas?

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    the Netherlands
    Posts
    215
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oops, I tested it with my .php files 'cuz I didn't have any .txt files in my cgi-bin. And I found another little problem. Here is the new code, try this:
    Code:
    #!/usr/bin/perl
    
    opendir(DIR,".");
    @files = sort(grep(/\.txt$/,readdir(DIR)));
    closedir(DIR);
    
    print "Content-type: text/html\n\n";
    
    $all_data = "";
    foreach $file (@files) {
    	open(FILE,"$file") || die("Could not find $file. $!");
    	@lines = <FILE>;
    	close(FILE);
    	foreach $line (@lines) {
    		$line =~ s/\</&lt;/g; # optional
    		$line =~ s/\n/\<br\>/g; # optional
    		$all_data .= "$line";
    	}
    	$all_data .= "<br>";
    }
    
    print $all_data;
    Mzzl, Chris

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    ok cool,
    still does the same thing....im thinking it means the dir is missing...i cant see in there where the dir is? it looks like the dir is " . "

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    the Netherlands
    Posts
    215
    Thanks
    0
    Thanked 0 Times in 0 Posts
    opendir(DIR,".");
    "." is indeed the directory. It means "current dir", so this script opens the current dir, wich is where your script is running in.

    You can change the dot to anything you want. Like "texts" to open a dir named "texts".

    Mzzl, Chris

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    hey Chris,
    ok it works with no erros but does not display the info and I have changed it abit..still no errors but if you look at the code then test it yourself look what happens
    im stumped...you got any ideas?
    or anyone elsE?
    cheers
    heres the code for this....
    print "Content-type:text/html\n\n";

    parse_form();

    $int =$FORM{int};
    $band =$FORM{band};

    sub parse_form {

    print "<html><head><title>TabbersTemple.com - $int tabs for $band </title></head>\n";
    print "<body bgcolor=\"#000000\" text=\"#FFFFFF\">\n";
    print "<b><small><font face=\"Verdana\">$int Tabs for $band </font><br>\n";

    opendir(DIR,"../tabberstemple/tabs/$int/$band");
    @files = sort(grep(/\.txt$/,readdir(DIR)));
    closedir(DIR);

    $all_data = "";
    foreach $file (@files) {
    open(FILE,"$file") || die("Could not find $file. $!");
    @lines = <FILE>;
    close(FILE);
    foreach $line (@lines) {
    $line =~ s/\</&lt;/g; # optional
    $line =~ s/\n/\<br\>/g; # optional
    $all_data .= "$line";
    }
    $all_data .= "<br>";
    }

    print $all_data;
    }
    thanks
    sir p


  •  

    Posting Permissions

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