Running a Ruby script through a shortcut on a different drive

I have a script which dumps a logfile into its local directory. This has
been working fine, but recently I’ve moved all the files into another
directory and used a shortcut to run it.

My shortcut is on the desktop on the “C” drive, and the files are in a
subfolder on “D”. The odd thing is that the logfile doesn’t go to either
of these places, it goes to the root of D:\

I assume that this is due to the way the windows command prompt won’t go
across drives without first going to the root of the new drive.

Is this a bug? Is there some easy way to make this more portable without
having the working directory be the root?

Thanks for your time.

In the properties for your shortcut, set the “Start in” path to wherever
you want your logs to be located. Otherwise, you need to modify your
script to write its log files to an explicit path rather than rely on
the current working directory.

-Jeremy

Jeremy B. wrote in post #1081812:

In the properties for your shortcut, set the “Start in” path to wherever
you want your logs to be located. Otherwise, you need to modify your
script to write its log files to an explicit path rather than rely on
the current working directory.

-Jeremy

Thanks Jeremy! I’d forgotten about that option.
Because my program also runs on some computers as an exe made by Ocra
(another complication), I’ve added a bit of code at the start that uses
File.dirname(FILE) if Dir.pwd evaluates as the root. It’s not
perfect but no one should be running this from a root drive anyway. I’ll
change the shortcut as well.

On 10/29/2012 09:26 AM, Joel P. wrote:

Thanks Jeremy! I’d forgotten about that option.
Because my program also runs on some computers as an exe made by Ocra
(another complication), I’ve added a bit of code at the start that uses
File.dirname(FILE) if Dir.pwd evaluates as the root. It’s not
perfect but no one should be running this from a root drive anyway. I’ll
change the shortcut as well.

Unless there is a good reason to do otherwise, you may want to
unconditionally use the File.dirname(FILE) trick. There are plenty
of other ways for that Start in option to be busted where it isn’t set
to the root directory of a drive. I suppose the way Ocra works may
complicate things there though.

Another option would be to add an argument for the script that would
specify the log directory. You could make that argument optional by
falling back to the File.dirname trick if the argument isn’t specified.

BTW, you might want to try doing File.dirname($0) instead of using
FILE here. I think that may work as you would expect even with
Ocra.

-Jeremy

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs