# Directory and file listing

**URL:** <https://rubytalk.org/t/directory-and-file-listing/23659>\
**Category:** ruby-talk\
**Created:** [28 December 2005 22:07 UTC](https://rubytalk.org/t/directory-and-file-listing/23659 "2005-12-28T22:07:17Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![adam\_beazley](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@adam\_beazley](https://rubytalk.org/u/adam_beazley)\
**Post date:** [28 December 2005 22:07 UTC](https://rubytalk.org/t/directory-and-file-listing/23659/1 "2005-12-28T22:07:17Z")

</div>

Hello,

I am trying to write my first ruby script to be used with Sketchup.  
Basically it is the first part of the script that I am having a problem  
with. So here is what i am trying to do:  
1: person imputs the directory path---ie c://program.../yada/  
2: ruby reads all image files in that directory and stores the file  
names in a string to be used later on.

So I basically what the script to go into a directory and list all of  
the file names. Then I can use those file names to feed into the rest of  
my script.

any help would be appreciated,  
thanks,  
Adam

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Detlef\_Reichl](https://avatars.discourse-cdn.com/v4/letter/d/439d5e/32.png) [@Detlef\_Reichl](https://rubytalk.org/u/Detlef_Reichl)\
**Post date:** [28 December 2005 22:33 UTC](https://rubytalk.org/t/directory-and-file-listing/23659/2 "2005-12-28T22:33:03Z")

</div>

> Hello,
> 
> I am trying to write my first ruby script to be used with Sketchup.  
> Basically it is the first part of the script that I am having a problem  
> with. So here is what i am trying to do:  
> 1: person imputs the directory path---ie c://program.../yada/  
> 2: ruby reads all image files in that directory and stores the file  
> names in a string to be used later on.

Hi,

img = Dir['\*.jpg']

returns all jpg-images in the current working directory as an array. So  
you can iterate over it like:

img.each do |img|  
p img  
end

Cheers  
detlef

> **···**
>
> Am Donnerstag, den 29.12.2005, 07:07 +0900 schrieb adam beazley:
> 
> > So I basically what the script to go into a directory and list all of  
> > the file names. Then I can use those file names to feed into the rest of  
> > my script.
> > 
> > any help would be appreciated,  
> > thanks,  
> > Adam

---

<div class="post-metadata">

**Author:** ![adam\_beazley](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@adam\_beazley](https://rubytalk.org/u/adam_beazley)\
**Post date:** [28 December 2005 22:56 UTC](https://rubytalk.org/t/directory-and-file-listing/23659/3 "2005-12-28T22:56:18Z")

</div>

thanks for your reply, I believe i understand, however I dont know how  
to make C:\Program Files\@Last Software\SketchUp 5\Materials the current  
directory in the program. Or how to make an option where the user can  
type in, or browse to their own directory.

But on the string answer of your question:  
img.each do |img|  
p img  
end

Does that mean that p will be a variable that will hold all the image  
names?

thanks

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Mike\_Harris](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@Mike\_Harris](https://rubytalk.org/u/Mike_Harris)\
**Post date:** [28 December 2005 23:07 UTC](https://rubytalk.org/t/directory-and-file-listing/23659/4 "2005-12-28T23:07:13Z")

</div>

adam beazley wrote:

> thanks for your reply, I believe i understand, however I dont know how to make C:\Program Files\@Last Software\SketchUp 5\Materials the current directory in the program. Or how to make an option where the user can type in, or browse to their own directory.
> 
> But on the string answer of your question:  
> img.each do |img|  
> p img  
> end
> 
> Does that mean that p will be a variable that will hold all the image names?
> 
> thanks

img = Dir['\*.jpg'] sets img to an array of the image names. the p in the next piece of code just means "do something." That code iterates through the array of image names. instead of p img, you could say print img, etc.

---

<div class="post-metadata">

**Author:** ![Johannes\_Friestad](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@Johannes\_Friestad](https://rubytalk.org/u/Johannes_Friestad)\
**Post date:** [28 December 2005 23:18 UTC](https://rubytalk.org/t/directory-and-file-listing/23659/5 "2005-12-28T23:18:08Z")

</div>

> thanks for your reply, I believe i understand, however I dont know how  
> to make C:\Program Files\@Last Software\SketchUp 5\Materials the current  
> directory in the program.

Dir.chdir "c:/Program Files/..." - use Dir.pwd to see where you are.

> Or how to make an option where the user can  
> type in, or browse to their own directory.

For instance like this:  
dir=gets # get user input from stdin  
Dir.chdir dir # move to specified directory  
img=Dir["\*.jpg"] #get array of all jpg files  
puts img # print the filenames, one per line

> But on the string answer of your question:  
> img.each do |img|  
> p img  
> end
> 
> Does that mean that p will be a variable that will hold all the image  
> names?

Nope. 'p' is a print command. 'img' is the variable that holds the image names.

Have a look at the documentation and some programming guides (ex.  
[Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby/%5C)) , and use irb for  
experimentation.

Good luck.

jf

---

<div class="post-metadata">

**Author:** ![Johannes\_Friestad](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@Johannes\_Friestad](https://rubytalk.org/u/Johannes_Friestad)\
**Post date:** [28 December 2005 23:25 UTC](https://rubytalk.org/t/directory-and-file-listing/23659/6 "2005-12-28T23:25:25Z")

</div>

> \> But on the string answer of your question:  
> \> img.each do |img|  
> \> p img  
> \> end  
> \>  
> \> Does that mean that p will be a variable that will hold all the image  
> \> names?

BTW: The example may be slightly confusing, in using the same variable  
name for the array of all image filenames as for the string holding a  
single filename.  
It can be rewritten like this:

images=Dir["\*.jpg"]  
images.each do |img|  
&nbsp;&nbsp;p img  
end

Here, 'images' is an array (list/collection) of all image file names,  
'img' is a string holding a single filename.

jf

---

<div class="post-metadata">

**Author:** ![adam\_beazley](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@adam\_beazley](https://rubytalk.org/u/adam_beazley)\
**Post date:** [29 December 2005 15:59 UTC](https://rubytalk.org/t/directory-and-file-listing/23659/7 "2005-12-29T15:59:14Z")

</div>

Thanks for all of the post so far I really appreciate it.  
Well here is the code which I havent got to work just yet so any advice  
would be appreciated:

> **···**
>
> #-----------------------------------------------------------------------------
> 
> require 'sketchup.rb'
> 
> #-----------------------------------------------------------------------------
> 
> dir=gets  
> Dir.chdir dir  
> #Dir.chdir "c:/Program Files/@Last Software/Sketchup 5/Materials/"  
> images=Dir["\*.jpg"]  
> images.each do |img|
> 
> end
> 
> def materialimporter  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model = Sketchup.active\_model  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;materials = model.materials
> 
> if (img)  
> # Adds a material to the "in-use" material pallet.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m = materials.add "img"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Returns nil if not successful, path if successful.  
> Should return a texture object  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.texture=images  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UI.messagebox $!.message  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> 
> else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UI.messagebox "Failure"  
> end
> 
> end
> 
> if( not file\_loaded?("massmaterialimporter.rb") )
> 
> &nbsp;&nbsp;&nbsp;&nbsp;# This will add a separator to the menu, but only once  
> &nbsp;&nbsp;&nbsp;&nbsp;add\_separator\_to\_menu("Plugins")
> 
> &nbsp;&nbsp;&nbsp;&nbsp;plugins\_menu = UI.menu("Plugins")  
> &nbsp;&nbsp;&nbsp;&nbsp;Materials\_menu = plugins\_menu.add\_submenu("Mass Material Importer")
> 
> &nbsp;&nbsp;&nbsp;&nbsp;Materials\_menu.add\_item("Import Materials") { materialimporter }
> 
> end
> 
> #-----------------------------------------------------------------------------  
> file\_loaded("massmaterialimporter.rb")
> 
> Ass you can see this script is meant to be put into a program called  
> sketchup. Im trying to make sketchup create a new texture for every jpg  
> in the given directory. Im a little confused as to how to make the  
> program go down the list of jpg's and make a new texture for each jpg  
> and naming that texture the same name as the jpg.
> 
> ie  
> ruby reads c://program...../.../.../sample.jpg  
> creates a texture and names it "sample"  
> then goes to the next jpg and does the same thing
> 
> anyway thanks everyone
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Wilson\_Bilkovich](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/wilson_bilkovich/32/8247_2.png) [@Wilson\_Bilkovich](https://rubytalk.org/u/Wilson_Bilkovich)\
**Post date:** [29 December 2005 16:59 UTC](https://rubytalk.org/t/directory-and-file-listing/23659/8 "2005-12-29T16:59:43Z")

</div>

require 'sketchup.rb'  
# I'm not getting a good picture of where this code fits, and how  
parameters are fed to it.  
# So, let's assume that Sketchup has a class method on it that will let us know.  
# e.g. Sketchup.materials\_path contains 'C:/Program Files/@Last  
Software/Blah Blah/'

# Call Materials\_menu.initialize\_menu somehow, probably not here,  
since this is the menu # action.  
Dir.chdir Sketchup.materials\_path  
Dir["\*.jpg"].each do |img\_name|  
&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;Sketchup.import\_material(img\_name)  
&nbsp;&nbsp;rescue Exception =\> e  
&nbsp;&nbsp;&nbsp;&nbsp;UI.messagebox(e.message)  
&nbsp;&nbsp;end  
end

# This needs to be defined before it is called, so put it in one of  
your classes.  
# Let's assume it's available as Sketchup.import\_material()  
# I've also made the assumption that 'texture' contains a filename,  
not the actual file data.  
# Note; you would put this actual definition in sketchup.rb, not here.  
def Sketchup.import\_material(file\_name)  
&nbsp;&nbsp;materials = Sketchup.active\_model.materials  
&nbsp;&nbsp;# Adds a material to the "in-use" material pallet.  
&nbsp;&nbsp;m = materials.add "img"  
&nbsp;&nbsp;m.texture = file\_name  
end

# This should be in a better place as well, and you could use a Materials\_menu  
# class variable instead of the file\_loaded? thing.  
def Materials\_menu.initialize\_menu  
&nbsp;&nbsp;unless file\_loaded?("massmaterialimporter.rb")  
&nbsp;&nbsp;&nbsp;&nbsp;# This will add a separator to the menu, but only once  
&nbsp;&nbsp;&nbsp;&nbsp;add\_separator\_to\_menu("Plugins")  
&nbsp;&nbsp;&nbsp;&nbsp;plugins\_menu = UI.menu("Plugins")  
&nbsp;&nbsp;&nbsp;&nbsp;Materials\_menu = plugins\_menu.add\_submenu("Mass Material Importer")  
&nbsp;&nbsp;&nbsp;&nbsp;Materials\_menu.add\_item("Import Materials") { materialimporter }  
&nbsp;&nbsp;end  
end

> **···**
>
> On 12/29/05, adam beazley \<abeazley@gbarchitects.com\> wrote:
> 
> > Thanks for all of the post so far I really appreciate it.  
> > Well here is the code which I havent got to work just yet so any advice  
> > would be appreciated:
> > 
> > #-----------------------------------------------------------------------------
> > 
> > require 'sketchup.rb'
> > 
> > #-----------------------------------------------------------------------------
> > 
> > dir=gets  
> > Dir.chdir dir  
> > #Dir.chdir "c:/Program Files/@Last Software/Sketchup 5/Materials/"  
> > images=Dir["\*.jpg"]  
> > images.each do |img|
> > 
> > end
> > 
> > def materialimporter  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model = Sketchup.active\_model  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;materials = model.materials
> > 
> > if (img)  
> > # Adds a material to the "in-use" material pallet.  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m = materials.add "img"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Returns nil if not successful, path if successful.  
> > Should return a texture object  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.texture=images  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UI.messagebox $!.message  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UI.messagebox "Failure"  
> > end
> > 
> > end
> > 
> > if( not file\_loaded?("massmaterialimporter.rb") )
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;# This will add a separator to the menu, but only once  
> > &nbsp;&nbsp;&nbsp;&nbsp;add\_separator\_to\_menu("Plugins")
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;plugins\_menu = UI.menu("Plugins")  
> > &nbsp;&nbsp;&nbsp;&nbsp;Materials\_menu = plugins\_menu.add\_submenu("Mass Material Importer")
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;Materials\_menu.add\_item("Import Materials") { materialimporter }
> > 
> > end
> > 
> > #-----------------------------------------------------------------------------  
> > file\_loaded("massmaterialimporter.rb")
> > 
> > Ass you can see this script is meant to be put into a program called  
> > sketchup. Im trying to make sketchup create a new texture for every jpg  
> > in the given directory. Im a little confused as to how to make the  
> > program go down the list of jpg's and make a new texture for each jpg  
> > and naming that texture the same name as the jpg.
> > 
> > ie  
> > ruby reads c://program...../.../.../sample.jpg  
> > creates a texture and names it "sample"  
> > then goes to the next jpg and does the same thing
> > 
> > anyway thanks everyone
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Dan\_Kohn](https://avatars.discourse-cdn.com/v4/letter/d/e99b99/32.png) [@Dan\_Kohn](https://rubytalk.org/u/Dan_Kohn)\
**Post date:** [31 December 2005 11:11 UTC](https://rubytalk.org/t/directory-and-file-listing/23659/9 "2005-12-31T11:11:14Z")

</div>

I'm not familiar with sketchup, but I can at least show you how  
iterations through an array should look:

dir=gets  
Dir.chdir dir  
images=Dir["\*.jpg"]  
images.each do |img|  
&nbsp;&nbsp;material\_importer( img)  
end

def material\_importer( img)  
&nbsp;&nbsp;m = Sketchup.new  
&nbsp;&nbsp;m.sample\_name = img  
&nbsp;&nbsp;m.filename = img + ".jpg"  
&nbsp;&nbsp;m.save  
end

In other words, pass each element of the array (img) to your method  
(material\_importer) and then do something with it there. You shouldn't  
meed to access the full array (images) from the method.

---

<div class="post-metadata">

**Author:** ![adam\_beazley](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@adam\_beazley](https://rubytalk.org/u/adam_beazley)\
**Post date:** [29 December 2005 20:53 UTC](https://rubytalk.org/t/directory-and-file-listing/23659/10 "2005-12-29T20:53:12Z")

</div>

Ok i think you lost me im still very new to ruby and programming in  
general. I have some experience with C lang but not extensive. So as of  
now this is where I am at with this program.

this first snippet is working:

> **···**
>
> #-----------------------------------------------------------------------------
> 
> require 'sketchup.rb'
> 
> #-----------------------------------------------------------------------------
> 
> def getfilenames
> 
> &nbsp;&nbsp;prompts = ["Directory Location"]  
> &nbsp;&nbsp;values = []  
> &nbsp;&nbsp;results = inputbox prompts, values, "dir"  
> &nbsp;&nbsp;if (!results)  
> &nbsp;&nbsp;#failure  
> &nbsp;&nbsp;return  
> &nbsp;&nbsp;else  
> &nbsp;&nbsp;#success: you can extract values from the results array  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;directory = results.to\_s  
> &nbsp;&nbsp;Dir.chdir directory  
> &nbsp;&nbsp;#Dir.chdir "C:\Program Files\@Last Software\SketchUp 5\Materials"  
> &nbsp;&nbsp;images=Dir["\*.jpg"]
> 
> &nbsp;&nbsp;#at this point the script is working  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#the current directory is set to what the user imput!  
> &nbsp;&nbsp;#also the "images" array has all of the .jpg files inside it.  
> #-------------------------------------------------------------------------------
> 
> --So thats all working fine and dandy, the images array has all of the  
> names of the jpg files in it and i can make them print one per line.  
> However the  
> images.each do |img| is not working I get a syntax error so im not sure  
> why thats not working.  
> Now im trying to think about how to make this more object oriented  
> because the way this program will have to work. Basically I need the  
> file names of the images to be in some sort of order.  
> The way it works in sketchup is that I have to first create a "material"  
> and name this material so that it is added to the "in-use" browser. Then  
> I add the jpg texture to the already named material with the correct  
> jpg.  
> So im trying to figure out how to do this with ruby, so that the  
> material is named the same as the texture without the .jpg suffix.
> 
> Im guessing this can be achieved with a while loop and maybe sorting the  
> "images array(alphabetically or by incrimenting the ?  
> what do you think?
> 
> &nbsp;&nbsp;&nbsp;&nbsp;Here is the correct script for adding a material, then naming it and  
> then adding a texture to it:  
> -------------------------------------------------------------------------  
> def addMaterial  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model = Sketchup.active\_model  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;materials = model.materials  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Adds a material to the "in-use" material pallet.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m = materials.add "Material Name"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Returns nil if not successful, path if successful. Should  
> return a texture object  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.texture="c:\\Program Files\\@Last Software\\SketchUp  
> 4\\Materials\\Carpet.jpg"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UI.messagebox $!.message  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> -------------------------------------------------------------------------  
> So how can I automate this script to loop until every jpg in the  
> directory has been added to the "in-use" material pallet and named the  
> same as its texture file.
> 
> Thanks for all of the help, I really appreciate it.  
> Adam
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Dan\_Kohn](https://avatars.discourse-cdn.com/v4/letter/d/e99b99/32.png) [@Dan\_Kohn](https://rubytalk.org/u/Dan_Kohn)\
**Post date:** [31 December 2005 11:04 UTC](https://rubytalk.org/t/directory-and-file-listing/23659/11 "2005-12-31T11:04:59Z")

</div>

In line 4 here, the array is sorted and then the method is called on  
each element of the array.

dir=gets  
Dir.chdir dir  
images=Dir["\*.jpg"]  
images.sort.each do |img|  
&nbsp;&nbsp;material\_importer( img)  
end

def material\_importer( img)  
&nbsp;&nbsp;model = Sketchup.active\_model  
&nbsp;&nbsp;materials = model.materials  
&nbsp;&nbsp;# Adds a material to the "in-use" material pallet.  
&nbsp;&nbsp;m = materials.add img.to\_s  
&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;# Returns nil if not successful, path if successful. Should  
&nbsp;&nbsp;&nbsp;&nbsp;# return a texture object  
&nbsp;&nbsp;&nbsp;&nbsp;m.texture= img.to\_s + ".jpg"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UI.messagebox $!.message  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

---

<div class="post-metadata">

**Author:** ![adam\_beazley](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@adam\_beazley](https://rubytalk.org/u/adam_beazley)\
**Post date:** [29 December 2005 22:22 UTC](https://rubytalk.org/t/directory-and-file-listing/23659/12 "2005-12-29T22:22:59Z")

</div>

just a little update before I leave for the day, I kept trying the:  
images.each do |img\_name| but never could get it to work, however I  
found that [images].to\_s changed the array to a string which i can now  
access each file by using image[0] or image[1], image[2] etc....

So now what I am thinking is I can do a while loop and make the name of  
the material and the texture start with images[0] and incrament by one  
until they are all exhausted. I need to search and find out how to  
increment in ruby (++i)?

thanks everyone  
again any coments or help would be apreciated

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Wilson\_Bilkovich](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/wilson_bilkovich/32/8247_2.png) [@Wilson\_Bilkovich](https://rubytalk.org/u/Wilson_Bilkovich)\
**Post date:** [29 December 2005 23:01 UTC](https://rubytalk.org/t/directory-and-file-listing/23659/13 "2005-12-29T23:01:15Z")

</div>

addMaterial needs to take a parameter, so it can be used without  
knowing what context it's in. Also.. I'm not sure why you have  
begin/rescue/end in there. How could assigning a string to  
"m.texture" ever fail? If your texture=(texture\_name) method in  
'materials' might raise an exception, you might want to consider  
handling it there, and having it return nil if no texture could be  
created.  
Anyway..

def addMaterial(file\_path)  
&nbsp;&nbsp;model = Sketchup.active\_model  
&nbsp;&nbsp;materials = model.materials  
&nbsp;&nbsp;material\_name = File.basename(file\_path) # returns Carpet.jpg from  
c:/program files/etc/Carpet.jpg  
&nbsp;&nbsp;# Adds a material to the "in-use" material pallet.  
&nbsp;&nbsp;m = materials.add material\_name # e.g. Carpet.jpg  
&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;# Returns nil if not successful, path if successful. Should return  
a texture object  
&nbsp;&nbsp;&nbsp;&nbsp;m.texture = file\_path  
&nbsp;&nbsp;rescue  
&nbsp;&nbsp;&nbsp;&nbsp;UI.messagebox $!.message  
&nbsp;&nbsp;end  
end

Now you can do:  
images.each do |image\_file|  
&nbsp;&nbsp;addMaterial(image\_file)  
end

The above is a 'block', and the "image\_file" variable is only  
available between the 'do' and 'end' keywords.  
In your code, you were trying to access it from elsewhere, which isn't correct.  
Your code comment:  
# Returns nil if not successful, path if successful. Should return a  
texture object  
..doesn't make too much sense to me. You're setting the path, so why  
does it get returned?  
Also, if "UI.messagebox" has a return value, sometimes your  
addMaterial method won't behave as you expect. I think you should  
revisit the use of begin/rescue/end in this case.

Further, you should put some thought into where addMaterial (in Ruby  
style, add\_material) should live.  
If you put it in the same class as 'materials', you can avoid the  
first few lines, and skip straight to 'materials.add'

Good luck,  
--Wilson.

> **···**
>
> On 12/29/05, adam beazley \<abeazley@gbarchitects.com\> wrote:
> 
> > Ok i think you lost me im still very new to ruby and programming in  
> > general. I have some experience with C lang but not extensive. So as of  
> > now this is where I am at with this program.
> > 
> > this first snippet is working:
> > 
> > \<snip\>
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#at this point the script is working  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#the current directory is set to what the user imput!  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#also the "images" array has all of the .jpg files inside it.  
> > #-------------------------------------------------------------------------------
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;Here is the correct script for adding a material, then naming it and  
> > then adding a texture to it:  
> > -------------------------------------------------------------------------  
> > def addMaterial  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model = Sketchup.active\_model  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;materials = model.materials  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Adds a material to the "in-use" material pallet.  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m = materials.add "Material Name"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Returns nil if not successful, path if successful. Should  
> > return a texture object  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.texture="c:\\Program Files\\@Last Software\\SketchUp  
> > 4\\Materials\\Carpet.jpg"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UI.messagebox $!.message  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > -------------------------------------------------------------------------  
> > So how can I automate this script to loop until every jpg in the  
> > directory has been added to the "in-use" material pallet and named the  
> > same as its texture file.

---

<div class="post-metadata">

**Author:** ![Craig\_Demyanovich1](https://avatars.discourse-cdn.com/v4/letter/c/8797f3/32.png) [@Craig\_Demyanovich1](https://rubytalk.org/u/Craig_Demyanovich1)\
**Post date:** [29 December 2005 22:33 UTC](https://rubytalk.org/t/directory-and-file-listing/23659/14 "2005-12-29T22:33:47Z")

</div>

I'm starting to pick up Ruby, and I learned through trial and error recently that I had to increment like this:

value += 1

The ++ operator didn't seem to be present, and it's not listed in Table 22.4, Ruby operators, of Programming Ruby, 2nd ed.

Regards,  
Craig

> **···**
>
> On Dec 29, 2005, at 5:22 PM, adam beazley wrote:
> 
> > So now what I am thinking is I can do a while loop and make the name of  
> > the material and the texture start with images[0] and incrament by one  
> > until they are all exhausted. I need to search and find out how to  
> > increment in ruby (++i)?

---

<div class="post-metadata">

**Author:** ![adam\_beazley](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@adam\_beazley](https://rubytalk.org/u/adam_beazley)\
**Post date:** [30 December 2005 15:52 UTC](https://rubytalk.org/t/directory-and-file-listing/23659/15 "2005-12-30T15:52:38Z")

</div>

thanks for your reply.

Well I am almost there I just need a little more help to get me home.  
Here is the script so far, the trouble I am having now is with the while  
loop.

> **···**
>
> --------------------------------------------
> 
> def materialimporter
> 
> &nbsp;&nbsp;prompt = ["Directory Location"]  
> &nbsp;&nbsp;value = []  
> &nbsp;&nbsp;results = inputbox prompt, value, "Directory"  
> &nbsp;&nbsp;if (!results)  
> &nbsp;&nbsp;#failure  
> &nbsp;&nbsp;return  
> &nbsp;&nbsp;else  
> &nbsp;&nbsp;#success: you can extract values from the results array  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;directory = results.to\_s  
> &nbsp;&nbsp;Dir.chdir directory  
> &nbsp;&nbsp;images=Dir["\*.\*"]  
> &nbsp;&nbsp;[images].to\_s
> 
> &nbsp;&nbsp;maxnum = images.length  
> &nbsp;&nbsp;img\_num = 0  
> &nbsp;&nbsp;model = Sketchup.active\_model  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;materials = model.materials
> 
> while images \<= maxnum
> 
> &nbsp;&nbsp;m = materials.add "images[img\_num]"  
> &nbsp;&nbsp;m.texture=images[img\_num]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;img\_num += 1  
> end  
> end
> 
> So the while loop doesnt seem to be working at all and im not sure why.  
> Please help.  
> thanks
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![adam\_beazley](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@adam\_beazley](https://rubytalk.org/u/adam_beazley)\
**Post date:** [30 December 2005 16:06 UTC](https://rubytalk.org/t/directory-and-file-listing/23659/16 "2005-12-30T16:06:16Z")

</div>

Nevermind,  
I figured it out and the script works and it does exactly what I wanted  
it to do. Thank you so much everyone, I appreciate the support on this  
one. Here is the final code:

> **···**
>
> #-----------------------------------------------------------------------------
> 
> require 'sketchup.rb'
> 
> #-----------------------------------------------------------------------------
> 
> def materialimporter
> 
> &nbsp;&nbsp;prompt = ["Directory Location"]  
> &nbsp;&nbsp;value = []  
> &nbsp;&nbsp;results = inputbox prompt, value, "Directory"  
> &nbsp;&nbsp;if (!results)  
> &nbsp;&nbsp;#failure  
> &nbsp;&nbsp;return  
> &nbsp;&nbsp;else  
> &nbsp;&nbsp;#success: you can extract values from the results array  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;directory = results.to\_s  
> &nbsp;&nbsp;Dir.chdir directory  
> &nbsp;&nbsp;images=Dir["\*.\*"]  
> &nbsp;&nbsp;[images].to\_s
> 
> &nbsp;&nbsp;maxnum = images.length  
> &nbsp;&nbsp;img\_num = 0  
> &nbsp;&nbsp;model = Sketchup.active\_model  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;materials = model.materials
> 
> until img\_num \> maxnum
> 
> &nbsp;&nbsp;m = materials.add images[img\_num]  
> &nbsp;&nbsp;m.texture=images[img\_num]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;img\_num += 1  
> end  
> end
> 
> if( not file\_loaded?("massmaterialimporter.rb") )
> 
> &nbsp;&nbsp;&nbsp;&nbsp;# This will add a separator to the menu, but only once  
> add\_separator\_to\_menu("Plugins")
> 
> &nbsp;&nbsp;&nbsp;&nbsp;plugins\_menu = UI.menu("Plugins")
> 
> &nbsp;&nbsp;&nbsp;&nbsp;plugins\_menu.add\_item("Import Materials") { materialimporter }
> 
> end
> 
> #-----------------------------------------------------------------------------  
> file\_loaded("massmaterialimporter.rb")
> 
> So i only have one last question:  
> I would like it if the name of the material could be brushedsteel  
> instead of brushedsteel.jpg so how do I take off the file extension?
> 
> thanks
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Dale\_Martenson](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Dale\_Martenson](https://rubytalk.org/u/Dale_Martenson)\
**Post date:** [31 December 2005 11:05 UTC](https://rubytalk.org/t/directory-and-file-listing/23659/17 "2005-12-31T11:05:50Z")

</div>

Change the 'while' to:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while img\_num \<= maxnum

then give it a try.

---

<div class="post-metadata">

**Author:** ![Wilson\_Bilkovich](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/wilson_bilkovich/32/8247_2.png) [@Wilson\_Bilkovich](https://rubytalk.org/u/Wilson_Bilkovich)\
**Post date:** [30 December 2005 16:29 UTC](https://rubytalk.org/t/directory-and-file-listing/23659/18 "2005-12-30T16:29:05Z")

</div>

filename = 'example.jpg'  
name = File.basename(filename, '\*.\*')  
# name now has the value 'example'

I'd also like to suggest that you read my last post more carefully..  
You're jumping through some unnecessary hoops with the 'images.to\_s',  
and img\_num things. 'each' does everything you need, all by itself.

> **···**
>
> On 12/30/05, adam beazley \<abeazley@gbarchitects.com\> wrote:
> 
> > Nevermind,  
> > I figured it out and the script works and it does exactly what I wanted  
> > it to do. Thank you so much everyone, I appreciate the support on this  
> > one. Here is the final code:
> > 
> > #-----------------------------------------------------------------------------
> > 
> > require 'sketchup.rb'
> > 
> > #-----------------------------------------------------------------------------
> > 
> > def materialimporter
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prompt = ["Directory Location"]  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value =   
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;results = inputbox prompt, value, "Directory"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!results)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#failure  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#success: you can extract values from the results array  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;directory = results.to\_s  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dir.chdir directory  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;images=Dir["\*.\*"]  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[images].to\_s
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maxnum = images.length  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;img\_num = 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model = Sketchup.active\_model  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;materials = model.materials
> > 
> > until img\_num \> maxnum
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m = materials.add images[img\_num]  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.texture=images[img\_num]  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;img\_num += 1  
> > end  
> > end
> > 
> > if( not file\_loaded?("massmaterialimporter.rb") )
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;# This will add a separator to the menu, but only once  
> > add\_separator\_to\_menu("Plugins")
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;plugins\_menu = UI.menu("Plugins")
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;plugins\_menu.add\_item("Import Materials") { materialimporter }
> > 
> > end
> > 
> > #-----------------------------------------------------------------------------  
> > file\_loaded("massmaterialimporter.rb")
> > 
> > So i only have one last question:  
> > I would like it if the name of the material could be brushedsteel  
> > instead of brushedsteel.jpg so how do I take off the file extension?
