We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Recursion breaks While Loop

Jzaltheral
Jzaltheral asked
on
Medium Priority
470 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
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
try
     my @files = grep !/^\.\.?/,readdir MOVEFILE;
     
#2: Read file name, if . or .. ignore file
     for $strFileName ( @files ){
         print "strFileName to move is $strFileName\n";
         ...

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
or try
local *MOVEFILE;
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
although that could limit the depth of recurson to the number of open files your system allows.

Author

Commented:
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!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.