D
deepak p
Hello,
I'm trying to add an extension (.sql) to all files whose names begin
with 'xa' in a specified directory. The files previously do not have
an extension. Hence, my goal is to rename file 'xaa' to
'xaa.sql'..likewise 'xab' to 'xab.sql'
I wrote a simple perl script to do this, but its not renaming the
files, and would appreciate any guidance anyone can suggest. Pasted
below is my script and the message printed to STDOUT. Thank you in
advance. At the moment, I'm developing it on SunOS running perl 5.61
but would need to be able to run it on Win32 as well, so would prefer
not having to make any OS system calls.
Deepak
----Script---
#!/usr/bin/perl -w
use strict;
my $Dir = "/home/aaa/datafiles/2003_08_28";
my $Debug = 1;
opendir(DIR, $Dir) or die "Can't open directory $Dir $! \n";
my @filenames = readdir(DIR);
@filenames = grep /^xa/, @filenames;
closedir(DIR);
my $counter = 0;
my ($filenames, $filename) = "";
if ($Debug == 1) {
foreach (@filenames) {
print "FileName $counter \t $filenames[$counter] \n";
$counter++;
}
}
my $dot = ".";
my $sql = "sql";
foreach $filename (@filenames) {
rename($filename, $filename.$sql) or warn "Couldn't rename $filename
to $filename$sql $! \n";
}
------Results printed to STDOUT-----
FileName 0 xaa
FileName 1 xab
FileName 2 xac
FileName 3 xad
FileName 4 xae
Couldn't rename xaa to xaa.sql No such file or directory
Couldn't rename xab to xab.sql No such file or directory
Couldn't rename xac to xac.sql No such file or directory
Couldn't rename xad to xad.sql No such file or directory
Couldn't rename xae to xae.sql No such file or directory
I'm trying to add an extension (.sql) to all files whose names begin
with 'xa' in a specified directory. The files previously do not have
an extension. Hence, my goal is to rename file 'xaa' to
'xaa.sql'..likewise 'xab' to 'xab.sql'
I wrote a simple perl script to do this, but its not renaming the
files, and would appreciate any guidance anyone can suggest. Pasted
below is my script and the message printed to STDOUT. Thank you in
advance. At the moment, I'm developing it on SunOS running perl 5.61
but would need to be able to run it on Win32 as well, so would prefer
not having to make any OS system calls.
Deepak
----Script---
#!/usr/bin/perl -w
use strict;
my $Dir = "/home/aaa/datafiles/2003_08_28";
my $Debug = 1;
opendir(DIR, $Dir) or die "Can't open directory $Dir $! \n";
my @filenames = readdir(DIR);
@filenames = grep /^xa/, @filenames;
closedir(DIR);
my $counter = 0;
my ($filenames, $filename) = "";
if ($Debug == 1) {
foreach (@filenames) {
print "FileName $counter \t $filenames[$counter] \n";
$counter++;
}
}
my $dot = ".";
my $sql = "sql";
foreach $filename (@filenames) {
rename($filename, $filename.$sql) or warn "Couldn't rename $filename
to $filename$sql $! \n";
}
------Results printed to STDOUT-----
FileName 0 xaa
FileName 1 xab
FileName 2 xac
FileName 3 xad
FileName 4 xae
Couldn't rename xaa to xaa.sql No such file or directory
Couldn't rename xab to xab.sql No such file or directory
Couldn't rename xac to xac.sql No such file or directory
Couldn't rename xad to xad.sql No such file or directory
Couldn't rename xae to xae.sql No such file or directory