Creating backup files on my script

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Nixcalo
Posts: 116
Joined: 06 Feb 2018, 04:24

Creating backup files on my script

19 Sep 2021, 21:42

I am writing a critical and very large script and I am constantly changing/updating it. I fear that some day, I might add a modification to the script that is not working and I won't be able to revert it (or the PC crashes, or whatever reason).

So I have tried to create backup copies of the script in a safe folder every time it runs, wiuth this line at the beginning of the script.
FileCopy, % A_ScriptFullPath, % SubStr(A_ScriptFullPath, 1, -3) . "RUN-TIME COPY.AHK", 1 ; Backup copy in case of catastrophic error

I have been able to create ONE backup copy with no problem, but I have found that is not enough. I might have to revert the script to the version I was using hours ago, and thus one backup copy is not enough. I need five at least.
So I did something like this...

Code: Select all

 num:=1
   While num<5 {
	
	If FileExist(A_ScriptDir . "\Backups Autohotkey organizado y funciones\" . SubStr(A_ScriptName, 1, -4) . "versión " . num . ".AHK")
		num++
	else
		{
			FileCopy, % A_ScriptFullPath, % A_ScriptDir . "\Backups Autohotkey organizado y funciones\" . SubStr(A_ScriptName, 1, -4) . "versión " . num . ".AHK", 1 ; Hago copia de seguridad en caso de error catastrófico
			Break
		}
}
Of course, I soon realized this is stupid. It will create successive versions (from 1 to 5) of my script if they are not already created. But then what? I need this script to know if there are no more free slots and then start again with 1 to 5, endlessly , repeating the cycle, so I have at least 5 versions of the script. I don' t mind that version 2 might be more updated than version 3 because I can always look at the timestamp, but I don't know how to do this thing.

So , in short, I need the script to check if there are 5 versions of my script. If there are less, create another one with a higher version number (until 5). If all five versions exist, restart making backup copies of the script starting with 1, next time with 2, etc.

I am not sure how the script can keep a check on what file was saved last (and making it time-efficient!) and then incrementing the version number by one, unless it's 5, when it should start back to 1.

Did I explain myself?

Thank you!
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Creating backup files on my script

19 Sep 2021, 21:59

You’re making it more complicated by making it reuse 1 to 5 and have it figure out which is oldest and replace that. Just keep making backups with a time stamp (using A_Now) as part of the file name, and have it delete the one with the oldest time stamp when there are 6 of them. When you use Loop, Files, it loops through them in an alphanumeric order, so the last one will be the oldest. So if the A_Index of the file loop equals 6, delete that file.
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Creating backup files on my script

19 Sep 2021, 22:35

Maybe this is useful to you: viewtopic.php?style=17&t=76062&p=419446#p329520
If you need to revert back hours ago you may need more than just 5. :?:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Holarctic, jameswrightesq and 432 guests