I am using a while loop to recurse through a set of folders and their subfolders - code is shown below. The problem is that when it comes back from a recursion, it falls out of the while loop, even if there were more files in the parent folder that need to be moved, or other folders that should have been recursed into. Any advice would be grand.
Here is the code.
.
.
.
#7: Move Working Logs to Minus 1
print "Move Working Files\n";
msgs_to_multi_logfiles ("Move Working Files", $h_message_msg, $strHostName); #Call to pipereader to write log message.
MoveFiles("${strWorkingLoc
}/Logs/${s
trFolder}"
,"${strArc
hiveLoc}/L
ogs/Minus1
/${strFold
er}");
.
.
.
sub MoveFiles
{
#This sub recursively moves all of the files and folders in a folder to a new folder
#Define variables for the parameters
my ($strCurrentFolder, #The folder to search for files
$strDestination #The folder to move the files to
);
my ($strFileName);
#Set the variables
($strCurrentFolder,$strDes
tination) = @_;
print "MoveFiles called for Directory: $strCurrentFolder\n";
#1: Open Current Folder
opendir(MOVEFILE,$strCurre
ntFolder);
$strFileName = readdir MOVEFILE;
#2: Read file name, if . or .. ignore file
do #while($strFileName = readdir MOVEFILE)
{
chomp $strFileName;
print "strFileName to move is $strFileName\n";
if (($strFileName eq ".") || ($strFileName eq ".."))
{
print "File is . or .. -- skipping file\n";
}
else
{
if (-d $strCurrentFolder . "/" . $strFileName)
#3: If file is a directory, create directory in destination, then recurse, appending
# directory name to current folder and destination folder
{
print "Creating Subdirectory.\n";
mkdir("${strDestination}/$
{strFileNa
me}");
MoveFiles("${strCurrentFol
der}/${str
FileName}/
","${strDe
stination}
/${strFile
Name}");
print "Back from recursed execution\n";
}
else
#4: If file is not a directory, move to destination folder
{
print "Moving File\n";
system("mv ${strCurrentFolder}/${strF
ileName} ${strDestination}");
}
}
print "Moving on with ${strCurrentFolder}.\n";
} while($strFileName = readdir MOVEFILE);
#5: No more files - close current folder, and exit sub
closedir(MOVEFILE);
}
And here is a sample output:
Current Log Folder = Mobius
Move Working Files
MoveFiles called for Directory: //WFF999999SFS51A/Working/
Logs/Mobiu
s/
strFileName to move is .
File is . or .. -- skipping file
Moving on with //WFF999999SFS51A/Working/
Logs/Mobiu
s/.
strFileName to move is ..
File is . or .. -- skipping file
Moving on with //WFF999999SFS51A/Working/
Logs/Mobiu
s/.
strFileName to move is Copy5oftest.txt
Moving File
Moving on with //WFF999999SFS51A/Working/
Logs/Mobiu
s/.
strFileName to move is Archive.log
Moving File
Moving on with //WFF999999SFS51A/Working/
Logs/Mobiu
s/.
strFileName to move is Copyoftest.txt
Moving File
Moving on with //WFF999999SFS51A/Working/
Logs/Mobiu
s/.
strFileName to move is test.txt
Moving File
Moving on with //WFF999999SFS51A/Working/
Logs/Mobiu
s/.
strFileName to move is Copy4oftest.txt
Moving File
Moving on with //WFF999999SFS51A/Working/
Logs/Mobiu
s/.
strFileName to move is Copy3oftest.txt
Moving File
Moving on with //WFF999999SFS51A/Working/
Logs/Mobiu
s/.
strFileName to move is VDRNET
Creating Subdirectory.
MoveFiles called for Directory: //WFF999999SFS51A/Working/
Logs/Mobiu
s/VDRNET/
strFileName to move is .
File is . or .. -- skipping file
Moving on with //WFF999999SFS51A/Working/
Logs/Mobiu
s/VDRNET/.
strFileName to move is ..
File is . or .. -- skipping file
Moving on with //WFF999999SFS51A/Working/
Logs/Mobiu
s/VDRNET/.
strFileName to move is Copy5oftest.txt
Moving File
Moving on with //WFF999999SFS51A/Working/
Logs/Mobiu
s/VDRNET/.
strFileName to move is Copyoftest.txt
Moving File
Moving on with //WFF999999SFS51A/Working/
Logs/Mobiu
s/VDRNET/.
strFileName to move is test.txt
Moving File
Moving on with //WFF999999SFS51A/Working/
Logs/Mobiu
s/VDRNET/.
strFileName to move is Copy4oftest.txt
Moving File
Moving on with //WFF999999SFS51A/Working/
Logs/Mobiu
s/VDRNET/.
strFileName to move is Copy3oftest.txt
Moving File
Moving on with //WFF999999SFS51A/Working/
Logs/Mobiu
s/VDRNET/.
strFileName to move is Copy2oftest.txt
Moving File
Moving on with //WFF999999SFS51A/Working/
Logs/Mobiu
s/VDRNET/.
strFileName to move is AuditServerOutput.csv
Moving File
Moving on with //WFF999999SFS51A/Working/
Logs/Mobiu
s/VDRNET/.
Back from recursed execution
Moving on with //WFF999999SFS51A/Working/
Logs/Mobiu
s/.
Current Log Folder = QMaster
At issue is the fact that in addition to the VDRNET folder, which it recurses into and empties as it should, there is a folder ...\Mobius\ArchiveCreation
which is never even enters.
I am settring the point values on this as high as possible, as it is for a project at work that I really need to get completed.
Thank you.
Greg H
Start Free Trial