Newbie question making a folder with ruby

ok well the program I am in the middle of programming I need it to
generate a folder in the c: when it loads up.

does anyone know how I can do this? and possibly make it with an if
variable so if there is already a folder in the c: called action3d dont
generate one but if there isnt then make one

Hello,

2009/8/13 Simon S. [email protected]:

ok well the program I am in the middle of programming I need it to
generate a folder in the c: when it loads up.

does anyone know how I can do this? and possibly make it with an if
variable so if there is already a folder in the c: called action3d dont
generate one but if there isnt then make one

Have a look at Dir.mkdir and File.exist?

Cheers,

Fleck Jean-Julien wrote:

Have a look at Dir.mkdir and File.exist?

Cheers,

Didnt work for me :s just gave errors. I am codeing ruby in google
sketchup if that helps.

Fleck Jean-Julien wrote:

Didnt work for me :s just gave errors. I am codeing ruby in google
sketchup if that helps.

What kind of error did you get and what is the code you used ?

p File.exists?(’~/Documents/fake/nonexistant/directories/new_dir’)
#=>false
p Dir.mkdir(’~/Documents/fake/nonexistant/directories/new_dir’)
#=>0
p File.exists?(’~/Documents/fake/nonexistant/directories/new_dir’)
#=>true
p Dir.mkdir(’~/Documents/fake/nonexistant/directories/new_dir’)
#=>:[-1,-1]:[0,0]: File exists -
~/Documents/fake/nonexistant/directories/new_dir (Errno::EEXIST)
p File.exists?(’~/Documents/fake/nonexistant/directories/new_dir’)
#=>true

#=>:[-1,-1]:[0,0]: File exists -
~/Documents/fake/nonexistant/directories/new_dir (Errno::EEXIST)
p File.exists?(’~/Documents/fake/nonexistant/directories/new_dir’)
#=>true

So, everything is working fine: you just can’t call Dir.mkdir on an
existant file. That’s why you have to use File.exist? to ensure your
directory is not yet in place:

if File.exist?(dir)
puts “#{dir} already exists !”
else
Dir.mkdir(dir)
end

Cheers,

Simon S. wrote:

ok well the program I am in the middle of programming I need it to
generate a folder in the c: when it loads up.

does anyone know how I can do this? and possibly make it with an if
variable so if there is already a folder in the c: called action3d dont
generate one but if there isnt then make one

Yet another answer (command line):

ruby -run -e mkdir newdirname

or in rb code:

ruby -run -e mkdir newdirname

Didnt work for me :s just gave errors. I am codeing ruby in google
sketchup if that helps.

What kind of error did you get and what is the code you used ?

To give more of an idea this is the code that I have on the .rb file. it
is a plugin im building for a program called google sketchup.

 status = UI.show_inspector "Components"
 toolbar = UI::Toolbar.new "Action 3D Toolbar"

 cmd = UI::Command.new("Export") {
 Sketchup.send_action(21237)
 }
 cmd.small_icon = "export.png"
 cmd.large_icon = "export.png"
 cmd.tooltip = "Save Climbing Frame As An Image"
 cmd.status_bar_text = "Testing the toolbars class"
 cmd.menu_text = "Test"
 toolbar = toolbar.add_item cmd
 toolbar.show

all that is doing is adding a new toolbar and opening somthing called a
components panel. now is it possible to add code in here that will
create a folder on there C: called temp and if it is already there dont
overwrite it.

Hello,

all that is doing is adding a new toolbar and opening somthing called a
components panel. now is it possible to add code in here that will
create a folder on there C: called temp and if it is already there dont
overwrite it.

Sure.
I’ll suppose you are on a UNIX system type (I do not know that sort of
thing on windows, perhaps, it is as simple as changing /temp by
c:/temp or c:\temp)

Just add:

dir = “/temp”

if File.exist?(dir)
puts “#{dir} already exists !”
else
Dir.mkdir(dir)
end

Cheers,

Lui Core wrote:

Simon S. wrote:

ok well the program I am in the middle of programming I need it to
generate a folder in the c: when it loads up.

does anyone know how I can do this? and possibly make it with an if
variable so if there is already a folder in the c: called action3d dont
generate one but if there isnt then make one

Yet another answer (command line):

ruby -run -e mkdir newdirname

or in rb code:

ruby -run -e mkdir newdirname

yeh im using a .rb file to do my codeing. so if I was to put code in
there wanting to make a folder on there c: called temp what would I put.
=) sorry noob to ruby here ^^

Fleck Jean-Julien wrote:

Hello,

all that is doing is adding a new toolbar and opening somthing called a
components panel. now is it possible to add code in here that will
create a folder on there C: called temp and if it is already there dont
overwrite it.

Sure.
I’ll suppose you are on a UNIX system type (I do not know that sort of
thing on windows, perhaps, it is as simple as changing /temp by
c:/temp or c:\temp)

Just add:

dir = “/temp”

if File.exist?(dir)
puts “#{dir} already exists !”
else
Dir.mkdir(dir)
end

Cheers,

ok I have got another error this is the error it is giving,
http://www.actinictesting.co.uk/error.jpg

any ideas? I am a noob when it comes down to programming etc and have
oinly ever used ruby on plugins. so im not sure what a unix stystem type
is :stuck_out_tongue: but if it helps im on windows :smiley:

The thing is I need it to make this folder automaticly using script in
the .rb file. what we are doing is giving out cds to our clients where
they can view our 3d models but to video the models they need a folder
called temp in there c: instead of telling them to add it we want it to
make it automaticly. when the sketchup.exe is run

Simon S. wrote:

Fleck Jean-Julien wrote:

Hello,

all that is doing is adding a new toolbar and opening somthing called a
components panel. now is it possible to add code in here that will
create a folder on there C: called temp and if it is already there dont
overwrite it.

Sure.
I’ll suppose you are on a UNIX system type (I do not know that sort of
thing on windows, perhaps, it is as simple as changing /temp by
c:/temp or c:\temp)

Just add:

dir = “/temp”

if File.exist?(dir)
puts “#{dir} already exists !”
else
Dir.mkdir(dir)
end

Cheers,

ok I have got another error this is the error it is giving,
http://www.actinictesting.co.uk/error.jpg

any ideas? I am a noob when it comes down to programming etc and have
oinly ever used ruby on plugins. so im not sure what a unix stystem type
is

Unix is another type of operating system. There are numerous versions
of unix that are slightly different, so a “unix system type” is an
awkward way to refer to any of the unix operating systems.

but if it helps im on windows :smiley:

Have you seen The Matrix?

Run this program:

puts dir

Compare the output to the output you are getting.

I am a noob when it comes down to programming

Ok, noob. Here is the way it works. You post your code, or better yet:
you construct a small example that duplicates your error and post that.
Then you post the exact error message, AND you put a comment in your
code indicating what line the error is on.

Fleck Jean-Julien wrote:

ok I have got another error this is the error it is giving,
http://www.actinictesting.co.uk/error.jpg

Didn’t you forget to write the line

dir = “/temp”

?

If you get another error, try also

dir = “c:/temp”

and

dir = “c:\temp”

and do not forget to post the code you are using together with the
error… It really helps understanding what you could have misdone.

Cheers,

I am using the code you gave me here

if File.exist?(dir)
puts “#{dir} already exists !”
else
Dir.mkdir(dir)
end

where do I put the dir = “c:/temp”?

where do I put the dir = “c:/temp”?

Just here:

dir = “c:/temp” ## <— here is where you define variable ‘dir’

if File.exist?(dir) ## <— and here is where you begin to use it
puts “#{dir} already exists !”
else
Dir.mkdir(dir)
end

Cheers,

ok I have got another error this is the error it is giving,
http://www.actinictesting.co.uk/error.jpg

Didn’t you forget to write the line

dir = “/temp”

?

If you get another error, try also

dir = “c:/temp”

and

dir = “c:\temp”

and do not forget to post the code you are using together with the
error… It really helps understanding what you could have misdone.

Cheers,

Fleck Jean-Julien wrote:

where do I put the dir = “c:/temp”?

Just here:

dir = “c:/temp” ## <— here is where you define variable ‘dir’

if File.exist?(dir) ## <— and here is where you begin to use it
puts “#{dir} already exists !”
else
Dir.mkdir(dir)
end

Cheers,

wow thats worked fantasticly THANKS!

also do you think you could help me out with somthing else :stuck_out_tongue: on a
program where it has file>help etc at the top is there a way to disable
this so that it does not show?

2009/8/14 Simon S. [email protected]:

also do you think you could help me out with somthing else :stuck_out_tongue: on a
program where it has file>help etc at the top is there a way to disable
this so that it does not show?

I’m afraid this is more a sketch’up issue rather than a Ruby one.

I’ve been looking at the documentation
(http://code.google.com/intl/en/apis/sketchup/docs/ourdoc/ui.html) but
couldn’t find a “hide” method for example for the menu class (so you
could write for exemple UI.menu(“File”).hide). Ask the dedicated
sketchup mailing lists, the perhaps already have a simple answer to
your request.

Cheers,

Just for the sake of thos who didn’t know this little trick:

There is a method in FileUtils caklled mkdir_p, which removes the
hassle of checking if the directories and superdirectories exist, and
automatically creates them if not.

http://www.ruby-doc.org/core/classes/FileUtils.html#M004315

Hope it is helpful
Cheers