miercuri, 6 ianuarie 2010

jedit - locally backup with timestamp the FTP modified files

Starting from
http://community.jedit.org/?q=node/view/1209#7340 - which does only 1 file, I wanted a file for each save. I've done it using timestamp in format yyyymmddhhmmss.
(error: if system date is changed, it's possible a file to be overwritten)


import java.util.Calendar;
import java.text.SimpleDateFormat;

//backup path
BackupPath = "Z:\\Jedit_backups\\"; //don't forget the trailing \\

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
timestamp = sdf.format(cal.getTime());

path = buffer.getDirectory();
path = path.replaceFirst("ftp://", "");
path = path.replace('/', '\\');
path = path.replaceAll(":", ""); //this is for local files (c:\ -> c\

//todo : remove other unwanted characters ?

path = BackupPath + path;

(new File(path)).mkdirs(); //create the backup directory if it doesn't exist

path = path + buffer.getName() + '_' + timestamp;
// buffer.getLastModified(); - fails for FTP: puts 0 - all the time

buffer.save(view,path,false); //save copy

Macros.message(view, "Backup done in " + BackupPath);


- put this in a file, eg save-backup.bsh
- put the file in a subfolder (eg Files) of 'macros' folder from jedit installation
- open jedit
- Utilities > Global Options > Shortcuts
- enter, in the 'filter' field, the name of the file
- click in the 'Primary shortcut'
- press CTRL + s (the 'Save' shortcut)
- accept the override
- in 'Saving & Backup' put the 'Max number of backups' to 0 (zero - it disables other backups)
- press OK to save changes
(done in Jedit 4.3)

Un comentariu:

  1. view.getStatus().setMessageAndClear( "Backup done in " + BackupPath);

    to escape of annoying message window.