Jump to content

SciTE - backup of files on save


Recommended Posts

  • Developers

Is there a way have SciTE to save the previous version of a file when it saves the new file?

The other script editors I have used save at least 1 and usually several previous versions of files.

<{POST_SNAPBACK}>

This can be done with a LUA script.

Add this code to AutoIt3.LUA and the file will always be moved to "filename".bak before the new version is saved.

function OnBeforeSave(filename)
  os.remove (filename..".bak")
  os.rename (filename, filename..".bak")
  return false
end

I can add it to the standard installer when ppl find this useful .... :lmao:

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

Will add the below LUA script to AutoIt3.LUA to facilitate to save backups of your Autoit3 script. It will be configurable by specifying the following in your SciTeUser.properties:

#define the number of backup files you want to keep   0=none
backup.files=3

function OnBeforeSave(filename)
    local sbck = tonumber(props['backup.files'])
    -- no backup specified
    if sbck == nil or sbck == 0 then
        return false
    end
    local nbck = 1
    while (sbck > nbck ) do
        local fn1 = sbck-nbck 
        local fn2 = sbck-nbck+1 
        os.remove (filename.. "." .. fn2 .. ".bak")
        if fn1 == 1 then
            os.rename (filename .. ".bak", filename .. "." .. fn2 .. ".bak")
        else
            os.rename (filename .. "." .. fn1 .. ".bak", filename .. "." .. fn2 .. ".bak")
        end
        nbck = nbck + 1
    end
    os.remove (filename.. "." .. ".bak")
    os.rename (filename, filename .. ".bak")
    return false
end

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

After looking at your code, I became a little interested in Lua.

I modified the code to create the backups in a different folder on the same drive as the original source and I used ".1.bak" for the first backup instead of ".bak".

There was nothing wrong with your code...I just started playing with Lua to see what I could do.

--
function OnBeforeSave(filename)
    local sbck = tonumber(props['backup.files'])
    -- no backup specified
    if sbck == nil or sbck == 0 then
        return false
    end

    local my_backup_path = (props['backup.path'])
    if (my_backup_path == nil) or (my_backup_path == 0) then
        my_backup_path = "d:\\Sci-TE-Backup"
    end

    -- Determine where to store the backups
    local my_loc_of_last_backslash = (string.find(filename,"[^%\\]*$",1))
      if my_loc_of_last_backslash == nil then
        return false
    end
    my_loc_of_last_backslash = my_loc_of_last_backslash - 1

    -- Extract the last part of the filename
    local my_filename = string.sub(filename,my_loc_of_last_backslash + 1,-1)

    -- Extract the drive letter
    my_drive_letter = string.sub(filename,1,2)

    -- Format complete path to location where backups will be stored
    my_backup_path = my_drive_letter .. string.sub(my_backup_path,3,-1)

    -- Shift existing backups up by 1
    local my_tmp_filename1 = ''
    local my_tmp_filename2 = ''
    local my_index = sbck
    while (my_index >= 1) do
        my_tmp_filename1 = my_backup_path .. "\\" .. my_filename .. "." .. my_index .. ".bak"
        os.remove(my_tmp_filename1)
        if my_index ~= 1 then
            my_tmp_filename2 = my_backup_path .. "\\" .. my_filename .. "." .. (my_index - 1) .. ".bak"
            os.rename(my_tmp_filename2, my_tmp_filename1)
        end
        my_index = my_index - 1
    end

    -- Rename the original file to .1.bak
    my_tmp_filename2 = my_backup_path .. "\\" .. my_filename .. ".1.bak"
    os.rename(filename, my_tmp_filename1)

    return false
end

Will add the below LUA script to AutoIt3.LUA to facilitate to save backups of your Autoit3 script. It will be configurable by specifying the following in your SciTeUser.properties:

#define the number of backup files you want to keep   0=none
backup.files=3

function OnBeforeSave(filename)
    local sbck = tonumber(props['backup.files'])
    -- no backup specified
    if sbck == nil or sbck == 0 then
        return false
    end
    local nbck = 1
    while (sbck > nbck ) do
        local fn1 = sbck-nbck 
        local fn2 = sbck-nbck+1 
        os.remove (filename.. "." .. fn2 .. ".bak")
        if fn1 == 1 then
            os.rename (filename .. ".bak", filename .. "." .. fn2 .. ".bak")
        else
            os.rename (filename .. "." .. fn1 .. ".bak", filename .. "." .. fn2 .. ".bak")
        end
        nbck = nbck + 1
    end
    os.remove (filename.. "." .. ".bak")
    os.rename (filename, filename .. ".bak")
    return false
end

<{POST_SNAPBACK}>

Link to comment
Share on other sites

  • Developers

After looking at your code, I became a little interested in Lua.

I modified the code to create the backups in a different folder on the same drive as the original source and I used ".1.bak" for the first backup instead of ".bak".

There was nothing wrong with your code...I just started playing with Lua to see what I could do.

<{POST_SNAPBACK}>

Didn't have much time to look at this but just a couple of observations/questions:

- Will it also work when the "backup" directory doesn't exists?

- I think it should default to the script directory when the 'backup.path' isn't defined.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I couldn't figure out how to create the backup directory in LUA in a direct manner, but I do believe I have it working...it does show a flash of a black DOS window if it has to create the directory.

The backup probably should default to the source directory. I was having so so so many backups in my source directory that it became unmanageable. I put code in to have it default to the source filename's directory.

I made a couple of changes (I left the my_ prefix in front of what I added):

--
function OnBeforeSave(filename)
    local sbck = tonumber(props['backup.files'])
    -- no backup specified
    if sbck == nil or sbck == 0 then
        return false
    end

    -- Find the last backslash in filename in order to format a path to the backup folder
    local my_loc_of_last_backslash = (string.find(filename,"[^%\\]*$",1))
      if my_loc_of_last_backslash == nil then
        return false
    end
    my_loc_of_last_backslash = my_loc_of_last_backslash - 1

    -- Extract the last part of the filename
    local my_filename = string.sub(filename,my_loc_of_last_backslash + 1,-1)

    -- Extract the drive letter
    local my_drive_letter = string.sub(filename,1,2)

    -- Extract filename's path
    local my_path = string.sub(filename,1,my_loc_of_last_backslash - 1)

    -- Retrieve path to backup folder from properties file.  Default to filename's path.
    local my_backup_path = (props['backup.path'])
    if (my_backup_path == nil) or (my_backup_path == 0) then
        my_backup_path = my_path
    end

    -- Format complete path to location where backups will be stored.  Create path if it does not exist
    my_backup_path = my_drive_letter .. string.sub(my_backup_path,3,-1)
    if (os.rename(my_backup_path,my_backup_path)) == nil then
        os.execute('mkdir "' .. my_backup_path .. '"')
    end

    -- Shift existing backups up by 1
    local my_tmp_filename1 = ''
    local my_tmp_filename2 = ''
    local my_index = sbck
    while (my_index >= 1) do
        my_tmp_filename1 = my_backup_path .. "\\" .. my_filename .. "." .. my_index .. ".bak"
        os.remove(my_tmp_filename1)
        if my_index ~= 1 then
            my_tmp_filename2 = my_backup_path .. "\\" .. my_filename .. "." .. (my_index - 1) .. ".bak"
            os.rename(my_tmp_filename2, my_tmp_filename1)
        end
        my_index = my_index - 1
    end

    -- Rename the original file to .1.bak
    my_tmp_filename2 = my_backup_path .. "\\" .. my_filename .. ".1.bak"
    os.rename(filename, my_tmp_filename1)

    return false
end

Didn't have much time to look at this but just a couple of observations/questions:

- Will it also work when the "backup" directory doesn't exists?

- I think it should  default to the script directory when the 'backup.path' isn't defined.

<{POST_SNAPBACK}>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...