newbie question making a folder with ruby

Discussion in 'Ruby' started by Simon Staton, Aug 13, 2009.

  1. Simon Staton

    Simon Staton Guest

    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
    --
    Posted via http://www.ruby-forum.com/.
    Simon Staton, Aug 13, 2009
    #1
    1. Advertising

  2. Hello,

    2009/8/13 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


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

    Cheers,

    --=20
    JJ Fleck
    PCSI1 Lyc=E9e Kleber
    Fleck Jean-Julien, Aug 13, 2009
    #2
    1. Advertising

  3. Simon Staton

    Simon Staton Guest

    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.

    --
    Posted via http://www.ruby-forum.com/.
    Simon Staton, Aug 13, 2009
    #3
  4. > 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
    Fleck Jean-Julien, Aug 13, 2009
    #4
  5. Simon Staton

    Simon Staton Guest

    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
    --
    Posted via http://www.ruby-forum.com/.
    Simon Staton, Aug 13, 2009
    #5
  6. >>
    >> 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
    Fleck Jean-Julien, Aug 13, 2009
    #6
  7. Simon Staton

    Lui Core Guest

    Simon Staton 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`
    --
    Posted via http://www.ruby-forum.com/.
    Lui Core, Aug 13, 2009
    #7
  8. Simon Staton

    Simon Staton Guest

    Lui Core wrote:
    > Simon Staton 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 ^^
    --
    Posted via http://www.ruby-forum.com/.
    Simon Staton, Aug 14, 2009
    #8
  9. Simon Staton

    Simon Staton Guest

    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.
    --
    Posted via http://www.ruby-forum.com/.
    Simon Staton, Aug 14, 2009
    #9
  10. 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
    Fleck Jean-Julien, Aug 14, 2009
    #10
  11. Simon Staton

    Simon Staton Guest

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

    --
    Posted via http://www.ruby-forum.com/.
    Simon Staton, Aug 14, 2009
    #11
  12. Simon Staton

    7stud -- Guest

    Simon Staton 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 :D


    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.


    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Aug 14, 2009
    #12
  13. Simon Staton

    Simon Staton Guest

    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
    --
    Posted via http://www.ruby-forum.com/.
    Simon Staton, Aug 14, 2009
    #13
  14. > 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 =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
    Fleck Jean-Julien, Aug 14, 2009
    #14
  15. Simon Staton

    Simon Staton Guest

    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"?
    --
    Posted via http://www.ruby-forum.com/.
    Simon Staton, Aug 14, 2009
    #15
  16. > 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
    Fleck Jean-Julien, Aug 14, 2009
    #16
  17. Simon Staton

    Simon Staton Guest

    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!
    --
    Posted via http://www.ruby-forum.com/.
    Simon Staton, Aug 14, 2009
    #17
  18. Simon Staton

    Simon Staton Guest

    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?
    --
    Posted via http://www.ruby-forum.com/.
    Simon Staton, Aug 14, 2009
    #18
  19. 2009/8/14 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?


    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
    Fleck Jean-Julien, Aug 14, 2009
    #19
  20. 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
    Ruben Medellín, Aug 14, 2009
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. THY
    Replies:
    3
    Views:
    2,925
    Alvin Bruney
    Oct 16, 2003
  2. Andy Sutorius

    Error after making a folder an application

    Andy Sutorius, Feb 14, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    325
    Scott Allen
    Feb 15, 2005
  3. Replies:
    5
    Views:
    1,869
    Andrew Thompson
    Jan 12, 2005
  4. -{ Rene Brehmer }-

    Making explorer style folder view...

    -{ Rene Brehmer }-, May 3, 2004, in forum: C++
    Replies:
    4
    Views:
    496
    Christopher Benson-Manica
    May 3, 2004
  5. Lloyd Linklater

    Ruby Newbie question about app making

    Lloyd Linklater, Jun 5, 2007, in forum: Ruby
    Replies:
    1
    Views:
    85
    SonOfLilit
    Jun 5, 2007
Loading...

Share This Page