CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Reading directory and a file



Apr 24, 2000, 2:55 AM

Post #1 of 2 (2635 views)
Reading directory and a file Can't Post

I have a directory /home/account with subdirectories looks like


in each user directory they have a filename "info.txt". In that info.txt file it contains like this

Name: Jay Byrne
Location : Usa
Education : Undergrad

How can I convert that all the info.txt files of each users into one flatfile like


I think the script should read each directory and open the info.txt and extract each information then read another directory till it reaches end of file right? Anyone who have done this before? plsss


Peter Van Hoecke

Apr 27, 2000, 5:56 AM

Post #2 of 2 (2635 views)
Re: Reading directory and a file [In reply to] Can't Post

This is a crude version, if you test it and could suggest corrections/clues to improve it, let me know!!

use strict;
my($path, @lines, $name, $location, $education, $list, $path);
$path = "c:/scripts/onliner/"; #enter the source path her ex. /home/account/
$list = "c:/scripts/onliner/list.txt"; #where the information should end up
open OUT, ">$list";
print OUT "Name|Location|Education\n"; #heading of the file
while( glob("*") ) # give me all files in this directory
if(-d $_ ) # if it's a directory
chdir $_;
if( -f "info.txt") #does the file exist
open(INFO, "info.txt");
@lines = <INFO>;
close INFO;
$name = $lines[0];
chomp $name;
$name =~ s/\w+ *: *//;
$location = $lines[1];
chomp $location;
$location =~ s/\w+ *: *//;
$education = $lines[2];
chomp $education;
$education =~ s/\w+ *: *//;
print OUT $name . "|" . $location . "|" . $education . "\n";
@lines = undef; # just to be on the safe side...
chdir "..";


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives