# Run script against every file in a directory

**URL:** <https://www.ruby-forum.com/t/run-script-against-every-file-in-a-directory/114587>\
**Category:** Ruby\
**Created:** [September 16, 2007, 5:46pm UTC](https://www.ruby-forum.com/t/run-script-against-every-file-in-a-directory/114587 "2007-09-16T17:46:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [September 16, 2007, 5:46pm UTC](https://www.ruby-forum.com/t/run-script-against-every-file-in-a-directory/114587/1 "2007-09-16T17:46:50Z")

</div>

Hi,

I’m currently tryin to implement a ruby script which will run through a  
directory of javascript files and run jsmin.rb against each of them as a  
part of my build script. im running into a number of silly issues so im  
just wondering if theres anyone out there thats done this kind of thing?

Cheers,

Chris

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [September 17, 2007, 9:14am UTC](https://www.ruby-forum.com/t/run-script-against-every-file-in-a-directory/114587/2 "2007-09-17T09:14:30Z")

</div>

Chris G. wrote:

> Hi,
> 
> I’m currently tryin to implement a ruby script which will run through a  
> directory of javascript files and run jsmin.rb against each of them as a  
> part of my build script. im running into a number of silly issues so im  
> just wondering if theres anyone out there thats done this kind of thing?
> 
> Cheers,
> 
> Chris

you would add the code from jsmin.rb (that performs god knows what  
purpose) and have that code run against the files…for crawling a  
filesystem you would go about it like this:

#!/usr/bin/ruby

require ‘find’

res = []

$stdin.each\_line do |file\_path|  
res \<\< File.size(file\_path.strip) #File.size is only an example…  
end #I dont know what jsmin.rb contains  
#so i cant say what to put there.

#########

usage of this would be as follows

find /etc/ -type f | ./program.rb # where /etc/ would be the directory  
that the  
# java files are in.

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [September 17, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/run-script-against-every-file-in-a-directory/114587/3 "2007-09-17T13:24:48Z")

</div>

Chris G. wrote:

> 

What kind of silly errors? What is not working?

Cheers,  
Mohit.  
9/16/2007 | 11:51 PM.

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [September 17, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/run-script-against-every-file-in-a-directory/114587/4 "2007-09-17T13:25:20Z")

</div>

Mohit S. wrote:

> > Chris

Well in a Unix Shell this would probably do it, as would a for loop with  
a bit  
of change:

find /directory -name “\*.js” -type f -exec jsmin.rb {} ;

In Ruby, I would suggest using ether the Find class or the Filescan gem.  
You  
can use it to recurse through the directory and then have the ruby  
script  
execute jsmin.rb with the file name as an argument.

TerryP.

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [September 17, 2007, 7:41pm UTC](https://www.ruby-forum.com/t/run-script-against-every-file-in-a-directory/114587/5 "2007-09-17T19:41:06Z")

</div>

2007/9/16, Chris G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hi,
> 
> I’m currently tryin to implement a ruby script which will run through a  
> directory of javascript files and run jsmin.rb against each of them as a  
> part of my build script. im running into a number of silly issues so im  
> just wondering if theres anyone out there thats done this kind of thing?

How about this:

Dir[“\*\*/\*.js”].each { |js| system(“ruby jsmin.rb #{js}”) }
