Execution and dos path woes

Is there an easy way in Ruby to convert a long dos path name into a
short path name, so as to eliminate spaces in the path?

I've encountered what may be a bug in Ruby, or perhaps a limitation in
how it communicates with the system in dos/Windows. I'm using the
backquote string to execute programs externally. I want to be able to
execute the program without adding its directory to the system path,
so I must call it with a full path name. Now, dos requires you to put
a path in quotes if it contains spaces. So if I try to run

`C:\Documents and Settings\kvonlaudermann\My Documents\foo.exe`

I get the error:

'C:\Documents' is not recognized as an internal or external command,
operable program or batch file.

But If I run

`"C:\Documents and Settings\kvonlaudermann\My Documents\foo.exe"`

It will work fine. Now, suppose I want to pass in another file as a
parameter to the executable. If the file is in the current directory,
I can do this:

`"C:\Documents and Settings\kvonlaudermann\My Documents\foo.exe"
foo.cfg`

But I actually want to specify the full path to the config file, which
may have spaces, so I need to put it in quotes as well:

`"C:\Documents and Settings\kvonlaudermann\My Documents\foo.exe"
"C:\Documents and Settings\kvonlaudermann\My Documents\foo.cfg"`

Oddly enough, this doesn't work; I get the same error message as in
the first example:

'C:\Documents' is not recognized as an internal or external command,
operable program or batch file.

It's as though the second quoted string causes the system to ignore
the first set of quotes. Also, if I type this in manually at a dos
command prompt, it works; it only fails when being called from a Ruby
script.

So it seems my options are:

- Find a way, either via a Ruby library method or a dos command, to
convert a long path into a short path, so that I don't have to use
quotes.

- Change the working directory to the executable's directory so that I
don't have to specify a path and quote the executable name.

- Change the working directory to the file's directory so that I don't
have to specify a path and quote the config file name.

I'm running Ruby 1.8.1 on Windows XP, BTW.

Any thoughts?

`"C:\Documents and Settings\kvonlaudermann\My Documents\foo.exe"
"C:\Documents and Settings\kvonlaudermann\My Documents\foo.cfg"`

how about

"C:\Documents and Settings\kvonlaudermann\My Documents\foo.exe" "C:\Documents and Settings\kvonlaudermann\My Documents\foo.cfg"

....no single quotes?

- Change the working directory to the executable's directory so that I
don't have to specify a path and quote the executable name.

- Change the working directory to the file's directory so that I don't
have to specify a path and quote the config file name.

you have Dir::chdir?

Any thoughts?

install linux or cygwin? just kidding :wink:

if soft links work on windows you may be able cd to a tmp dir and set up links
for the exe and/or cfg - just be sure to remove them in an ensure block.

cheers.

-a

···

On Thu, 28 Oct 2004, Karl von Laudermann wrote:
--

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
When you do something, you should burn yourself completely, like a good
bonfire, leaving no trace of yourself. --Shunryu Suzuki

===============================================================================

Try

`C:/Documents and Settings/kvonlaudermann/My Documents/foo.exe`

instead.

Regards,

Matt

···

On Fri, 29 Oct 2004 02:29:00 +0900, Karl von Laudermann <doodpants@mailinator.com> wrote:

`C:\Documents and Settings\kvonlaudermann\My Documents\foo.exe`

doodpants@mailinator.com (Karl von Laudermann) wrote in message news:<729e3289.0410280927.4ccb6f20@posting.google.com>...
<snip>

- Find a way, either via a Ruby library method or a dos command, to
convert a long path into a short path, so that I don't have to use
quotes.

<snip>

Any thoughts?

require "win32/file"

short_path = File.short_path(long_path)

win32-file is available on the RAA.

Regards,

Dan

"Karl von Laudermann" <doodpants@mailinator.com> schrieb im Newsbeitrag
news:729e3289.0410280927.4ccb6f20@posting.google.com...

Is there an easy way in Ruby to convert a long dos path name into a
short path name, so as to eliminate spaces in the path?

I've encountered what may be a bug in Ruby, or perhaps a limitation in
how it communicates with the system in dos/Windows. I'm using the
backquote string to execute programs externally. I want to be able to
execute the program without adding its directory to the system path,
so I must call it with a full path name. Now, dos requires you to put
a path in quotes if it contains spaces. So if I try to run

`C:\Documents and Settings\kvonlaudermann\My Documents\foo.exe`

I get the error:

'C:\Documents' is not recognized as an internal or external command,
operable program or batch file.

But If I run

`"C:\Documents and Settings\kvonlaudermann\My Documents\foo.exe"`

It will work fine. Now, suppose I want to pass in another file as a
parameter to the executable. If the file is in the current directory,
I can do this:

`"C:\Documents and Settings\kvonlaudermann\My Documents\foo.exe"
foo.cfg`

But I actually want to specify the full path to the config file, which
may have spaces, so I need to put it in quotes as well:

`"C:\Documents and Settings\kvonlaudermann\My Documents\foo.exe"
"C:\Documents and Settings\kvonlaudermann\My Documents\foo.cfg"`

Oddly enough, this doesn't work; I get the same error message as in
the first example:

'C:\Documents' is not recognized as an internal or external command,
operable program or batch file.

It's as though the second quoted string causes the system to ignore
the first set of quotes. Also, if I type this in manually at a dos
command prompt, it works; it only fails when being called from a Ruby
script.

Then can you show us the exact invocation Ruby code? You probably used
system with a single string (the complete commmand line). Try to use
system with an array of strings, i.e. (note the usage of single quotes and
comma)

# assumed
system '"C:\Documents and Settings\kvonlaudermann\My Documents\foo.exe"
"C:\Documents and Settings\kvonlaudermann\My Documents\foo.cfg"'

# to try
system 'C:\Documents and Settings\kvonlaudermann\My Documents\foo.exe',
  'C:\Documents and Settings\kvonlaudermann\My Documents\foo.cfg'

So it seems my options are:

- Find a way, either via a Ruby library method or a dos command, to
convert a long path into a short path, so that I don't have to use
quotes.

Nah, that's ugly.

- Change the working directory to the executable's directory so that I
don't have to specify a path and quote the executable name.

- Change the working directory to the file's directory so that I don't
have to specify a path and quote the config file name.

I would not use any solution that involves changing the working dir.
Because that affects the current script. But you have another option:

- Get the quoting right.

Also, if it's a rule that the config file has to reside beside the exe
with foo.exe then it should be easy to figure the path from within the exe
using Win lib methods.

An additional note: there are File.join and File.split to deal with path
name components in a platform independend way.

Kind regards

    robert

Ara.T.Howard@noaa.gov wrote in message news:<Pine.LNX.4.60.0410281247410.6691@harp.ngdc.noaa.gov>...

···

On Thu, 28 Oct 2004, Karl von Laudermann wrote:

> `"C:\Documents and Settings\kvonlaudermann\My Documents\foo.exe"
> "C:\Documents and Settings\kvonlaudermann\My Documents\foo.cfg"`

how about

"C:\Documents and Settings\kvonlaudermann\My Documents\foo.exe" "C:\Documents and Settings\kvonlaudermann\My Documents\foo.cfg"

...no single quotes?

Those aren't single quotes; that's Ruby's backquote method. IOW, that
line is a source listing for a complete Ruby program. :wink:

djberg96@hotmail.com (Daniel Berger) wrote in message news:<6e613a32.0410281552.ae4eb11@posting.google.com>...

require "win32/file"

short_path = File.short_path(long_path)

win32-file is available on the RAA.

Cool, thanks!