oblate kramrdc has asked for the wisdom of the Perl Monks concerning the following question:

Hi follow wisdom seekers!

Still learning Perl and loving it...

Any help would be greatly appreciated! Tnx!

I have a program that arranges the content of a text file into rows so that I could import it to Access.

But I want it to read the content of the whole directory instead of letting the user enter a filename.

Then stores it to one location. Or maybe in a sql or mysql database.

So, the text file contents in the directory becomes record entries.

Here's the sample data:
text1.txt
<NAME>MARK\n<AGE>27\n<ADDRESS>PHILIPPINES\n
text2.txt
<NAME>Anthony\n<AGE>26\n<ADDRESS>UNITED KINGDOM\n

Here's the code
use strict; use warnings; my $SOURCEFILE; my $TARGETFILE; my $REPORTFILE; my $converted; my $discarded; my $holder; my $titleid; print "Enter filename to convert: "; chomp($SOURCEFILE = <STDIN>); print "Save to:"; chomp($TARGETFILE =<STDIN>); print "Save Conversion Report to:"; chomp($REPORTFILE =<STDIN>); open SOURCEFILE, '<', $SOURCEFILE or die "Can't open $SOURCEFILE: $!"; open TARGETFILE, '>', $TARGETFILE or die "Can't open $TARGETFILE: $!"; open REPORTFILE, '>', $REPORTFILE or die "Can't open $REPORTFILE: $!"; $converted=0; $discarded=0; sub reporting { print TARGETFILE $holder; print REPORTFILE " -> "; print REPORTFILE $holder; $converted++; } #Evaluate per line foreach my $line (<SOURCEFILE>) { if ($line =~ /<NAME>(\w+)/) { print REPORTFILE $line; #saves original value to repor +tfile $line =~ s/<NAME>/|/; $line =~ s/\n//g; $holder = $line; reporting; } elsif ($line =~ /<AGE>(\w+)/) { print REPORTFILE $line; $line =~ s/<AGE>/|/; $line =~ s/\n//g; $holder = $line; reporting; } elsif ($line =~ /<ADDRESS>(\w+)/) { print REPORTFILE $line; $line =~ s/<ADDRESS>/|/; $line =~ s/\n//g; $holder = $line; reporting; } elsif ($line =~ /<PHONE>(\w+)/) { $discarded++; print REPORTFILE "<xx> "; print REPORTFILE $line; } } print TARGETFILE "^\n"; # eof line marker print REPORTFILE "No. of fields Converted: "; print REPORTFILE $converted, "\n"; print REPORTFILE "No. of fields Discarded: "; print REPORTFILE $discarded, "\n"; close SOURCEFILE or die "Can't close: $!"; close TARGETFILE or die "Can't close: $!"; close REPORTFILE or die "Can't close: $!";

Replies are listed 'Best First'.
Re: convert directory content to dbf
by liverpole (Monsignor) on Feb 08, 2007 at 15:15 UTC
    Hi oblate kramrdc,

    The 2 standard ways to get the contents of a directory are glob and opendir (followed by readdir and closedir).

    Here's an example of the latter:

    # Assuming you want to read files in directory $dir opendir(my $fh, $dir) or die "Can't read directory '$dir' ($!)\n"; my @files = readdir($fh); closedir($fh); foreach my $file (@files) { next if ($file eq "." or $file eq ".."); # Skip self and parent my $path = "$dir/$file"; # Construct full pathname if (-f $path) { # Do something with regular file $path ... } elsif (-d $path) { # Do something with subdirectory $path ... } }

    Note that you need to construct the full pathname, since readdir gives you only the basename of each file/subdirectory.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: convert directory content to dbf
by Moron (Curate) on Feb 08, 2007 at 15:27 UTC
    The builtin functions opendir, readdir and closedir might be useful, e.g.:
    my $dirpath = 'x/y/z'; my $dh = opendir $dirpath or die $!; map ProcessFile( "$dirpath/$_" ), readdir $dh; closedir $dh; sub ProcessFile { my $SOURCEFILE = shift; ( -f $SOURCEFILE ) or return; # ordinary files only my $TARGETFILE = $SOURCEFILE . '.out'; my $REPORTFILE = $SOURCEFILE . '.rpt'; # ... insert OP code here }

    -M

    Free your mind

      my $dh = opendir $dirpath or die $!;

      I think you have got this a bit wrong. opendir takes two arguments (directory handle and path) and returns true on success. Your code should probably have read

      opendir my $dh, $dirpath or die $!;

      Cheers,

      JohnGG

      I used this but i get this error message always
      Not enough arguments for opendir at seccon.pl line 22, near "$dirpath + or" Execution of seccon.pl aborted due to compilation errors.
        That's because the code you were given is wrong. opendir takes two arguments, not one, and returns true if successful. It may help you to look at the opendir documentation. The code should read something like

        opendir my $dirHandle, $dirpath or die qq{opendir: $dirpath: $!\n};

        I hope this correction helps you make progress.

        Cheers,

        JohnGG