Page 1 of 1

Make backups in one folder with changed names

Posted: 22 Feb 2021 22:34
by Alter
Hello! I searched forum and haven't found anything that meets my criteria, and I'm complete noob at scripting, so here I am.
I want to make backups of selected files in Backups folder with names changed to meet a key. Basically:

I highlight files, for example:
C:/Files/Home_picture.jpg
C:/Files/Program.exe

Script creates folder in that root called Backups
C:/Files/Backups

Copies selected files, and pastes them in Backups folder with changed names meeting a key [filename_backup.extension], for example
C:/Files/Backups/Home_picture_backup.jpg
C:/Files/Backups/Program_backup.exe

If i select new files and folder Backups already exist in their folder, it copies inside it instead.

I hope something like this could be done. Thanks in advance!

Re: Make backups in one folder with changed names

Posted: 22 Feb 2021 23:16
by highend

Code: Select all

    $items     = <get SelectedItemsNames <crlf>>;
    $subExists = (exists("<curpath>\Backup") == 2) ? 1 : 0;
    if (!$subExists) { new("Backup", "dir"); }

    foreach($item, $items, <crlf>, "e") {
        if (exists($item) == 2) { continue; }

        // Backup subfolder already exists
        if ($subExists) {
            copyitem "<curpath>\$item", "<curpath>\Backup\$item";

        // Backup subfolder doesn't exist
        } else {
            copyitem "<curpath>\$item", "<curpath>\Backup\" . gpc($item, "base") . "_backup" . "." . gpc($item, "ext");
        }
    }

Re: Make backups in one folder with changed names

Posted: 23 Feb 2021 07:57
by Alter
Thank you very much! Works like a charm

Re: Make backups in one folder with changed names

Posted: 23 Feb 2021 10:50
by Horst
Alter wrote: 23 Feb 2021 07:57 Thank you very much! Works like a charm
Why not making something like _date_time instead of _backup.
Then you can have multiple versions of each file in the backup sub-dir.