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 6649665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:48:05+00:00 2026-05-26T00:48:05+00:00

I am attempting to delete files based on a pattern from all directories contained

  • 0

I am attempting to delete files based on a pattern from all directories contained in a given path. I have the following but it acts like an infinite loop. When I cancel out of the loop, no files are deleted. Where am I going wrong?

def recursive_delete (dirPath, pattern)
    if (defined? dirPath and  defined? pattern && File.exists?(dirPath))
        stack = [dirPath]

        while !stack.empty?
            current = stack.delete_at(0)
            Dir.foreach(current) do |file|
                if File.directory?(file)
                    stack << current+file
                else
                    File.delete(dirPath + file) if (pattern).match(file)
                end
            end
        end

    end
end

# to call:
recursive_delete("c:\Test_Directory\", /^*.cs$/)
  • 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-26T00:48:05+00:00Added an answer on May 26, 2026 at 12:48 am

    You don’t need to re-implement this wheel. Recursive file glob is already part of the core library.

    Dir.glob('C:\Test_Directory\**\*.cs').each { |f| File.delete(f) }
    

    Dir#glob lists files in a directory and can accept wildcards. ** is a super-wildcard that means “match anything, including entire trees of directories”, so it will match any level deep (including “no” levels deep: .cs files in C:\Test_Directory itself will also match using the pattern I supplied).

    @kkurian points out (in the comments) that File#delete can accept a list, so this could be simplified to:

    File.delete(*Dir.glob('C:\Test_Directory\**\*.cs'))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to delete all rows in two dependent tables based on a
I'm attempting to delete all rows in one table that do not have a
I am currently attempting to follow along with section 10.38 from RoR Tutorial. All
I am attempting to store files in a MySql database based upon this article
I'm currently using a HttpResponse to download files from my Server. I already have
I have a Setup/Deployment project for my C# and attempting to delete a file
In attempting to use ed to delete lines around a certain pattern I've been
Attempting to print out a list of values from 2 different variables that are
Attempting to deploy a MOSS solution to a UAT server from dev server for
Attempting to use the data series from this example no longer passes the JSONLint

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.