[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Directory and file listing

adam beazley

12/28/2005 10:07:00 PM

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-....


16 Answers

Detlef Reichl

12/28/2005 10:33:00 PM

0

Am Donnerstag, den 29.12.2005, 07:07 +0900 schrieb adam beazley:
> 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

>
> 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
>



adam beazley

12/28/2005 10:56:00 PM

0



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-....


Mike Harris

12/28/2005 11:07:00 PM

0

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.



Johannes Friestad

12/28/2005 11:18:00 PM

0

> 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.
http://www.ruby-doc.org/docs/Progra...) , and use irb for
experimentation.


Good luck.

jf


Johannes Friestad

12/28/2005 11:25:00 PM

0

> > 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|
p img
end

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

jf


adam beazley

12/29/2005 3:59:00 PM

0

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
model = Sketchup.active_model
materials = model.materials

if (img)
# Adds a material to the "in-use" material pallet.
m = materials.add "img"
begin
# Returns nil if not successful, path if successful.
Should return a texture object
m.texture=images
rescue
UI.messagebox $!.message
end

else
UI.messagebox "Failure"
end

end




if( not file_loaded?("massmaterialimporter.rb") )

# This will add a separator to the menu, but only once
add_separator_to_menu("Plugins")

plugins_menu = UI.menu("Plugins")
Materials_menu = plugins_menu.add_submenu("Mass Material Importer")

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-....


adam beazley

12/29/2005 8:53:00 PM

0

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

prompts = ["Directory Location"]
values = []
results = inputbox prompts, values, "dir"
if (!results)
#failure
return
else
#success: you can extract values from the results array
end

directory = results.to_s
Dir.chdir directory
#Dir.chdir "C:\Program Files\@Last Software\SketchUp 5\Materials"
images=Dir["*.jpg"]


#at this point the script is working
#the current directory is set to what the user imput!
#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?

Here is the correct script for adding a material, then naming it and
then adding a texture to it:
-------------------------------------------------------------------------
def addMaterial
model = Sketchup.active_model
materials = model.materials
# Adds a material to the "in-use" material pallet.
m = materials.add "Material Name"
begin
# Returns nil if not successful, path if successful. Should
return a texture object
m.texture="c:\\Program Files\\@Last Software\\SketchUp
4\\Materials\\Carpet.jpg"
rescue
UI.messagebox $!.message
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-....


Dan Kohn

12/29/2005 9:05:00 PM

0

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|
material_importer( img)
end

def material_importer( img)
m = Sketchup.new
m.sample_name = img
m.filename = img + ".jpg"
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.

Dan Kohn

12/29/2005 9:15:00 PM

0

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|
material_importer( img)
end

def material_importer( img)
model = Sketchup.active_model
materials = model.materials
# Adds a material to the "in-use" material pallet.
m = materials.add img.to_s
begin
# Returns nil if not successful, path if successful. Should
# return a texture object
m.texture= img.to_s + ".jpg"
rescue
UI.messagebox $!.message
end
end

adam beazley

12/29/2005 10:23:00 PM

0

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-....