Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading multiple files

Status
Not open for further replies.

jupiler

Technical User
Oct 1, 2002
14
BE
Hi,

I'm used to applying Perl scripts to only one file. What do I need to add to them in order to handle a complete folder in which multiple txt files are stored?

Thanks for your help!

J
 
in linux i would have done something like:

#!/usr/bin/perl

use strict;

#grep if your files have a txt extension (or just ls if #you're #going to work with all files)

my @txts = `ls | grep 'txt'`;

foreach my $file (@txts) {
'do whatever you want with the file names'
}

#hope this is helpfull, don't know if the ` works in win
 
And another way...

Code:
my $dir = 'c:/windows/temp/';
my $file_name;

opendir SOMEDIR, $dir or die "Cannot open directory. $!\n";

while ($file_name = readdir SOMEDIR) {
	chomp $file_name;
	# Skip if file is a directory
	next if (-d "$dir$file_name");
	
	# Do some stuff to the file
}

closedir SOMEDIR;
 
By browsing through the archives, I found some relevant lines:

opendir(FOLDERNAME, "[path of folder]");
@Files=grep(/\.txt/, readdir(FOLDERNAME));
close(FOLDERNAME);

foreach $file (@Files){
[specification of the command]
}

Unfortunately, it does not work so there must be some mistake in the way I want to apply a Perl script on multiple files (since the subscript in the '[specification of the command]' works perfectly ok without the first three lines.)
 
jupiler - can you give us more info about what you're trying to do to/with the files in the directory?
 
The txt files in the folder are convered pdf files from which I want to remove the 'hard returns'. For instance:

John is walking
in the park. He is walking
with Mary.

becomes:

John is walking in the park.

He is walking with Mary.

I've written a, I admit, not-so-elegant script which works perfectly ok if I apply it to one file. However, since I have currently 184 files in the folder (a number which is still increasing), I would like to add some lines to my script so that Perl makes the changes to all the files in the folder. This will save me a lot of time.

I will post the script tomorrow so that you can have a look at it.
 
This is the script. What do I need to change in order to apply it to multiple files? The eventual output files need to have the same name as the input files, but stored in a different folder (e.g. C:\Perl\Corpus\). Any help will be appreciated. Thanks!

Code:
print "Name of file (leave out extension)?\n";
$FileName=<STDIN>;
chomp $FileName;

open (INPUT, "$FileName.txt")||die;
@list=<INPUT>;
chomp @list;
close(INPUT);

foreach $line (@list){
  $line=~s/^/ /;
  if($line!~/[A-Za-z]/){
    open(OUTFILE, ">>outfile.txt")||die;
    print OUTFILE " NEW_LINE";
    close(OUTFILE);
  }
  if($line=~/[A-Za-z]/){
    open(OUTFILE, ">>outfile.txt")||die;
    print OUTFILE $line;
    close(OUTFILE);
  }
}

open(INFILE, "outfile.txt")||die;
while(<INFILE>){
  open(OUTPUT, ">>result.txt")||die;
  $_=~s/^[ ]//;
  $_=~s/[ ]+/ /;
  $_=~s/[\t]+/ /;
  $_=~s/ NEW_LINE /\n\n/g;
  $_=~s/\: /:\n\n/g;
  $_=~s/\. /.\n\n/g;
  $_=~s/\? /?\n\n/g;
  $_=~s/\; /;\n\n/g;
  $_=~s/NEW_LINE/\n\n/g;
  print OUTPUT "$_";
}
close(INFILE);
close(OUTPUT);
unlink("outfile.txt");
 
Here you go - I made an assumption that you wanted a 'result' file for each input file. Give this a shot:

Code:
my $dir = './txt_files/';		# Path to txt files
opendir(FOLDERNAME, "$dir") or die;
my @files=grep(/\.txt$/, readdir(FOLDERNAME));
close(FOLDERNAME);

foreach my $file (@files){
	open (INPUT, "$dir$file")||die;
	my @list=<INPUT>;
	chomp @list;
	close(INPUT);

	foreach my $line (@list){
		$line=~s/^/ /;
		if($line!~/[A-Za-z]/){
			open(OUTFILE, ">>outfile.txt")||die;
			print OUTFILE " NEW_LINE";
			close(OUTFILE);
		}
		if($line=~/[A-Za-z]/){
			open(OUTFILE, ">>outfile.txt")||die;
			print OUTFILE $line;
			close(OUTFILE);
		}
	}

	open(INFILE, "outfile.txt")||die;
	open(OUTPUT, "> ${dir}result.${file}")||die;
	while(<INFILE>){
		$_=~s/^[ ]//;
		$_=~s/[ ]+/ /;
		$_=~s/[\t]+/ /;
		$_=~s/ NEW_LINE /\n\n/g;
		$_=~s/\: /:\n\n/g;
		$_=~s/\. /.\n\n/g;
		$_=~s/\? /?\n\n/g;
		$_=~s/\; /;\n\n/g;
		$_=~s/NEW_LINE/\n\n/g;
		print OUTPUT "$_";
	}
	close(INFILE);
	close(OUTPUT);
	unlink("outfile.txt");
}
 
Thanks. It works nicely. This has been very helpful! Regards.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top