"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