On Wednesday 05 July 2006 12:32, you wrote:
> Let's say I have a .filename.swp as the swap file. Is it possible to 
> automatically recover and store the recovered file as filename.recovered? I 
> would like to do this non-interactively and for multiple files. Any ideas?
> 
A great question I think - maybe vim has a way to handle these files simply, 
but I don't know of it.

If it helps, below is the perl script I use to find .*.sw* files, open them 
with -r, save them with a "new_" prefix, compare the new to the original file 
they came from, and delete the new if they are the same.
If not the same, open them both with gvimdiff (hence the "new_" prefix rather 
than extension - so syntax highlighting still works).

I'm sure someone will have a better way though... a vim script perhaps?

#!/usr/bin/perl -w

use strict;
my $v = 'gvim -f';
my $vd = 'gvimdiff -f';

my $dir = '.';
if (scalar @ARGV)
{
    $dir = $ARGV[0];
}
my $cmd = "find $dir -name '.*.sw*'";
my @files=`$cmd`;

foreach my $file (@files)
{
    my $filepath;
    my $filename;
    my $newfile;
    my $oldfile;
    chomp $file;
    if ($file =~ /^(.*)\/\.(.*)\.sw.$/)
    {
        $filepath = $1;
        $filename = $2;
        $oldfile = "$filepath/$filename";
        $newfile = $filepath . "/new_" . $filename;
        $cmd = "$v -r $file -c 'write $newfile|quit'";
        print "$cmd\n";
        system($cmd);
        unlink "$file";

        if (system("diff '".$oldfile."' '".$newfile."'"))
        {
            # Files differ
            print "'$oldfile' differs from '$newfile'\n";
            system("$vd '$oldfile' '$newfile'");
        }
        else
        {
            print "'$oldfile' not changed\n";
            unlink "$newfile";
        }
    }
}

Reply via email to