• Login
sketchucation logo sketchucation
  • Login
🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

Getting user home directory

Scheduled Pinned Locked Moved Developers' Forum
4 Posts 3 Posters 220 Views 3 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    draftomatic
    last edited by 16 Aug 2011, 17:07

    As I google for a solution it seems that people use either ENV['HOME'] or File.expand_path('~').

    Neither of these work on PC since it looks like the variable is HOMEPATH instead. So I was just hoping for a sanity check and wondering what others are doing here. Will this be reliable?

    if (WIN)
      userPath = ENV['HOMEDRIVE'] + ENV['HOMEPATH']
    elsif (MAC)
      userPath = File.expand_path('~')
    end
    
    1 Reply Last reply Reply Quote 0
    • D Offline
      Dan Rathbun
      last edited by 16 Aug 2011, 19:26

      See my code example:
      [ Code ] !autoload.rb loads "!_autoload" folders

      and FYI:

      @unknownuser said:

      ](http://www.ruby-doc.org/core-1.8.6/classes/File.html#M000794 )":1laxg9xj]

      File.expand_path(file_name [, dir_string] ) → abs_file_name

      Converts a pathname to an absolute pathname. Relative paths are referenced from the current working directory of the process unless dir_string is given, in which case it will be used as the starting point. The given pathname may start with a **` ~`**’’, which expands to the process owner‘s home directory (**%(#008000)[the environment variable HOME must be set correctly]**.) ~user’’ expands to the named user‘s home directory.

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • T Offline
        TIG Moderator
        last edited by 16 Aug 2011, 20:01

        Surely ENV["USERPROFILE"] gives it in one step ?

        TIG

        1 Reply Last reply Reply Quote 0
        • D Offline
          Dan Rathbun
          last edited by 16 Aug 2011, 20:33

          As my code does:

          ENV['HOME']=ENV['USERPROFILE'] unless ENV['HOME']
          ENV['USER']=ENV['USERNAME'] unless ENV['USER']
          # now set a global;
          $HOME = ENV['HOME']
          # .. or use the tilde shortcut within pathstrings.
          
          

          BTW.. the " ~*user*" feature does not work on Windows. Likely because of Security issues.

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          1 / 1
          • First post
            1/4
            Last post
          Buy SketchPlus
          Buy SUbD
          Buy WrapR
          Buy eBook
          Buy Modelur
          Buy Vertex Tools
          Buy SketchCuisine
          Buy FormFonts

          Advertisement