Ruby script to take single regular expression and search directory

Posted on 2013-09-16
Medium Priority
Last Modified: 2013-09-19
I am brand new to Ruby. Just started learning it yesterday and I am trying to write a ruby script. It takes a single regular expression as a command line argument and searches the current directory and all its subdirectories for:
files with names that match the given regular expression & files that have content that matches the given regular expression. At least, that is what i want it to do but it isn't yet. Can i get some help please?

here is all i have so far:
puts "Enter path"
#str = gets.chomp
#puts Dir.entries(str)

#puts File.read(str)
#eval File.read(str) #like running script to read another script
#ARGV[0] #output first command line arg 
#puts ARGV[1]
#a = (/in/ =~ 'filename')
#a = (/Regexp.new =~ '')
a = Regexp.new ARGV[0]
b = ARGV[1]
c = (a =~ b)
#concat some text before c
puts c

Open in new window

Question by:Mark_Co
1 Comment
LVL 24

Accepted Solution

fridom earned 2000 total points
ID: 39498603
I suggest starting to search for the Files in the current directory. Check
Dir.glob for trying to match the files.

Then filter out directories change into the directory and run the Dir.blog in it also.

For directory handling you may like to have a look at:

The idea is simple.
You write  method which takes one parameter (a directory)
you filter out all the elements which are directories also and
call the above method again
If there is no more directory you run the
Dir.glob method to filter for the file_names
you append them e.g to an Array
on working out of the recursion you simply append
the Elements found by Dir.glob to the Array

It's easier to program as to describe ;-)

Here is an example on how to use Glob

files = Dir.glob("*.txt")
File.open("output", "w") do |out|
    files.each do | file |
       out.print("#{file} ")

Open in new window


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
Watch the video of Kernel Migrator for SharePoint, which demonstrate the process easily of migration from SharePoint to SharePoint, OneDrive for Business & Google Drive servers, Public Folder to SharePoint, File Server to SharePoint. The tool has va…
Suggested Courses

600 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