Re: ed.ex :: file backup routine
- Posted by K_D_R Jan 17, 2012
- 1133 views
Looks fine to me. I guess you are intending the contents of the original and backup files to be the same. For some reason I though the backup file would hold the contents that the original file had before it was overwritten.
The loop is ok. There might be a way or two to do it more efficently, but it's pretty good overall.
Thanks for looking at the code again - it had "problems" so I rewrote it again - this time without loops.
My intent is to copy/backup the file on the disk and then save the "live" or current file to disk. My desire is to have a backup trail. I am now thinking about about having the back up routine append "~" to previous backups. In other words, if I back-up myfile.ex, myfile.ex gets written to the file as is, a normal save. The previous version would be copied or moved to myfile.ex, unless there is already a myfile.ex in which case that file would be moved/renamed to myfile.ex~ and so on perhaps 3 or 4 levels deep. The oldest file, say myfile.ex~~ would be trashed/overwritten on the next back-up. LOL, maybe when I can master an elementary "loops". Hope springs eternal.
This is my latest effort:
elsif command[1] = '~' then status = 0 if not file_exists(file_name) then -- in "ed.ex" parlance "write" file: save_file(file_name) stop = FALSE end if sequence backup_file_name = file_name & "~" sequence backup_files = dir("*.*~") for x = 3 to length(backup_files) do backup_file_history = update_history(backup_file_history, backup_files[x][D_NAME]) end for if not file_exists(backup_file_name) then status = eu_filesys:copy_file(file_name, backup_file_name, 0) save_file(file_name) stop = FALSE set_top_line(" ") set_top_line(file_name & " <backed up as> " & backup_file_name) else -- overwrite existing file set_top_line(" ") set_top_line("file: " & backup_file_name & " already exists - overwrite? ") first_bold("y or ") first_bold("n : ") answer = key_gets("yn", {}) if find('y', answer) then status = eu_filesys:copy_file(file_name, backup_file_name, 1) save_file(file_name) stop = FALSE set_top_line(" ") set_top_line(file_name & " <backed up as> " & backup_file_name) else -- rename backup_file_name to filename not in backup history set_top_line(" ") set_top_line("rename " & backup_file_name & " ? : ") first_bold("y or ") first_bold("n : ") answer = key_gets("yn", {}) if find('y', answer) then set_top_line(" ") set_top_line("new file name: ") loop do answer = delete_trailing_white(key_gets("",backup_file_history)) until length(answer) != 0 and not find(answer, backup_file_history) end loop backup_file_name = answer backup_file_history = update_history(backup_file_history, answer) stop = TRUE status = eu_filesys:copy_file(file_name, backup_file_name, 0) save_file(file_name) stop = FALSE set_top_line(file_name & " <backed up as> " & backup_file_name) else -- overwrite an existing backup_file_name from backup_file_history answer = delete_trailing_white(key_gets("",backup_file_history)) if length(answer) !=0 then backup_file_name = answer backup_file_history = update_history(backup_file_history, answer) stop = TRUE end if status = eu_filesys:copy_file(file_name, backup_file_name, 1) save_file(file_name) stop = FALSE end if end if end if
Regards, Kenneth Rhodes