Call external command with relative path

45 views
Skip to first unread message

oversky

unread,
May 30, 2012, 3:28:53 AM5/30/12
to vim_use
I install gvimPortable in a thumb drive.
The $VIMRUNTIME is d:\Dropbox\Portable\GVimPortable\App\vim\vim73\.
I have a character encoding converter located in d:\Dropbox\Portable
\ConvertZ\ConvertZ.exe
Currently, I use :%!D:\PROGRA~1\ConvertZ\ConvertZ.exe /i:utf8 /o:big5
% to run the converter.
However, when I plug the thumb drive into another computer,
the driver letter of the thumb drive may change to another one.
How do I change the absolute path of convertz.exe to a relative path
referring to $VIMRUNTIME?

Benjamin R. Haskell

unread,
May 30, 2012, 4:29:46 AM5/30/12
to vim_use
You should be able to get the prefix (D:\Dropbox\Portable) by using:

let $PORTABLE = fnamemodify($VIMRUNTIME, ':h:h:h:h')

You might need to change the number of :h's. There may also be fewer
:h's if you use $VIM instead of $VIMRUNTIME.

Then your command can become:

:%!$PORTABLE\ConvertZ\ConvertZ.exe /i:utf8 /o:big5

You may also wish to add ':8' to the modifiers, so that you get a
filename that will never have spaces in it.

See:
:help fnamemodify()
:help filename-modifiers

--
Best,
Ben

oversky

unread,
May 30, 2012, 10:19:58 AM5/30/12
to vim_use
I still have problem.
I check with :echo $PORTABLE,
it shows correct path.
But the error message still says it can't find the file.
Then I use :! instead of :%!,
the command paste to shell is

C:\WINDOWS\system32\cmd.exe /c $PORTABLE\ConvertZ\ConvertZ.exe /
i:sjis /o:utf8 1.cue

$PORTABLE is not replaced by its value.
Any suggestion?

Christian Brabandt

unread,
May 30, 2012, 10:29:06 AM5/30/12
to vim...@googlegroups.com
:h expand()

regards,
Christian

Charles Campbell

unread,
May 30, 2012, 1:10:12 PM5/30/12
to vim...@googlegroups.com
I think you'll need to know about

:he exe

ie.
:let PORTABLE = fnamemodify($VIMRUNTIME, ':h:h:h:h')
:exe '%!C:\WINDOWS\system32\cmd.exe /c
'.PORTABLE.'\ConvertZ\ConvertZ.exe /'

or, if you prefer,
:exe '%!C:\WINDOWS\system32\cmd.exe /c
'.fnamemodify($VIMRUNTIME, ':h:h:h:h').'\ConvertZ\ConvertZ.exe /'

Now for the caveats: I'm not using Windows, don't have ConvertZ.exe, am
not using a thumb drive, so the above has not been tested.

Regards,
Chip Campbell

oversky

unread,
May 31, 2012, 2:21:18 AM5/31/12
to vim_use
On 5月30日, 下午10時29分, "Christian Brabandt" <cbli...@256bit.org> wrote:
>
> :h expand()
>
> regards,
> Christian

I tried expand with <C-R>, the following line works successfully.
:%!<C-R>=expand("$PORTABLE")<CR>\ConvertZ\ConvertZ.exe /i:big5 /o:gbk
%<CR>

Thanks for everyone's help.
Reply all
Reply to author
Forward
0 new messages