Tim Chase wrote:
Is there a function to create a backup directory if there
isn't one when Vim tries to backup a file?
Well, you can use the following code:
--------------------------------------------------
function! EnsureDirExists(d)
let l:s = substitute(a:d, '[/\\]*$', '/', '')
let l:isDir = isdirectory(resolve(expand(l:s)))
if !l:isDir
echo "Making directory...".l:s
call system("mkdir ".escape(s," "))
let l:isDir = isdirectory(resolve(expand(l:s)))
if !l:isDir
echoerr "Could not create directory: ".l:s." (is it already
a file?"
endif
endif
" remove the following line to cut down on
" verbosity...more here for testing purposes.
echo a:d." [".l:s."] was ".(l:isDir?"": "not ")."a directory"
endfunction
" test our function with various lines of
" data at the end of the file
$?^finish$?+,$v/^\s*"/call EnsureDirExists(getline('.'))
finish
" tests go here
"
" a nonexistant directory
~/nonexistant
" a directory
~/tmp
" a directory with backslash
~/tmp/
" a link to a directory
~/temp
" a link to a directory with backslash
~/temp/
" same as above, only using $HOME instead of "~" for paths
$HOME/nonexistant
$HOME/tmp
$HOME/tmp/
$HOME/temp
$HOME/temp/
" test a directory with a space in its name
~/tmp/foo bar
" test a directory with a space in its name and a backslash
~/tmp/foo bar/
" test the root directory
/
" test a blank line
" test a file
~/tmp/foo.txt
--------------------------------------------------
The EnsureDirExists function will try its darndest to ensure that the
directory passed to it exists, after which point, you can set your
backupdir option. You might have to tweak the "mkdir" call to either
use "-p" or whatever the win32 version is to make all components of a
path if it's multiple levels deep.
Just a few thoughts...
Wow, cool. I really need to learn Vim more. :-)
:Robert