karthick
karthick

Reputation: 21

rename the txt file extension using perl

I am doing the below steps:

Read all the text files in a directory and store it in an array named @files Run a foreach loop on each text file. Extract the file name(stripping of .txt) using split operation and creating a folder of that particular filename. Rename that file to Test.txt (so as to work as input fo another perl executable) Executing test.pl for each file by adding the line require "test.pl"; It works fine for only one file, but not any more. Here is my code:

opendir DIR, ".";
my @files = grep {/\.txt/} readdir DIR;

foreach my $files (@files) {

    @fn = split '\.', $files;
    mkdir "$fn[0]" 
        or die "Unable to create $fn[0] directory <$!>\n";

    rename "$files", "Test.txt";
    require "test3.pl";

    rename "Test.txt", "$files";
    system "move $files $fn[0]";
}

Upvotes: 2

Views: 1828

Answers (2)

pavel
pavel

Reputation: 3498

you don't require the file to be loaded once, but done every time.

So, replace

require "test3.pl";

with

do "test3.pl";

Upvotes: 2

Denny Mathew
Denny Mathew

Reputation: 1132

Can you glob for files in that directory..

Replace,

opendir DIR, ".";
my @files = grep {/\.txt/} readdir DIR;

with,

my @files = <*.txt>;

Upvotes: 1

Related Questions