views:

401

answers:

5

I am trying to read in from a few files using something like this

IO.foreach("TeamFields.txt") { |line| fieldNames.push(line.chomp) }

It works fine when running the from the command line, but when I package to an .exe with shoes and run it can't find the file. Is there a way to specify a path relative the the .exe or do I have to provide the full filepath (e.g. "c:\files\TeamFields.txt")? Thanks for the help.

+1  A: 

You need to set "Current Application Directory" correctly before going relative. The user can execute your app with different start up dir, or system can call your app with different dir.

If files in question are in the folder of your app, the only thing you need to do is to get that folder, and set it to be current.

majkinetor
+1  A: 

I don't program in ruby, but I do with windows, and odds are the relative path will be based on the location of the .exe file.

So, yes, you're probably better off passing a full path for the file name.

Randolpho
A: 

The constant __FILE__ will contain the full path to the currently executing file. You can then use methods of the File class to strip off the filename, append the relative path for whatever other file in your package it is you want and resolve the result.

moonshadow
+1  A: 

This is because your executable is not run with the correct current directory.

Either fix the current directory (for example in the shortcut) or modify your Ruby program to automatically set the working directory to the program directory with:

Dir.chdir(File.dirname($PROGRAM_NAME))
Vincent Robert
A: 

I have been having similar problems, storing a config file for my apps. I found that

Dir.chdir(File.dirname($PROGRAM_NAME))

Only returned '.' as a path. After further investigation using

File.expand_path($PROGRAM_NAME).gsub($PROGRAM_NAME,"")

for .shy files returned a new path each time where the contained rb file was unpacked to. eg. C:/DOCUME~1/username/LOCALS~1/Temp/shoes-ShoesReadWriteTest.3540/

C:/DOCUME~1/username/LOCALS~1/Temp/shoes-ShoesReadWriteTest.4064/

So my config file was being created and stored in a different location each time, not very useful. To get around this problem I have gone the standard route and stored my config file in the users home directory. By calling:

Dir.chdir(ENV['HOME'])

I still do not know how you refference the original .shy or .exe location though if it was to point to a data file location.

Running: ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32] Shoes Rasins

Munkymorgy