[script] Simple Backup System for RJ TE

Ask questions about how to create a script or swap scripts with other users.
Post Reply
User avatar
pjj
Posts: 2108
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

[script] Simple Backup System for RJ TE

Post by pjj »

Thanks to Rickard's fantastic support for script development (and his terrific editor in general!), I am now able to present quite elaborated script for backing up your source files, hoping you will find it useful.

Goal of Simple Backup System for RJ TextEdit (SBS) is to store, depending on your configuration and backup mode you've chosen, all or some files located in a given project directory and its subdirectories. The archive is saved into #backup subdirectory. There are two modes for backup: full and partial. Archive files have timestamps in their names, and each run (backup type, date & time, comment) is also logged into a text file for a quick reference. -- This is, of course, not a full-blown versioning system, but I hope it will come in handy nevertheless.

I tried to painstakingly describe everything related to SBS, so please refer to readme.txt file contained in the SBS archive, available to download below. Please notice that you need RJ TE in version at least 10.20 to run this script.
sbs-1.0.zip
(5.98 KiB) Downloaded 751 times
(re-uploaded to the new forum; previously downloaded 15 times)

If you have any comments or feature requests, please write them in this thread.

SBS is licensed as beerware :wink:
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus
User avatar
pjj
Posts: 2108
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

Re: [script] Simple Backup System for RJ TE

Post by pjj »

Funny thing, but if you run backup script before noon, with some locales filename could be corrupted: time part (and, which is worse, file extension) could be completely omitted due to a space leading hour digit (instead of 0); to correct this, just change one line:

Code: Select all

SET archive_name=%date:/=%@%time:~0,2%h%time:~3,2%m
with these two:

Code: Select all

FOR /F "tokens=1-2 delims=/: " %%a in ("%TIME%") DO (if %%a LSS 10 (SET time=0%%ah%%bm) else (SET time=%%ah%%bm))
SET archive_name=%date:/=%@%time%
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus
Andreas
Posts: 10
Joined: 05 May 2016 18:44
Location: DE Mönchengladbach

Re: [script] Simple Backup System for RJ TE

Post by Andreas »

Hello PJJ,

I need a way more easier backup script and I'm not working with projects. I took a look at your script and thought I could change i to my needs. But I think it's to complicated for me. I'm a webdeveloper with knowledge of PHP, JavaScript ... but I don't know Fastscript and RJ TextEd is new to me.

Maybe you can find the time to change it for me|us or lead me to the right direction?

What I want to achieve is, that every time I save a file opened via FTP on the server, that TextEd automaticaly saves a copy of this file local with the appropriate folder structure as on the server. E.g.

Code: Select all

hostname/folder/folder/my-file.js
All I should have to do is to set a parent backup folder somewhere.

Do you think that this is possible with TextEd?

ps
I worked for about 15 years with the editor PsPad. But this has now too many crashes, etc. And I'm looking for a new editor. PsPad had always automatically saved my open files as backup in a backup folder. This folder has always been a helpful collecturium in which I could search for old code snippets and old solutions. Unfortunately, I miss this option in RJ Texted.

Regards Andreas
User avatar
pjj
Posts: 2108
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

Re: [script] Simple Backup System for RJ TE

Post by pjj »

I'd love to help, but perhaps it would be feasible for you without any script? There's an option to save remote files locally (Options > Editor > File > Save remote files), but you need to pick location for each saved file.

If you decide that this is not what you want, sure, we can tackle the script. I guess it will be not too complicated: just find current directory, find backup directory based on this, save current file into the backup directory with date in its name -- and run this script when file is opened (or saved). Do you want to give it a try yourself? Or would you prefer that I do it?

One more thing: FastScript runs JavaScript (and three other languages), and what you saw in my script that indeed could have been a bit intimidating :wink: was just GUI. In this script we won't need any of this.
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus
User avatar
pjj
Posts: 2108
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

Re: [script] Simple Backup System for RJ TE

Post by pjj »

After some tinkering I came up with something like this:

Code: Select all

var i, j;

var docNameFull = Document.FileName;
var docPath, docNameExt, docName, docExt;

i = ScriptUtils.RightPosFrom("\\", docNameFull, 999);
j = ScriptUtils.RightPosFrom(".", docNameFull, 999);
docPath = copy(docNameFull, 0, i);
docExt = copy(docNameFull, j + 1, 999);

docNameExt = copy(docNameFull, Length(docPath) + 1, 999);
docName = copy(docNameExt, 0, Length(docNameExt) - Length(docExt) - 1);

var now = DateToStr(Date()) + "_" + copy(TimeToStr(Time()), 0, 5);
now = ScriptUtils.StringReplaceAll(now, ":", "-");

var text = Document.Text;

// target file doesn't exist yet
Word mode = fmCreate or fmOpenReadWrite;

// save target file
TFileStream stream = TFileStream.Create(docPath + "#backup" + "\\" + docName + "_" + now + "." + docExt, mode);
try {
    stream.WriteString(text, encUTF8);
}
finally {
    stream.Free;
}
A couple of notices and caveats:
  • file must have extension (any)
  • #backup dir needs to be located in the same dir as the file
  • you can run this script manually or on document save; on document open it won't do anything, since document name etc. are not known yet
  • file is saved as UTF8, you can use encAnsi or encUnicode, too
  • you could use Document.SaveAsNoMRU and run script on document open, but then standard Windows dialog for saving would open and you still would need to click to go to your backup directory
I hope it will help you a bit, somehow :)
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus
Post Reply