#!perl -w # Script to process all *.txt files in current directory and put the output # in the corresponding *.txt.new files in the current directory again. my $indir = 'C:\Users\li\test'; # the input directory name my $outdir = 'C:\Users\li\test2'; # the output directory name my $i = 0; # loop index my $infile; # the input file name my $outfile; # the output file name while (defined($infile = glob($indir."\\*.txt"))) { # loop through the txt files in directory $indir printf("(%d)\tNow processing file => %s\t",++$i,$infile); $outfile = $outdir."\\".substr($infile,rindex($infile,"\\")+1); # name the output file open (IFILE, "<$infile") or die "Can't open $infile: $!"; # open input file for reading open (OFILE, ">$outfile") or die "Can't create file: $!"; # create output file for writing while () { # loop through contents of the input file my @lines = ; my @newlines; foreach(@lines) { $_ =~ s/<\/end>/<\/end>\n/g; push(@newlines,$_); print OFILE $_; } close(IFILE) or die "Can't close $infile: $!"; # close input file close(OFILE) or die "Can't close $outfile: $!"; # close output file printf("Output file => %s\n",$outfile); } }