Execution and dos path woes

  • Thread starter Karl von Laudermann
  • Start date
K

Karl von Laudermann

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?
 
A

Ara.T.Howard

`"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 ;-)

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
--
===============================================================================
| 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
===============================================================================
 
M

Matt Mower

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

Try

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

instead.

Regards,

Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top