Strange function filereadable()

279 views
Skip to first unread message

.瀚

unread,
Sep 16, 2008, 1:21:42 AM9/16/08
to Vim user support
hi!
  I am using gvim 7.2.15 on WIndows XP pro. I find a strange thing about filereadable()

Problem:
1. create a directory in C:, for example c:\try this
2. create a text file in the directory, for example try.txt
3.open vim
     :let filename = 'c:\\try\ this\\try.txt'
     :echo filereadable(filename)
then vim output 0, very strange, but
     :echo filereadable("c:\\try\ this\\try.txt")
results in 1

real issue:
I define a global variable in my vimrc
if has('win32')
let g:vimfiles = $HOME.'\vimfiles\'
endif
let g:vimfiles = escape(g:vimfiles, ' \')
so I can use this:
silent execute 'helptags '.g:vimfiles.'doc'|   " Tag help files
execute 'setlocal tags+='.g:vimfiles.'stltags'|    " Add Stl tags
....
it's convenient, but when use
filereadable(g:vimfiles . "path_to_sth_readable")
vim return me 0

Does anyone have a good solution to this ? Thanks!

Ivan Tishchenko

unread,
Sep 16, 2008, 4:08:09 AM9/16/08
to vim...@googlegroups.com
I'd guess it's because you use single quotes in one place and double in
another. Single quoted string does not interpret backslashes, so '\\'
is TWO slashes, and that file doesn't exist.

Solution would be using forward slashes in filenames.

WBR,
Ivan.

.瀚

unread,
Sep 16, 2008, 10:10:22 AM9/16/08
to vim...@googlegroups.com
Oh , I understand .  Now I use an extra variable g:vimfiles_no_escape to solve this problem
Thanks!

Tony Mechelynck

unread,
Sep 16, 2008, 12:01:00 PM9/16/08
to vim...@googlegroups.com, Vim user support
On 16/09/08 07:21, .瀚 wrote:
> hi!
> I am using gvim 7.2.15 on WIndows XP pro. I find a strange thing about
> filereadable()
>
> Problem:
> 1. create a directory in C:, for example c:\try this
> 2. create a text file in the directory, for example try.txt
> 3.open vim
> :let filename = 'c:\\try\ this\\try.txt'
> :echo filereadable(filename)
> then vim output 0, very strange, but
> :echo filereadable("c:\\try\ this\\try.txt")
> results in 1

Since you used double quotes in the echo statement and single quotes in
the let statement, the value is not the same. Either try double quotes
in the let statement too, or 'C:\try this\try.txt' either side.

Note that Vim (even Vim for Windows) knows about forward slashes as path
separators: I would expect filereadable('C:/try this/try.txt') to return 1.

>
> real issue:
> I define a global variable in my vimrc
> if has('win32')
> let g:vimfiles = $HOME.'\vimfiles\'
> endif
> let g:vimfiles = escape(g:vimfiles, ' \')
> so I can use this:
> silent execute 'helptags '.g:vimfiles.'doc'| " Tag help files
> execute 'setlocal tags+='.g:vimfiles.'stltags'| " Add Stl tags
> ....
> it's convenient, but when use
> filereadable(g:vimfiles . "path_to_sth_readable")
> vim return me 0
>
> Does anyone have a good solution to this ? Thanks!

see above


Best regards,
Tony.
--
hundred-and-one symptoms of being an internet addict:
174. You know what a listserv is.

Ivan Tishchenko

unread,
Sep 16, 2008, 2:39:59 PM9/16/08
to vim...@googlegroups.com
By the way, try fnamemodify() too. It will help you automate
slash-conversion; e.g. if you have variable fname with win32 filename
(i.e. with backslashes), you can easily convert it with
fnamemodify(fname, ':p') to "good" format. :)

WBR,
Ivan.

.瀚

unread,
Sep 16, 2008, 8:48:34 PM9/16/08
to vim...@googlegroups.com
Thanks very much!
Reply all
Reply to author
Forward
0 new messages