Results 1 to 7 of 7

Thread: vb script to backup important directories

  1. #1
    Good friend of the forums
    Join Date
    Jan 2010
    Location
    outside chicago, il
    Posts
    442

    Default vb script to backup important directories

    I wrote this vbscript for a friend of mine to backup important directories to another hard drive. I figured someone else might enjoy it so here it is. The MyTimeStamp was written by Rubens Almeida and I tweaked it so it uses month, day, and 4 digit year instead of day, month, and 2 digit year.

    Update1 The destination folder now contains the source folders. I.E. source=c:\test Dest=D:\backup, After backup D:\backup\test\

    Update2 Add the time to the destination folder if the user wants it.


    Code:
    Option Explicit
    
    ' Change DestDrv to the drive letter of the external passport drive
    ' Add or subtract sourcefolders and change the sourcefolders(2) to the correct number
    Const DestDrv = "C:\"
    Const DateAndTime = False ' Change to True if you want the date and time in the destination folder
    Dim SourceFolders(2)
    SourceFolders(0) = "C:\test\test2"
    SourceFolders(1) = "C:\test2"
    SourceFolders(2) = "C:\test3\test4"
    
    Dim fso
    Dim DestDrvObj, DestDrvFreespc, DestDrvFreespcAfter
    Dim DestDirectory, objFolder
    Dim Logg, logName
    Dim indx1
    
      Set fso = CreateObject("Scripting.FileSystemObject")
      Set DestDrvObj = fso.GetDrive(DestDrv)
      DestDrvFreespc = DestDrvObj.FreeSpace
      DestDirectory = DestDrv & MyTimeStamp(Date())
    
      If fso.FolderExists(DestDirectory) Then
        msgbox("The destination folder already exists.")
        WScript.quit
      Else
        Set objFolder = fso.CreateFolder(DestDirectory)
        logName = DestDirectory & "\log.txt"
      End If
    
      Set logg = fso.OpenTextFile(logName, 8, True)
      logg.WriteLine "Backup started at " & Now() & vbNewLine
      For indx1 = 0 to UBound(SourceFolders, 1)
        copyme SourceFolders(indx1), DestDirectory
      Next
      logg.WriteLine "Backup completed at " & Now() & vbNewLine
      DestDrvFreespcAfter = DestDrvObj.FreeSpace
      logg.WriteLine "Output Drive Freespace: Before " & FormatNumber(DestDrvFreespc/1048576, 1) &_
      " GB After: " & FormatNumber(DestDrvFreespcAfter/1048576, 1) & " GB"
    
      Set logg = Nothing
      Set objFolder = Nothing
      Set DestDrvObj = Nothing
      Set fso = Nothing
      Wscript.quit
    
    function MyTimeStamp(curTime)
    dim myTime, myDay, myMonth, myYear
    dim myHour, myMin
    
      myTime = Now()
      myDay = Datepart("d", myTime)
      if Len(myDay) = 1 then myDay = "0" & myDay
      myMonth = DatePart("m", myTime)
      if Len(myMonth) = 1 then myMonth = "0" & myMonth
      myYear = DatePart("yyyy", myTime)
      'if Len(myYear) = 4 then myYear = Right(myYear,2)
      if DateAndTime = True then
        myHour = DatePart("h", myTime)
        myMin = DatePart("n", myTime)    
    wscript.echo myMin
    wscript.echo myTime
    
        if Len(myHour) = 1 then myHour = "0" & myHour
        if Len(myMin) = 1 then myMin = "0" & myMin
        myTimeStamp = myMonth & myDay & myYear & "_" & myHour & "-" & myMin
      else
        myTimeStamp = myMonth & myDay & myYear
      end if
      myTimeStamp = Cstr(myTimeStamp)
    End function
    
    Sub copyme(Src, Dest)
    Dim thisfolder, item, Spath, Dpath
    
      Set thisfolder = fso.GetFolder(Src)
      If Not fso.FolderExists(Dest & "\" & thisfolder.Name) then fso.CreateFolder(Dest & "\" & thisfolder.Name)
    
      For each item in thisfolder.Files
        Spath = fso.BuildPath(Src, item.name)
    '    On error resume next
         fso.CopyFile Spath, Dest & "\" & thisfolder.Name & "\" & item.Name
    
        If Err.Number <> 0 then Wscript.echo "Error: " & Err.Number & " on " & Spath
        on Error Goto 0
      Next
    
      For each item in thisfolder.SubFolders
        Dpath = fso.BuildPath(Dest, thisfolder.Name)
        copyme item.Path, Dpath
      Next
      Set thisfolder=Nothing
    End Sub
    I like the bleeding edge, but I don't like blood loss

  2. #2
    Junior Member NaZirCon's Avatar
    Join Date
    Sep 2007
    Posts
    71

    Default

    It does what it says. But you may consider building the original folder structure to the DestDirectory as well, because this "copyme" function puts all the files in the same destination directory, regardless of folder structure.
    Beer is served only to members of the trade union!

  3. #3
    Good friend of the forums
    Join Date
    Jan 2010
    Location
    outside chicago, il
    Posts
    442

    Default

    Sources:
    C:\test
    C:\test\test4
    C:\test\test4\New Folder
    C:\test\test4\New Wave Sound.wav

    C:\test2
    C:\test2\New Microsoft Office Access Application.mdb

    C:\test3
    C:\test3\test4
    C:\test3\test4\New Folder
    C:\test3\test4\New Microsoft Word Document.doc
    C:\test3\test4\New Folder\New Text Document.txt

    Dest:
    C:\11222008
    C:\11222008\log.txt
    C:\11222008\New Folder
    C:\11222008\New Microsoft Office Access Application.mdb
    C:\11222008\New Microsoft Word Document.doc
    C:\11222008\test4
    C:\11222008\New Folder
    C:\11222008\New Folder\New Text Document.txt
    C:\11222008\test4
    C:\11222008\test4\New Folder
    C:\11222008\test4\New Wave Sound.wav

    I just found a bug. New Text Document.txt is in the wrong spot. I seem to have a problem with subfolders of the same name. My tests last night didn't have any subfolders with the same names.

    Did you want Dest to look like this?:
    C:\11222008\test
    C:\11222008\test2
    C:\11222008\test3
    etc,etc

    That might solve my problem. I will continue to work on this.
    Thanks,
    I like the bleeding edge, but I don't like blood loss

  4. #4
    Junior Member NaZirCon's Avatar
    Join Date
    Sep 2007
    Posts
    71

    Default

    Actually, what i wanted to say is that if you have the original folder structure like this :
    Code:
    C:\test\test2\test3
    , you must replicate this structure to the destination directory. Then it would look like this :
    Code:
    D:\11222008\test\test2\test3
    In other words, you have to create folders in the destination directory first, and then copy the files. If not, you'll have a big mess
    Beer is served only to members of the trade union!

  5. #5
    Good friend of the forums
    Join Date
    Jan 2010
    Location
    outside chicago, il
    Posts
    442

    Default

    Quote Originally Posted by NaZirCon View Post
    Actually, what i wanted to say is that if you have the original folder structure like this :
    Code:
    C:\test\test2\test3
    , you must replicate this structure to the destination directory. Then it would look like this :
    Code:
    D:\11222008\test\test2\test3
    In other words, you have to create folders in the destination directory first, and then copy the files. If not, you'll have a big mess
    Hmm,

    The code I just uploaded does this:
    Code:
    directory struct
    C:\test\test2\test3\test4
    
    src = c:\test
    dest = d:\11222008\test\test2\test3\test4
    
    src = c:\test\test2
    dest = C:\11222008\test2\test3\test4
    and so on.
    That should be good enough to avoid problems, don't you think?
    I like the bleeding edge, but I don't like blood loss

  6. #6
    Junior Member NaZirCon's Avatar
    Join Date
    Sep 2007
    Posts
    71

    Default

    Oh, yes...now it does I didn't see the update.
    And one more thing : if you do more than one backup a day, you may add the time , as a prefix to a date:

    Code:
    D:\02_21_45_11222008\...
    Beer is served only to members of the trade union!

  7. #7
    Good friend of the forums
    Join Date
    Jan 2010
    Location
    outside chicago, il
    Posts
    442

    Default

    Quote Originally Posted by NaZirCon View Post
    Oh, yes...now it does I didn't see the update.
    And one more thing : if you do more than one backup a day, you may add the time , as a prefix to a date:

    Code:
    D:\02_21_45_11222008\...
    Yeah I was thinking about that too. I will look into that tomorrow.

    Thanks for the good ideas.
    I like the bleeding edge, but I don't like blood loss

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •