1

I'm writing plugins for sketch up. I cannot find a simple way to append a text file from the My Documents directory on Windows. The problems I am having are:

  • opening with the current username
  • differences between Windows versions.

I need the code to open a file in append mode in My Documents on Windows 7 on a 32bit machine.

sawa
  • 165,429
  • 45
  • 277
  • 381
help1234
  • 23
  • 6

2 Answers2

2

You can use the backtick ` in Ruby to run a command line against the system. It will then return the result of the call.

Here's the official description at Ruby-Doc.org

Returns the standard output of running cmd in a subshell. The built-in syntax %x{...} uses this method. Sets $? to the process status.

`echo` #=> 'ECHO is on.' if ran on Windows

In Windows, there are special variables you can use to gather information such as a username:

echo %username%

Hence, you can use these two to gather the location of the user's My Documents folder (provided they are on Windows).

# Tested on Windows XP and 7
my_documents = File.join(`echo %userprofile%`.chomp, "My Documents")

Note: There is also a method in Ruby that makes system calls which is system. However, this returns a boolean which will not work in your case.

Also note that this solution will only work on Windows.

Charles Caldwell
  • 16,649
  • 4
  • 40
  • 47
1
file = File.open(ENV['userprofile'] + "\\My Documents\\file.txt", "a")
file.puts "text to append"
file.close

EDIT:
Default location of user's documents is:

On Windows 98 and Windows Me
C:\My Documents
On Windows 2000 and Windows XP
%USERPROFILE%\My Documents
On Windows Vista and later
%USERPROFILE%\Documents

Non-English versions of Windows XP or earlier will use directory names appropriate to that language.

You can find the right path on Non-English systems in Windows Registry:

require 'win32/registry'
reg_path = 'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders'
reg = Win32::Registry::HKEY_CURRENT_USER.open( reg_path )
right_path = reg['Personal']    # => C:\Documents and Settings\addo\Dokumenty
Community
  • 1
  • 1
A.D.
  • 4,487
  • 3
  • 38
  • 50
  • Although this method does work, it does not open files from a particular directory. On windows, this is not particularly efficient. Thanks anyway though – help1234 Jan 30 '13 at 16:50
  • oh totally forgot you were asking something more specific ;) I'll try to update. – A.D. Jan 30 '13 at 16:51
  • Should actually be `ENV['userprofile'] + '/My Documents/file.txt'` – Charles Caldwell Jan 30 '13 at 17:16
  • Constants in Ruby begins with a capital letter. And constants should be constant :) Rename `Current_file` to `current_file`. – A.D. Jan 30 '13 at 17:50
  • @CharlesCaldwell you are right, he wanted 'my documents', but I would avoid this because not everyone use EN Windows.. f.e. I have `Dokumenty`. Check http://stackoverflow.com/a/3492996/1136008 – A.D. Jan 30 '13 at 18:14
  • @A.D. I was merely referring to the original question. I also do not use "My Documents". – Charles Caldwell Jan 30 '13 at 18:17