File::exists?( "filename with a blank in it" )

R

Ralph Shnelvar

Newbie here/

Consider

IMAGEMAGICK_EXE_PATH = '"f:\Program Files\ImageMagick-6.5.8-Q16\convert.exe"'

(rdb:2) File::exists?( IMAGEMAGICK_EXE_PATH )
false

What's the right way to test if a file name which has a blank in it exists?
 
M

Matthew K. Williams

Newbie here/

Consider

IMAGEMAGICK_EXE_PATH = '"f:\Program Files\ImageMagick-6.5.8-Q16\convert.exe"'

The issue isn't so much that you have a blank in it as that you've added
an extra set of quotes -- in the example above, your filename has double
quotes at the beginning and end. Drop the double quotes and it should
work fine, assuming the path is correct.

Matt
 
R

Rob Biedenharn

Newbie here/

Consider

IMAGEMAGICK_EXE_PATH = '"f:\Program Files\ImageMagick-6.5.8-
Q16\convert.exe"'

(rdb:2) File::exists?( IMAGEMAGICK_EXE_PATH )
false

What's the right way to test if a file name which has a blank in it
exists?

Well, probably to NOT put the quotes into the filename.


IMAGEMAGICK_EXE_PATH = 'f:\\Program Files\\ImageMagick-6.5.8-Q16\
\convert.exe'
or
IMAGEMAGICK_EXE_PATH = 'f:/Program Files/ImageMagick-6.5.8-Q16/
convert.exe'
(yes, Ruby's File class will work just fine with / as the directory
separator even on Windows.)

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
R

Ralph Shnelvar

Matthew said:
The issue isn't so much that you have a blank in it as that you've added
an extra set of quotes -- in the example above, your filename has double
quotes at the beginning and end. Drop the double quotes and it should
work fine, assuming the path is correct.

Matt

Isn't a Windows pathname with surrounding double quotes a valid
pathname?!?!?

What's the best Ruby-esque way to stip out surrounding double quotes if
they exist?

And ,yes, your example and Rob Biedenharn's example both work.

Thank you to both.
 
R

Rob Biedenharn

Isn't a Windows pathname with surrounding double quotes a valid
pathname?!?!?

What's the best Ruby-esque way to stip out surrounding double quotes
if
they exist?

And ,yes, your example and Rob Biedenharn's example both work.

Thank you to both.


Well, perhaps you're thinking of the quotes that are needed/used to
supply a path containing spaces to a command-line program.

In any case, you could remove quotes with:

def unquote(string)
string.sub(/\A(['"])(.*)\1\z/, '\2')
end

unquote(IMAGEMAGICK_EXE_PATH)


irb> IMAGEMAGICK_EXE_PATH = '"f:\Program Files\ImageMagick-6.5.8-
Q16\convert.exe"'
=> "\"f:\\Program Files\\ImageMagick-6.5.8-Q16\\convert.exe\""

Note that the canonical representation of the string makes the
included double-quotes obvious.

irb> def unquote(string)
irb> string.sub(/\A(['"])(.*)\1\z/, '\2')
irb> end
=> nil
irb> unquote(IMAGEMAGICK_EXE_PATH)
=> "f:\\Program Files\\ImageMagick-6.5.8-Q16\\convert.exe"

Compare this result to the value of IMAGEMAGICK_EXE_PATH itself above.

irb> unquote(unquote(IMAGEMAGICK_EXE_PATH))
=> "f:\\Program Files\\ImageMagick-6.5.8-Q16\\convert.exe"

And this is save against a path without quotes, too.

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
L

Luis Lavena

Newbie here/

Consider

IMAGEMAGICK_EXE_PATH = '"f:\Program Files\ImageMagick-6.5.8-Q16\convert..exe"'

(rdb:2) File::exists?( IMAGEMAGICK_EXE_PATH )
false

What's the right way to test if a file name which has a blank in it exists?

For double quotes: use double backlashes:

"F:\\Program Files\\ImageMagick..."

For single quotes, it can work as is.

I recommendation also is ensure all the paths are expanded with
File.expand_path, which is going to ensure forward slashes are used
instead.
 
W

W. James

Ralph said:
Isn't a Windows pathname with surrounding double quotes a valid
pathname?!?!?

The quotes are not part of the file-name.

When one enters
type "foo bar"
at the DOS-prompt, the quotes serve to indicate that there
is one file named
foo bar
and not two files named
foo
and
bar

If you enter
dir foo*
you will see
foo bar
not
"foo bar"

The quotes are not part of the file-name.

--
 

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

No members online now.

Forum statistics

Threads
473,796
Messages
2,569,645
Members
45,371
Latest member
TroyHursey

Latest Threads

Top