Re: Budget Builder

new topic     » goto parent     » topic index » view thread      » older message » newer message
--   xbackup.e 
--    Kenneth Rhodes 
 
--   backup current file, current directory, or a list of files saved to a text file  
--   with the file extension "prj" -"wee.prj", for example. Creates directory  
--   "Archive" in current directory. All files in current directory will be copied  
--   to a subdirectory named with a timestamp, "dt". 
 
--  All files listed in a text file with the extension "prj" 
--  will be copied to  a subdirectory labeled "filename.prj" 
--  in a subdirectory labeled with a timestamp, "dt". 
 
-- backupfiles("backup.e") "backup.e" copied to /home/kenneth/wee/Archive/backup.e/dt 
-- backupfiles() current directory files placed in /home/kenneth/wee/Archive/dt 
-- backupfiles("wee.prj") "wee.prj" files > /home/kenneth/wee/Archive/wee.prj/dt 
 
include std/filesys.e 
include std/sequence.e 
include std/text.e 
include std/error.e 
include std/console.e 
include std/sort.e 
include std/io.e 
 
enum PATH, FILES 
object TMP = {{},{}}, files={} 
sequence today, dt 
global object BACKUPDIR, sourceDir="", 
brd={--"brd" stands for backup, replace, delete. 
0, -- integer flag for operation sucess or failure: 0 = failure, 1 = sucess  
"", "" -- place holders for sequences msg and backup directory 
}                               
function load_directory_files(sequence path) 
      -- load files from directory, path,  into sequence files 
      -- returns a sequence of file names with an absolute path 
      object file = "" 
      TMP[PATH] = path & SLASH 
      TMP[FILES] = dir(path) 
      if not atom(TMP[FILES])then 
            if length(TMP[FILES]) >=3 then 
                  for x = 3 to length(TMP[FILES]) do  
                        if not equal(TMP[FILES][x][D_ATTRIBUTES],  "d") then 
                              file = TMP[PATH] & TMP[FILES][x][D_NAME] 
                              files = append(files,canonical_path(file)) 
                        end if 
                  end for 
            end if 
      end if 
return sort(files) 
end function 
 
global function backupfiles(sequence file_or_list = "") 
sequence msg ="", MS= "\n\tThe original source code file: \"%s\","\n\thas been archived in:\n\"%s\"\n\n"     
integer BCF = 0 -- switch: 1 = Backup Current FILE 
  if equal(file_or_list, "") then--backup all the files in the current directory 
     files=load_directory_files(sourceDir) 
     BACKUPDIR=sourceDir &SLASH &"Archive"           
  elsif equal("prj", fileext(file_or_list)) then -- backup("myprog.prj") 
     --backup files listed in files with "prj" file extension   
     BACKUPDIR=dirname(file_or_list) & SLASH & "Archive" 
     sourceDir=dirname(file_or_list) 
     BACKUPDIR=BACKUPDIR & SLASH & filebase(file_or_list) &"."& fileext(file_or_list) 
     files=read_lines(file_or_list) 
  else  --Backup the current file 
        BACKUPDIR=dirname(file_or_list)& SLASH & "Archive" 
        BACKUPDIR=BACKUPDIR&SLASH&filebase(file_or_list)&"."&fileext(file_or_list) 
        BCF=1 
        files=file_or_list 
  end if 
  if not atom(files) then 
       if file_exists(BACKUPDIR) or create_directory(BACKUPDIR) then end if  
       today=date() 
       dt=sprintf("%04d:",today[1]+1900) & -- year 
          sprintf("%02d:",today[2]) & -- month 
          sprintf("%02d:",today[3])& -- day 
          sprintf("%02d:",today[4])& -- hour 
          sprintf("%02d",today[5]) -- minute 
       BACKUPDIR=BACKUPDIR &SLASH& dt 
       if file_exists(BACKUPDIR) or create_directory(BACKUPDIR) then 
          if BCF then --Backup Current File:backupfiles("wee.exw")  
            if copy_file(file_or_list,BACKUPDIR,1) then  
              msg &= sprintf(MS,{file_or_list,BACKUPDIR}) 
              brd={1,msg,BACKUPDIR} 
            end if 
          else-- backup all files in the current directory: backupfiles(), 
              -- or backup files listed in "*.prj file:backupfiles("wee.prj") 
             for x=1 to length(files) do printf(1,"%s\n",{files[x]}) 
                if not absolute_path(sprintf("%s",{files[x]})) then  
                   if copy_file( sourceDir&SLASH& files[x],BACKUPDIR,1) then  
                      msg &= sprintf(MS,{files[x],BACKUPDIR})  
                      brd={1,msg,BACKUPDIR} 
                   end if 
                else -- files[x], is an absolute path 
                    if copy_file(sprintf("%s",{files[x]}),BACKUPDIR,1) then   
                        msg &= sprintf(MS,{files[x],BACKUPDIR}) 
                        brd={1,msg,BACKUPDIR} 
                    end if 
                end if 
            end for 
         end if 
       else 
         msg &=sprintf("\nBackup operation failed! Failed to create %s\n"{BACKUPDIR}) 
         brd={0,msg,BACKUPDIR} 
       end if 
    else msg &= "\nBackup operation failed!\n" 
         brd={0, msg, BACKUPDIR} 
    end if 
return brd 
end function 

Regards, Ken

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu