Jump to content

Scite backup copies


Recommended Posts

  • Developers

This version will always have a 2 digit backup sequence number and rename any single digit to 2 digits.

--------------------------------------------------------------------------------
-- run a cmd command and capture the output
--------------------------------------------------------------------------------
function perform_oscommand(cmd)
   local handle=io.popen("" .. cmd)
   local consoleoutput = handle:read('*all')
   handle:close()
   return consoleoutput
end

--------------------------------------------------------------------------------
-- OnBeforeSave(filename)
--
-- keep the number of backups as defined by backup.files = ?    by Jos van der Zande (Jos)
--
-- AutoItTools.OnBeforeSave
--------------------------------------------------------------------------------
function AutoItTools:OnBeforeSave(filename)
    -- skip making the backup when file isn't changed
    if not editor.Modify  then
        return false
    end
    local backupfiles = tonumber(props['backup.files'])
    -- backup specified and bigger than 0 ?
    if backupfiles == nil or backupfiles == 0 then
        return false
    end
    -- get file directory
    fdir = filename:match(".+[/\\]")
    fname = filename:match(".+[/\\](.+)")
    fext = fname:match(".+(%..+)") or ""
    if fext ~= "" then
        fname = fname:match("(.+)%..+")
    end
    -- set backup directory
    if props['backup.directory'] == "" then
        bdir = fdir .. "backup"             -- no fixed backupdirectory location defined
    else
        bdir = props['backup.directory']    -- use the backupdirectory location defined for backup.directory
    end
    -- set backup rename to extra string. default   Filename_bakx.Fileext
    if props['backup.renameto'] == "" then
        bmid = "_bak"                      -- default
    else
        bmid = props['backup.renameto']    -- use the backupdirectory location defined for backup.directory
    end
    -- ensure the backup directory exists
    perform_oscommand('mkdir "'..bdir..'"')
    --
    -- check for all current existing backup files
    local currentbackupfiles = 1
    local tfile = ""
    local fh=0
    while (1) do
        tfile = bdir.."\\".. fname..bmid..currentbackupfiles..fext
        tfile2 = bdir.."\\".. fname..bmid..string.format("%02d",currentbackupfiles)..fext
        fh = io.open (tfile, "r")
        if fh == nil then
            fh = io.open (tfile2, "r")
            if fh == nil then break end
            io.close(fh)
        else
            io.close(fh)
            os.rename (tfile,tfile2)
        end
        currentbackupfiles = currentbackupfiles + 1
    end
    -- move old backupfiles to 1 to (backupfiles-1)
    if backupfiles > 0 and backupfiles < currentbackupfiles then
        for y=1,currentbackupfiles-1 do
            y2 = y + backupfiles - currentbackupfiles
            local fn1 = bdir.."\\".. fname..bmid..string.format("%02d",y)..fext
            local fn2 = bdir.."\\".. fname..bmid..string.format("%02d",y2)..fext
            if currentbackupfiles - y < backupfiles then
                os.remove (fn2)
                os.rename (fn1,fn2)
            else
                os.remove (fn1)
            end
        end
        currentbackupfiles = backupfiles
    end
    local fn1 = bdir.."\\"..fname..bmid..string.format("%02d",currentbackupfiles)..fext
    os.remove (fn1)
    os.rename (filename, fn1)
    return false
end

This isn't implemented in Tidy yet, but will do so when the approach is firmed-up. 

Jos 

Edited by Jos

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

  • Moderators

Jos,

You have to code it so you choose how you want to denominate the backups. This "old dog" has had to learn quite a few "new tricks" over the years and I am quite sure I can cope with a new SciTE backup numbering system if necessary!

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Developers
2 minutes ago, Melba23 said:

You have to code it so you choose how you want to denominate the backups.

That an option too .....  but requires an extra config option and more logic in both the above function and Tidy to cope with the 2 different logic's. :) 

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

  • Moderators

Jos,

I was not suggesting an option for the user! I was trying to say that I can live with whatever YOU decide YOU want to code!

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

1 hour ago, Dan_555 said:

Hi, i'm jumping into this with a request. Can you add leading 0 before the numbers ?

So that the sorting order, (in my case i'm sorting the files by the extension, not by the date), be correct.

Dan, this issue was addressed, at least in Windows Explorer, many versions ago. I believe there is a way to change the behaviour if required. Of course, the old dir command still lists files in the order you described but with Windows Explorer in my Windows 7 this is not a problem as it very cleverly treats the numeric portions of the file name/ext in ascending order. At first I thought this was a daft change by Microsoft but now I actually find it quite convenient.

Phil Seakins

Link to comment
Share on other sites

34 minutes ago, Jos said:

This version will always have a 2 digit backup sequence number and rename any single digit to 2 digits.

Very nice Jos. (I actually would prefer three digits but I think I would probably be outvoted as even I can see that being a bit over the top).

Phil Seakins

Link to comment
Share on other sites

  • Developers

The 2 digit version number is now also implemented in the current Tidy Beta version 19.1127.1402.6.
The naming convention is still fixed and the same: scriptname_oldxx.au3.

Jos

 

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

Both an update for SciTE.exe and AutoItTools.lua are available in Beta too. SciTE has the patch applied to suppress the popup box for os.execute. 
All you need to do is set this in SciTEUser,properties:

create.hidden.console=1

let me know when you see issues. :) 

Jos

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

  • 1 month later...
  • Developers

I had to make a small update as the Save-As would not save the file at all when there were no modifications. This was cause by returning a true in the first test.
This version fixes that:

--------------------------------------------------------------------------------
-- run a cmd command and capture the output
--------------------------------------------------------------------------------
function perform_oscommand(cmd)
   local handle=io.popen("" .. cmd)
   local consoleoutput = handle:read('*all')
   handle:close()
   return consoleoutput
end

--------------------------------------------------------------------------------
-- OnBeforeSave(filename)
--
-- keep the number of backups as defined by backup.files = ?    by Jos van der Zande (Jos)
--
-- AutoItTools.OnBeforeSave
--------------------------------------------------------------------------------
function AutoItTools:OnBeforeSave(filename)
    -- skip making the backup when file isn't changed
    if not editor.Modify  then
        return false
    end
    local backupfiles = tonumber(props['backup.files'])
    -- backup specified and bigger than 0 ?
    if backupfiles == nil or backupfiles == 0 then
        return false
    end
    -- get file directory
    fdir = filename:match(".+[/\\]")
    fname = filename:match(".+[/\\](.+)")
    fext = fname:match(".+(%..+)") or ""
    if fext ~= "" then
        fname = fname:match("(.+)%..+")
    end
    -- set backup directory
    if props['backup.directory'] == "" then
        bdir = fdir .. "backup"             -- no fixed backupdirectory location defined
    else
        bdir = props['backup.directory']    -- use the backupdirectory location defined for backup.directory
    end
    -- set backup rename to extra string. default   Filename_bakx.Fileext
    if props['backup.renameto'] == "" then
        bmid = "_bak"                      -- default
    else
        bmid = props['backup.renameto']    -- use the backupdirectory location defined for backup.directory
    end
    -- ensure the backup directory exists
    perform_oscommand('mkdir "'..bdir..'"')
    --
    -- check for all current existing backup files
    local currentbackupfiles = 1
    local tfile = ""
    local fh=0
    while (1) do
        tfile = bdir.."\\".. fname..bmid..currentbackupfiles..fext
        tfile2 = bdir.."\\".. fname..bmid..string.format("%02d",currentbackupfiles)..fext
        fh = io.open (tfile, "r")
        if fh == nil then
            fh = io.open (tfile2, "r")
            if fh == nil then break end
            io.close(fh)
        else
            io.close(fh)
            os.rename (tfile,tfile2)
        end
        currentbackupfiles = currentbackupfiles + 1
    end
    -- move old backupfiles to 1 to (backupfiles-1)
    if backupfiles > 0 and backupfiles < currentbackupfiles then
        for y=1,currentbackupfiles-1 do
            y2 = y + backupfiles - currentbackupfiles
            local fn1 = bdir.."\\".. fname..bmid..string.format("%02d",y)..fext
            local fn2 = bdir.."\\".. fname..bmid..string.format("%02d",y2)..fext
            if currentbackupfiles - y < backupfiles then
                os.remove (fn2)
                os.rename (fn1,fn2)
            else
                os.remove (fn1)
            end
        end
        currentbackupfiles = backupfiles
    end
    local fn1 = bdir.."\\"..fname..bmid..string.format("%02d",currentbackupfiles)..fext
    os.remove (fn1)
    os.rename (filename, fn1)
    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

11 hours ago, Jos said:

I had to make a small update

What did you update? The code in this post is identical to the earlier post on 7/6/20

On 7/6/2020 at 9:26 PM, Jos said:

This version will always have a 2 digit backup sequence number

 

Phil Seakins

Link to comment
Share on other sites

  • Developers

mmm ...  you are right it was correct in the posted code but wrong in my own version on my Laptop, like it was in the posted code a page back.
...  guess I had a senior moment somewhere back then and didn't replace it with the correct version. 😕

Jos

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

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...