Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6128081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:35:11+00:00 2026-05-23T16:35:11+00:00

I have been trying to find a solution to this for some time. I

  • 0

I have been trying to find a solution to this for some time. I have found questions and answers on recursion but nothing that seemed to fit this particular situation.

I have written a class which should go through the given folder and all subfolders and rename files and folders if a particular search pattern is found.

Everything works as expected the replaceAllInDir gets called, it replaces files and folders if needed. The next step then is to do the same for all subfolders within the given folder.
So a subfolder gets identified and replaceAllInDir gets called from within itself. Let’s assum the particular subfolder called does not contain any subfolders. I would then expect that we return to the parent folder and continue looking for other subfolders. But instead control is not returned to the parent calling method and the program ends.

I am aware of other ways of solving the actual use case, but I cannot explain the behaviour of ruby.

class MultiFileAndFolderRename
  attr_accessor :rootDir, :searchPattern, :replacePattern

  def initialize(rootDir, searchPattern, replacePattern)
    @rootDir = rootDir
    @searchPattern = searchPattern
    @replacePattern = replacePattern
  end

  def execute
    replaceAllInDir(@rootDir)
  end

  def getValidDirEntries(dir)
    dirList = Dir.entries(dir)
    dirList.delete('.')
    dirList.delete('..')
    dirList
  end

  def replaceAllInDir(currentDir)
    Dir.chdir(currentDir)
    puts "Processing directory: " + Dir.pwd
    dirList = getValidDirEntries(currentDir)
    dirList.each { |dirEntry|
      attemptRename(dirEntry)
    }

    dirList = getValidDirEntries(currentDir)
    dirList.each { |dirEntry|
      if File.directory?(dirEntry)
        newDir = currentDir + '\\' + dirEntry
        rntemp = MultiFileAndFolderRename.new(newDir, 'searchString', 'replaceString')
        rntemp.replaceAllInDir(newDir)
      end
    }
  end

  def attemptRename(dirEntry)
    if dirEntry.match(@searchPattern)
      newname = dirEntry.to_s.sub(@searchPattern, @replacePattern)
      FileUtils.mv(dirEntry.to_s, newname)
    end
  end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:35:11+00:00Added an answer on May 23, 2026 at 4:35 pm

    You have a bug. The first line of replaceAllInDir() is Dir.chdir(). chdir() changes the directory of the current process on a global scale. It’s not call-stack dependent. So later when you move into a subdirectory and change into that, the change becomes permanent even if you return from the recursion.

    You need to change back to the correct directory after any call to replaceAllInDir(). For example:

    ...
    dirList.each { |dirEntry|
      if File.directory?(dirEntry)
        ....
        rntemp.replaceAllInDir(newDir)
        Dir.chdir(currentDir) # <- Restore us back to the correct directory
      end
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to google this but have been unable to find a solution
I have been trying to find some resource on this topic for a while
I have been trying to find decent Image Acquisition + Image Processing solution at
I have been trying to find some step by step guidance on how to
I have been trying for about an hour now to find an elegant solution
I've been trying to figure this out for quite some time now. I couldn't
I've been Googling for a while now trying to find some sort of solution
I'm trying to a LINQ query, and I have been stumped for some time.
Well, I have been scratching my head for some time now. But failed to
I have been searching around for ages trying to find a solution to my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.