rename function failing during a directory renaming operation

Posted by jaossai@yahoo.com on 2018-08-01 18:53

I have a small perl script that I want to use to rename directories / folders in a certain way.
If you put the directories name directly in the function it works but if you put a variable it fails.
See my code below:
Any ideas on how to fix this. Running on windows 7 and using 32 bit Activestate perl.

# test.pl
#use this script to rename folders to make it consistent naming style
# We will decide on our naming convention and style when this Perl script is working
# Aug 1, 2018
# test script to rename directory folder
#$file = "C:/folder1/" . "$file";
#$neu = "C:/folder1/" . "$neu";

#!/usr/local/bin/perl -w
#check for command line arguments
if ($ARGV[0] ne " " ){
$dir = $ARGV[0];
} else {
$dir= ".";
}

die "No such directory exists, $dir\n" unless (-d $dir);
#Change directory and list "directories only
chdir ($dir);
foreach (<*>) {

if (-d $_){
$dir = $_;
$Smedword = substr($dir,0,4); #Extract the the first of directory starting with the word "SMED"
if($Smedword =~ "SMED"){

#split the words
@foldername = split ( /_/, $dir);
@oldname = split ( /_/, $dir);

($foldername[0],$foldername[1]) = ($foldername[1], $foldername[0]);
$newfolder = @foldername;
$oldfolder = @oldname;

print "$foldername[1] $foldername[0]\n";

print "the old name is : @oldname\n";

print " new name is: @foldername\n";
$oldfolder = "c:\\perlcodes\\"."@oldname";
$newfolder = "c:\\perlcodes\\"."@foldername";
print " $oldfolder\n";
print " $newfolder\n";

rename($oldfolder, $newfolder) || die("Error in renaming"); #this one does not work??????

# direct renaming works perfect
rename (smed100 , smed200) || die ("Error in renaming with direct name of folder"); #this works ok

# direct renaming works very well
rename ("SMED 0987_1532 Howard St", "1532 Howard St SMED 0987") || die ("Failed to rename folder name") ; #this works fine

}

}

}

jaossai@yahoo.com | Tue, 2018-08-07 10:50

I was not successful using the "rename" function call but instead used the system call and it worked.

test3.pl
#use this script to rename folders to make it consistent naming style
# We will decide on our naming convention and style when this Perl script is working
# Joseph Ossai
# Aug 1, 2018
# test script to rename directory folders in our Site Mitigation Directory in U:\1 SCANNED PDF'S\Site Mitigation (SMED)
# From : SMED (XXXX_YYYY) (ST NO XXXXX) (ST NAME JJJJJ)
#
# To : (ST NO XXXXX) (ST NAME ) (SMED NO RRRR)
#
#
#-----------------------------------------------------------------------------------------------
#!/usr/bin/perl -w
#use strict;
use English;
use File::DosGlob;
use warnings;
#check for command line arguments passed for validity
if ($ARGV[0] ne " " ){
$directory = $ARGV[0];
} else {
$directory= ".";
}

die "No such directory exists, $directory\n" unless (-d $directory);
#Change directory and list "directories only
chdir ($directory);
foreach (<*>) {

if (-d $_){
$directory = $_;
$Smedword = substr($directory,0,4); #Extract the the first of directory starting with the word "SMED"
if($Smedword =~ "SMED"){

#split the words
@foldername = split ( /_/, $directory);
@oldname = split ( /_/, $directory);
print "the old name is: $directory\n";

($foldername[0],$foldername[1]) = ($foldername[1], $foldername[0]);

$newfolder = @foldername;
$JT = @foldername;

my $N1 = "\"$directory\"";
my $N2 = "\"@foldername\"";
print "oldname is : $N1\n";
print "the new name is: $N2\n";

system("ren $N1,$N2") || die "Error in renaming of the folders Failed: fix the parameters passed";

}

}

}