Greetings. I'm new to perl, and this should be a simple task, but I am dropping the ball somewhere. I'm working on a script to automate the archiving of some .pdf files nightly. It will be necessary for perl to rename the files that have been copied over; that is where I have hit the wall. My script as it is now will *not* rename the files using scalars. It seems to work fine if the filename is explicity defined in the rename function. Some of the formatting got a little wacky towards the end in the cut/paste, but it should be fairly discernable. The code as it is now reads as:
#!/usr/bin/perl
#Mount appropiate shares
`mount_smbfs //########:####\@192.168.213.50/JobData /Volumes/athripd005`;
#Create list of folders to archive
$folders[0]="/usr/local/tomove";
#$folders[0]="/Volumes/athripd005/Active Lifestyles/Refined PDF/";
#$folders[1]="/Volumes/athripd005/Business Week/Refined PDF/";
#$folders[2]="/Volumes/athripd005/Classic Living/Refined PDF/";
#$folders[3]="/Volumes/athripd005/Early Run/Refined PDF/";
#$folders[4]="/Volumes/athripd005/Gameday/Refined PDF/";
#$folders[5]="/Volumes/athripd005/Classic Living/Refined PDF/";
#$folders[6]="/Volumes/athripd005/Madison-Oconee/Classifieds/Refined PDF/";
#$folders[7]="/Volumes/athripd005/Madison-Oconee/Madison/Refined PDF/";
#$folders[8]="/Volumes/athripd005/Madison-Oconee/Oconee/Refined PDF/";
#Descend into folders and copy appropriate PDFs to desktop folder.
for ($i = 0; $i < @folders; $i++){
chdir $folders[$i];
$pwd=`pwd`;
print("\n$pwd");
@filenames=`ls -1`;
foreach $_ (@filenames){
print("\nFILE: $_");
if (($_ =~m/pdf/ == 1) && ($_ =~ m/archived/ != 1)){
print("\nTHIS IS A NEW PDF");
$archived=$_;
$archived =~ s/pdf/pdfarchived/;
$directory=`pwd`;
`cp $_ /usr/local/moved`;
print "\nrename $_ $archived\n";
rename($_ , $archived) || die "Cannot rename $_ in $directory: $!";
}
}
}
Note that for now I have commented out the specifics of the script and created very basic directories (/usr/local/tomove and /usr/local/moved) in an attempt to get the script to work. This was also to rule out the possibility that it was an issue with trying to use the rename function across filesystems - which the script will eventually need to do. So all this script is trying to do at the moment is match on pdf and copy the file from /usr/local/tomove to /usr/local/moved and rename it...i.e. match blackpdf, cp it to /usr/local/moved, and rename it from blackpdf to blackpdfarchived.
The script fails when using scalars in the rename function with "No such file or directory.", and it does the same thing if I try to use the "mv" command. Obviously, the file is indeed still in present in the directory. If I try to use the File::Copy module, it fails with an error message regarding a newline in the file. However, if I explicity list the filename in all of the above mentioned functions, the script works. The use of the variable seems to be the issue.
I apologize for such a lengthy first post. Anyone have any suggestions here? Hopefully it is a careless mistake on my part. Thanks in advance.
#!/usr/bin/perl
#Mount appropiate shares
`mount_smbfs //########:####\@192.168.213.50/JobData /Volumes/athripd005`;
#Create list of folders to archive
$folders[0]="/usr/local/tomove";
#$folders[0]="/Volumes/athripd005/Active Lifestyles/Refined PDF/";
#$folders[1]="/Volumes/athripd005/Business Week/Refined PDF/";
#$folders[2]="/Volumes/athripd005/Classic Living/Refined PDF/";
#$folders[3]="/Volumes/athripd005/Early Run/Refined PDF/";
#$folders[4]="/Volumes/athripd005/Gameday/Refined PDF/";
#$folders[5]="/Volumes/athripd005/Classic Living/Refined PDF/";
#$folders[6]="/Volumes/athripd005/Madison-Oconee/Classifieds/Refined PDF/";
#$folders[7]="/Volumes/athripd005/Madison-Oconee/Madison/Refined PDF/";
#$folders[8]="/Volumes/athripd005/Madison-Oconee/Oconee/Refined PDF/";
#Descend into folders and copy appropriate PDFs to desktop folder.
for ($i = 0; $i < @folders; $i++){
chdir $folders[$i];
$pwd=`pwd`;
print("\n$pwd");
@filenames=`ls -1`;
foreach $_ (@filenames){
print("\nFILE: $_");
if (($_ =~m/pdf/ == 1) && ($_ =~ m/archived/ != 1)){
print("\nTHIS IS A NEW PDF");
$archived=$_;
$archived =~ s/pdf/pdfarchived/;
$directory=`pwd`;
`cp $_ /usr/local/moved`;
print "\nrename $_ $archived\n";
rename($_ , $archived) || die "Cannot rename $_ in $directory: $!";
}
}
}
Note that for now I have commented out the specifics of the script and created very basic directories (/usr/local/tomove and /usr/local/moved) in an attempt to get the script to work. This was also to rule out the possibility that it was an issue with trying to use the rename function across filesystems - which the script will eventually need to do. So all this script is trying to do at the moment is match on pdf and copy the file from /usr/local/tomove to /usr/local/moved and rename it...i.e. match blackpdf, cp it to /usr/local/moved, and rename it from blackpdf to blackpdfarchived.
The script fails when using scalars in the rename function with "No such file or directory.", and it does the same thing if I try to use the "mv" command. Obviously, the file is indeed still in present in the directory. If I try to use the File::Copy module, it fails with an error message regarding a newline in the file. However, if I explicity list the filename in all of the above mentioned functions, the script works. The use of the variable seems to be the issue.
I apologize for such a lengthy first post. Anyone have any suggestions here? Hopefully it is a careless mistake on my part. Thanks in advance.