Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Recursion breaks While Loop

Posted on 2006-03-31
4
Medium Priority
?
449 Views
Last Modified: 2012-06-21
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/${strFolder}","${strArchiveLoc}/Logs/Minus1/${strFolder}");
.
.
.
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,$strDestination) = @_;
      print "MoveFiles called for Directory: $strCurrentFolder\n";
#1: Open Current Folder
      opendir(MOVEFILE,$strCurrentFolder);
      
      $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}/${strFileName}");
                        MoveFiles("${strCurrentFolder}/${strFileName}/","${strDestination}/${strFileName}");
                        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}/${strFileName} ${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/Mobius/
strFileName to move is .
File is . or .. -- skipping file
Moving on with //WFF999999SFS51A/Working/Logs/Mobius/.
strFileName to move is ..
File is . or .. -- skipping file
Moving on with //WFF999999SFS51A/Working/Logs/Mobius/.
strFileName to move is Copy5oftest.txt
Moving File
Moving on with //WFF999999SFS51A/Working/Logs/Mobius/.
strFileName to move is Archive.log
Moving File
Moving on with //WFF999999SFS51A/Working/Logs/Mobius/.
strFileName to move is Copyoftest.txt
Moving File
Moving on with //WFF999999SFS51A/Working/Logs/Mobius/.
strFileName to move is test.txt
Moving File
Moving on with //WFF999999SFS51A/Working/Logs/Mobius/.
strFileName to move is Copy4oftest.txt
Moving File
Moving on with //WFF999999SFS51A/Working/Logs/Mobius/.
strFileName to move is Copy3oftest.txt
Moving File
Moving on with //WFF999999SFS51A/Working/Logs/Mobius/.
strFileName to move is VDRNET
Creating Subdirectory.
MoveFiles called for Directory: //WFF999999SFS51A/Working/Logs/Mobius/VDRNET/
strFileName to move is .
File is . or .. -- skipping file
Moving on with //WFF999999SFS51A/Working/Logs/Mobius/VDRNET/.
strFileName to move is ..
File is . or .. -- skipping file
Moving on with //WFF999999SFS51A/Working/Logs/Mobius/VDRNET/.
strFileName to move is Copy5oftest.txt
Moving File
Moving on with //WFF999999SFS51A/Working/Logs/Mobius/VDRNET/.
strFileName to move is Copyoftest.txt
Moving File
Moving on with //WFF999999SFS51A/Working/Logs/Mobius/VDRNET/.
strFileName to move is test.txt
Moving File
Moving on with //WFF999999SFS51A/Working/Logs/Mobius/VDRNET/.
strFileName to move is Copy4oftest.txt
Moving File
Moving on with //WFF999999SFS51A/Working/Logs/Mobius/VDRNET/.
strFileName to move is Copy3oftest.txt
Moving File
Moving on with //WFF999999SFS51A/Working/Logs/Mobius/VDRNET/.
strFileName to move is Copy2oftest.txt
Moving File
Moving on with //WFF999999SFS51A/Working/Logs/Mobius/VDRNET/.
strFileName to move is AuditServerOutput.csv
Moving File
Moving on with //WFF999999SFS51A/Working/Logs/Mobius/VDRNET/.
Back from recursed execution
Moving on with //WFF999999SFS51A/Working/Logs/Mobius/.
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
0
Comment
Question by:Jzaltheral
  • 3
4 Comments
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 16345145
try
     my @files = grep !/^\.\.?/,readdir MOVEFILE;
     
#2: Read file name, if . or .. ignore file
     for $strFileName ( @files ){
         print "strFileName to move is $strFileName\n";
         ...
0
 
LVL 85

Expert Comment

by:ozo
ID: 16345479
or try
local *MOVEFILE;
0
 
LVL 85

Expert Comment

by:ozo
ID: 16345488
although that could limit the depth of recurson to the number of open files your system allows.
0
 
LVL 2

Author Comment

by:Jzaltheral
ID: 16345925
Ozo,

Your first suggestion was exactly what I needed.  I changed a few lines of code and it works like a champ.

Thank you, and enjoy the points!
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question