newbie question making a folder with ruby

S

Simon Staton

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
 
F

Fleck Jean-Julien

Hello,

2009/8/13 Simon Staton said:
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,

--=20
JJ Fleck
PCSI1 Lyc=E9e Kleber
 
S

Simon Staton

Fleck said:
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.
 
F

Fleck Jean-Julien

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 ?

--=20
JJ Fleck
PCSI1 Lyc=E9e Kleber
 
S

Simon Staton

Fleck said:
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
 
F

Fleck Jean-Julien

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

p File.exists?('~/Documents/fake/nonexistant/directories/new_dir')
#=3D>false
p Dir.mkdir('~/Documents/fake/nonexistant/directories/new_dir')
#=3D>0
p File.exists?('~/Documents/fake/nonexistant/directories/new_dir')
#=3D>true
p Dir.mkdir('~/Documents/fake/nonexistant/directories/new_dir')
#=3D>:[-1,-1]:[0,0]: File exists -
~/Documents/fake/nonexistant/directories/new_dir (Errno::EEXIST)
p File.exists?('~/Documents/fake/nonexistant/directories/new_dir')
#=3D>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,

--=20
JJ Fleck
PCSI1 Lyc=E9e Kleber
 
L

Lui Core

Simon said:
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`
 
S

Simon Staton

Lui said:
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 ^^
 
S

Simon Staton

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

Fleck Jean-Julien

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 =3D "/temp"

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

Cheers,

--=20
JJ Fleck
PCSI1 Lyc=E9e Kleber
 
S

Simon Staton

Fleck said:
Hello,


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 :p but if it helps im on windows :D
 
7

7stud --

Simon said:
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.


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

Simon Staton

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
 
F

Fleck Jean-Julien

ok I have got another error this is the error it is giving,

Didn't you forget to write the line

dir =3D "/temp"

?

If you get another error, try also

dir =3D "c:/temp"

and

dir =3D "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,

--=20
JJ Fleck
PCSI1 Lyc=E9e Kleber
 
S

Simon Staton

Fleck said:
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"?
 
F

Fleck Jean-Julien

where do I put the dir =3D "c:/temp"?

Just here:


dir =3D "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,

--=20
JJ Fleck
PCSI1 Lyc=E9e Kleber
 
S

Simon Staton

Fleck said:
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!
 
S

Simon Staton

also do you think you could help me out with somthing else :p 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?
 
F

Fleck Jean-Julien

2009/8/14 Simon Staton said:
also do you think you could help me out with somthing else :p 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,

--=20
JJ Fleck
PCSI1 Lyc=E9e Kleber
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top