I have tried a few methods unsuccessfullyYou should have shown us those attempts, so we could help you understand why they failed, and how to improve them.
Anyway, a rather simple and straightforward solution to your problem, is to turn your code into a function:
And call that function on all your files (process_file("A.txt", "B.csv");).sub process_file { my ($original, $final) = @_; open( my $original_fh, "<", $original ) or die $!; open( my $final_fh, ">", $final ) or die $!; my $first = <$original_fh>; print $final_fh $first; my @replace = $first =~ /"([^"]*)"/g; while (my $line = <$original_fh>) { print "Enter the desired number of columns: "; my $nr = <STDIN>; chomp($nr); $line =~ s/((\w\w\t){$nr})/$1\n/g; $line =~ s/\t/,/g; $line =~ s/(\d{2})/$replace[$1]/g; print $final_fh $line; } close $original_fh; close $final_fh; }
You can get the list of files using glob ; this will return the list of files that match a given pattern: my @files = glob 'path/*.txt'; If you don't provide a path, glob will look into the current directory, which you can change with chdir.
And to change the extension, you can just use a substitution: my $new_file = $name =~ s/txt$/csv/r;
Edit: changed the last sentence, Akatsuki obviously already knows about substitutions.
In reply to Re: Read from multiple files change the data with a script and the write the respective output files with a different extension
by Eily
in thread Read from multiple files change the data with a script and the write the respective output files with a different extension
by Akatsuki
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |