Jump to content


Photo

Basic Backup Utility


  • Please log in to reply
15 replies to this topic

#1 shane

shane
  • Members
  • 8 posts

Posted 01 February 2007 - 11:43 AM

Hi, I was asked to create a backup util for use by someone who wanted to back all their photos up from their PC to another location on a fairly regular basis. They were currently using a drag and drop in windows to accomplish this but were not happy with the amount of time it was taking.

This is the util I put together for them, just in case its of any use to anyone else out there;

#SingleInstance, off
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.


; Gui Create Section

gui, font, s15, Arial
Gui, Add, Text, x17 y12 w440 h30, Shane's Backup Util
gui, font, s8, Arial

Gui, Add, Edit, vedtsource x16 y57 w300 h20, %SourceName%
Gui, Add, Button, vbtnsource gSOURCESELECT x356 y57 w100 h20, Source Select
Gui, Add, Edit,  vedttarget x16 y87 w300 h20, %TargetName%
Gui, Add, Button, vbtntarget gTARGETSELECT x356 y87 w100 h20, Target Select

Gui, Add, Text, vWorkArea x16 y130 w440 h70, Ready!

Gui, Add, Progress, vProg1 -smooth C808080 x16 y220 w440 h10, 0

Gui, Add, CheckBox, gWARNING vReplace x16 y240 w180 h20, Select to Replace Existing Files
Gui, Add, Button, vbtnbackup gBACKUP x256 y240 w100 h30, Run Backup
Gui, Add, Button, vbtnclose gGuiClose x366 y240 w90 h30, Exit App


Gui, Show, x131 y91 h280 w477, Shane's Backup Util

Return


SOURCESELECT:
   {
    ; Select a Source folder - Button on Gui
    SetControls( "disable" ) 
    GuiControlGet, Tempsource,,edtsource
    FileSelectFolder, SourceName, *%Tempsource% , 3, Select a Source Folder for your backup.   
    Sleep, 250
    Tempsource=
    SetControls( "enable" ) 
    GuiControl, , edtsource, %SourceName%
    Return
   }




TARGETSELECT:
   {
    ; Select a Target folder - Button on Gui
    SetControls( "disable" ) 
     GuiControlGet, Temptarget,,edttarget
    FileSelectFolder, TargetName, *%Temptarget% , 3, Select the destination you would like to backup to.   
    Sleep, 250
    Temptarget=
    SetControls( "enable" ) 
    GuiControl, , edttarget, %TargetName%
    Return
   }


BACKUP:
   {
    GuiControl, , WorkArea, Backup Running...
    FileDelete,  c:\sbprob.txt

    GuiControlGet, SourceName, ,edtsource
    GuiControlGet, TargetName, ,edttarget

    ;Check for and remove a trailing \ from SourceName
    StringRight, LastChar, SourceName, 1
    if LastChar = \
      {
            StringTrimRight, SourceName, SourceName, 1
       GuiControl, , edtsource, %SourceName%
      }

    ;Check for and remove a trailing \ from TargetName
    StringRight, LastChar, TargetName, 1
    if LastChar = \
      {
            StringTrimRight, TargetName, TargetName, 1
       GuiControl, , edttarget, %TargetName%
      }

    IfNotExist, %SourceName%
      {
       MsgBox, 0, Problem, The Source folder you have specified does not exist
       Return
      }

    IfNotExist, %TargetName%
      {
       MsgBox, 0, Problem, The Target folder you have specified does not exist
       Return
      }

    SetControls( "disable" ) 
    GuiControlGet, Replace, ,Replace

    ;Count how many files are to be copied by this process

    SourceCount=0
    Loop, %SourceName%\*.*, 0, 1
      {
        SourceCount++
      }

    ;Calculate the percentage to progress the bar by for each stage
    peradd=%SourceCount%
    peradd:=100/peradd

     ; Create the folder structure at the Destination if it does not already exist

     Loop, %SourceName%\*.*, 2, 1
      
      {
       Stringreplace, foldername, A_LoopFileFullPath, %SourceName%
       FileCreateDir, %targetName%\%foldername%
      }

    ; Perform the copy of files

   
    progadd=0
    Loop, %SourceName%\*.*, 0, 1
      {
       Stringreplace, filename, A_LoopFileFullPath, %SourceName%
       progadd:=progadd+peradd
       GuiControl, , Prog1, %progadd%

       ;Check for and remove a Starting \ from fileName
       StringLeft, FirstChar, filename, 1
       if FirstChar = \
         {
               StringTrimLeft, filename, filename, 1
         }
   
       ; Perform a basic copy not normally overwrite files - Also sets the Creation time to match the sourcefile
       FileCopy, %SourceName%\%filename%, %TargetName%\%filename%, %Replace%
       IfEqual, Errorlevel, 0
         {
          FileGetTime, stime, %SourceName%\%filename%, C
          FileSetTime, %stime%, %TargetName%\%filename%, C, 0, 0
          FileSetAttrib,  -A, %SourceName%\%fileName%, 0, 0
           GuiControl, , WorkArea, Backup Running...`nCopying %SourceName%\%filename% to %TargetName%\%filename%
          Continue
         }

       ; Check if the file has been modified (Archive bit set?)
       FileGetAttrib,  abit, %SourceName%\%filename%
       IfInString, abit, A
         {
          ; Check if the files are the same (created at the same time)
          FileGetTime, SourceTime, %SourceName%\%filename%, C
          FileGetTime, TargetTime, %TargetName%\%filename%, C
          IfEqual,  SourceTime, %TargetTime%
            {
             FileCopy, %SourceName%\%filename%, %TargetName%\%filename%, 1
             IfNotEqual, Errorlevel, 0
               {
                FileAppend,  Not Copied %SourceName%\%filename%`n,  c:\sbprob.txt
                Continue
               }
             FileGetTime, stime, %SourceName%\%filename%, C
             FileSetTime, %stime%, %TargetName%\%filename%, C, 0, 0
              FileSetAttrib,  -A, %SourceName%\%filename%, 0, 0
             GuiControl, , WorkArea, Backup Running...`nCopying %SourceName%\%filename% to %TargetName%\%filename%
             Continue
            }
          ; Check to see if this filename includes an extension
          StringTrimRight, filenametemp, filename, 3
          StringRight, LastChar, filenametemp, 1
          IfEqual,  LastChar, .
            {
             StringLen, len1, filename
             len1 :=len1-4
             StringTrimLeft, fext, filename, %len1%
             StringTrimRight, fname, filename, 4
             FileMove,  %SourceName%\%filename%, %SourceName%\%fname%%A_DD%%A_MM%%A_YYYY%%fext%, 1
             FileCopy,  %SourceName%\%fname%%A_DD%%A_MM%%A_YYYY%%fext%, %TargetName%\%fname%%A_DD%%A_MM%%A_YYYY%%fext%
             IfNotEqual, Errorlevel, 0
               {
                FileAppend,  Not Copied %SourceName%\%fname%%A_DD%%A_MM%%A_YYYY%%fext%`n,  c:\sbprob.txt
                Continue
               }
             FileGetTime, stime, %SourceName%\%fname%%A_DD%%A_MM%%A_YYYY%%fext%, C
             FileSetTime, %stime%, %TargetName%\%fname%%A_DD%%A_MM%%A_YYYY%%fext%, C, 0, 0
             FileSetAttrib,  -A, %SourceName%\%fname%%A_DD%%A_MM%%A_YYYY%%fext%, 0, 0
             GuiControl, , WorkArea, Backup Running...`nCopying %SourceName%\%fname%%A_DD%%A_MM%%A_YYYY%%fext% to %TargetName%\%fname%%A_DD%%A_MM%%A_YYYY%%fext%
             Continue
            }
          FileMove,  %SourceName%\%filename%, %SourceName%\%filename%%A_DD%%A_MM%%A_YYYY%
          FileCopy,  %SourceName%\%filename%%A_DD%%A_MM%%A_YYYY%, %TargetName%\%filename%%A_DD%%A_MM%%A_YYYY%
          IfNotEqual, Errorlevel, 0
            {
             FileAppend,  Not Copied %SourceName%\%fname%%A_DD%%A_MM%%A_YYYY%`n,  c:\sbprob.txt
             Continue
            }
          FileGetTime, stime, %SourceName%\%fname%%A_DD%%A_MM%%A_YYYY%, C
          FileSetTime, %stime%, %TargetName%\%fname%%A_DD%%A_MM%%A_YYYY%, C, 0, 0
          FileSetAttrib,  -A, %SourceName%\%filename%%A_DD%%A_MM%%A_YYYY%, 0, 0
          GuiControl, , WorkArea, Backup Running...`nCopying %SourceName%\%fname%%A_DD%%A_MM%%A_YYYY% to %TargetName%\%fname%%A_DD%%A_MM%%A_YYYY%
         }


      }

   IfExist,  c:\sbprob.txt
      {
       MsgBox, 0, Problems Occured, Not all files were copied succefully`n`nYou will now see a notepad window displaying problem files`n`nYou must close this window to continue.
       RunWait,  notepad c:\sbprob.txt, ,Max   
      }

   SetControls( "enable" ) 
   
   GuiControl, , WorkArea, Ready!
   Return
   
   
   }

GuiClose:
ExitApp

WARNING:
GuiControlGet, Replace, ,Replace
IfEqual, Replace, 1
   {
    MsgBox, 0, Beware, This option should only usually be used for your first backup using this product.`nUsing this option at any other time could cause permenant loss of data!
   }
Return

SetControls( state ) 
	{
   	 GuiControl, %state%, btnsource
    	 GuiControl, %state%, btntarget
   	 GuiControl, %state%, btnbackup
   	 GuiControl, %state%, btnclose
	} 



#2 majkinetor

majkinetor
  • Fellows
  • 4511 posts

Posted 01 February 2007 - 12:27 PM

It doesn work - Menu doesn't exist.

#3 shane

shane
  • Members
  • 8 posts

Posted 01 February 2007 - 12:58 PM

Apologies, I thought I had taken the menu out completely. Edited should work now.

#4 majkinetor

majkinetor
  • Fellows
  • 4511 posts

Posted 01 February 2007 - 01:57 PM

Set of commands on several places

GuiControl, Disable, btnsource
GuiControl, Disable, btntarget
GuiControl, Disable, btnbackup
GuiControl, Disable, btnclose

should be replaced by

SetControls( state ) {
    GuiControl, %state%, btnsource 
    GuiControl, %state%, btntarget 
    GuiControl, %state%, btnbackup 
    GuiControl, %state%, btnclose 

}
Then you can say instead

SOURCESELECT: 
   { 
    ; Select a Source folder - Button on Gui 
    GuiControl, Disable, btnsource 
    GuiControl, Disable, btntarget 
    GuiControl, Disable, btnbackup 
    GuiControl, Disable, btnclose 
    GuiControlGet, Tempsource,,edtsource 
    FileSelectFolder, SourceName, *%Tempsource% , 3, Select a Source Folder for your backup.    
    Sleep, 250 
    Tempsource= 
    GuiControl, Enable, btnsource 
    GuiControl, Enable, btntarget 
    GuiControl, Enable, btnbackup 
    GuiControl, Enable, btnclose 
    GuiControl, , edtsource, %SourceName% 
    Return 
   }

SOURCESELECT: 
   { 
    ; Select a Source folder - Button on Gui 
    SetControls( "disable" )
    GuiControlGet, Tempsource,,edtsource 
    FileSelectFolder, SourceName, *%Tempsource% , 3, Select a Source Folder for your backup.    
    Sleep, 250 
   
    SetControls( "enable" ),  Tempsource := ""
    GuiControl, , edtsource, %SourceName% 
    Return 
   }


#5 shane

shane
  • Members
  • 8 posts

Posted 01 February 2007 - 02:16 PM

Cheers for that, didn't know you could create your own routines and call them like that.

I have updated the code.

Thanks :D

#6 Laszlo

Laszlo
  • Fellows
  • 4713 posts

Posted 01 February 2007 - 09:55 PM

Here is an alternative script, which you could tweak to your liking.

#7 Johny

Johny
  • Members
  • 20 posts

Posted 09 February 2007 - 03:37 AM

Hi

You can use some code from this script also if you want to copy your files and compress them in a zip file. WinZip, IzArc or a similar application is required.

I use this for my daily backup.

#8 corrupt-NLI

corrupt-NLI
  • Guests

Posted 10 February 2007 - 03:17 AM

Here's another one :) .

#9 bruno

bruno
  • Members
  • 161 posts

Posted 08 March 2011 - 05:54 AM

Here's another one :) .

I use a small program called syncdir by Kyle A. York to synchronize files and directories for backup purposes. However, I recently discovered all features work fine except it ignores files with identical content and time stamp but different filename capitalization. If filename is the same (and content and time stamp is also the same) but capitalization (uppercase/lowercase) is different, files are still considered IDENTICAL! I am wondering if this is also the case with your script: Generic Timed File Backup Script, v0.05 Beta - b6210701? :)

#10 MacroMan!

MacroMan!
  • Members
  • 604 posts

Posted 08 March 2011 - 02:55 PM

except it ignores files with identical content and time stamp but different filename capitalization.


This is a windows quirk and not program specific.

If you try and rename a file to the same name as another with different capitalization, even windows thinks it's the same name.

David

#11 bruno

bruno
  • Members
  • 161 posts

Posted 08 March 2011 - 08:53 PM

except it ignores files with identical content and time stamp but different filename capitalization.


This is a windows quirk and not program specific.

If you try and rename a file to the same name as another with different capitalization, even windows thinks it's the same name.

David

Thanks! This was baffling me for a while... Is there any innovative way (adding code, etc.) to implement changes in the upper/lower- cases in the file & folder names?

#12 MacroMan!

MacroMan!
  • Members
  • 604 posts

Posted 08 March 2011 - 09:58 PM

Why would you want to? Most (pretty much all windows) programs will ignore case so if you tried to start differentiating on case, you would run into a lot of problems with windows quirks. My advice, just call the file something else, or have a number at the end that can be incremented or something and you'll save your self a lot of time in the long run.

David

#13 bruno

bruno
  • Members
  • 161 posts

Posted 10 March 2011 - 04:17 AM

So this is a shame! Since I am changing a bunch of song titles to be case consistent. For example: "This Is The Song I Like.mp3" rather than "This Is the Song I Like.mp3" or "this is the song i like.mp3" ... etc. And none of them will back up! :(

#14 MacroMan!

MacroMan!
  • Members
  • 604 posts

Posted 11 March 2011 - 12:09 AM

Why don't you just remove the backuped up files? Then the new files with the changed case will backup correctly.

David

#15 bhawk2

bhawk2
  • Guests

Posted 11 March 2011 - 03:13 PM

Hi Shane, great util
is it possible to save multi source and target path?
so the next time the user open this util he can select from a list the source and the target path

Thanks,
Bhawk2