MS Office Forum / Word / Programming / December 2006
re: backup to multiple folders
|
|
Thread rating:  |
jimc - 28 Dec 2006 21:31 GMT regarding my previous post which asked the correct procedure to backup to multiple locations including Flash drive and a date based (even/odd) backup folder.
I have been playing with my macro and the macro as refined by Graham Mayor and have discovered that the size of the odd/even backup folders will grow unnacceptably if not size limited. Especially in my case where it is a single manuscript length file backed up daily.
So before saving a file to a folder, is it possible to limit the number of files in the folder to the 3 newest files (total of 4, including new save)
open folder count files (should never be more than 4, if this macro has run before) delete oldest file ( or all files older that the newest 3) save new file
I am currently trying to figure this out, so no code fragment to post, if anyone has any ideas, I will post back
Charlie Hoffpauir - 28 Dec 2006 23:15 GMT >regarding my previous post which asked the correct procedure to backup >to multiple locations including Flash drive and a date based (even/odd) [quoted text clipped - 16 lines] >I am currently trying to figure this out, so no code fragment to post, >if anyone has any ideas, I will post back Have you considered using xxcopy to produce the backups? The full set of options available make it possible to do some very custom backups. You should be able to set "switches" in the xxcopy command line (in your macro) to enable all the options you want.
Charlie Hoffpauir http://freepages.genealogy.rootsweb.com/~charlieh/
jimc - 29 Dec 2006 14:36 GMT OK, after playing around with arrays and such, I decided (unless someone can show me how) that it is simply easier to time limit the files in the folder to 15 days, hopefully this
should limit the number of files to 6 or 7, if used every day. I chose a larger number than 3 files just in case I didnt use tha macro every day. Here is what I have
<snip... (dim strFileB As String 'contains complete path and file name)
'check to see if the file already exists, if not: save it, if it does: 'save it after deleting all files older than 15 days
If Dir(strFileB) > "") Then 'if it is there, resave it without checking folder for number of files. ActiveDocument.SaveAs FileName:=strFileB Else Do While strFileB <> "" 'check ages of files in folder, delete if older than 15 days If DateDiff("d", FileDateTime(strFileB), Now()) > 15 Then kill strFileB End If strFileB = Dir() Loop 'strFileB must contain complete path and filename ActiveDocument.SaveAs FileName:=strFileB End If
> regarding my previous post which asked the correct procedure to backup > to multiple locations including Flash drive and a date based (even/odd) [quoted text clipped - 16 lines] > I am currently trying to figure this out, so no code fragment to post, > if anyone has any ideas, I will post back jimc - 29 Dec 2006 19:03 GMT ahh...still cant get it to work
Dim strFileA As String Dim strFileB As String Dim strFlash As String Dim strPathOdd As String Dim strPathEven As String Dim strPath As String Dim strCurDir As String Dim dFileDate As Date Dim strExt As String
'save the original document ActiveDocument.Save Debug.Print CurDir() 'Saved original document name strFileA = ActiveDocument.Name
'Flash drive filename strFlash = "F:\" & strFileA
'Document extension strExt = "*.doc"
'Paths to odd/even folders strPathOdd = "C:\Documents and Settings\Jim.DELL5150\My Documents\MyOddBackups\" strPathEven = "C:\Documents and Settings\Jim.DELL5150\My Documents\MyEvenBackups\"
' Assign Backup to different folder every other day If Even = (Day(Now) Mod 2) - 1 Then strFileB = strPathOdd & (Format$(Date, "MMM d ")) & strFileA Debug.Print strFileB strPath = strPathOdd Else strFileB = strPathEven & (Format$(Date, "MMM d ")) & strFileA strPath = strPathEven End If strCurDir = CurDir
'check to see if the file already exists, if not: save it, if it does: 'save it after deleting all files older than 15 days
If (Dir(strFileB) > "") Then 'if it is there, resave it ActiveDocument.SaveAs FileName:=strFileB
Else ChDir (strPath) sfile = Dir(CurDir() & "\" & strExt) 'get name of first file in path wit .doc as extension Debug.Print CurDir Debug.Print sfile Do Until Len(sfile) = 0 If DateDiff("d", Date, FileDateTime(sfile)) > 24 Then Kill (strPath & sfile) End If Debug.Print DateDiff("d", Date, FileDateTime(sfile)) 'equals 0 sfile = Dir() Loop
ActiveDocument.SaveAs FileName:=strFileB End If '*************************************************************************** 'Close the document ActiveDocument.Close
Russ - 29 Dec 2006 23:31 GMT Jimc,
What doesn't work? Does the DateDiff function always = 0?
Speaking of being explicit... ;-) Do you always use the 'Option Explicit' statement in your main declaration module to cover all subroutines for missed variable declarations? ...because 'sfile' variable does not seem to be declared below in your code.
> ahh...still cant get it to work > [quoted text clipped - 63 lines] > 'Close the document > ActiveDocument.Close
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
jimc - 30 Dec 2006 20:49 GMT > Jimc, > > What doesn't work? Russ, Thank you very much for your reply. I had myself in a iterative loop (my brain, not the code). I got it going this morning after putting it down over night. Upon running the macro, I resave the file the file if already saved, in both the current directory and on the Flash drive. (for incremental saves) If it hasnt been saved yet, I save a copy in either the Odd day backup folder or the even day folder after removing all of the old files(so that the backup folders dont just grow out of control). If you have any ideas, let me know...this is my first attempt at VBA.
Sub newBackup() 'newBackup Macro
Dim strFileA As String Dim strFileB As String Dim strFlash As String Dim strPathOdd As String Dim strPathEven As String Dim strPath As String Dim strCurDir As String Dim dFileDate As Date Dim strExt As String Dim sFile As String
'save the original document ActiveDocument.Save 'Save original document name strFileA = ActiveDocument.Name
'Flash drive filename, just keep filename, overwrite whats there from previous backups strFlash = "F:\" & strFileA
'Paths to odd/even folders strPathOdd = "C:\Documents and Settings\Jim.DELL5150\My Documents\MyOddBackups\" strPathEven = "C:\Documents and Settings\Jim.DELL5150\My Documents\MyEvenBackups\"
' Assign Backup to different folder every other day If Even = (Day(Now) Mod 2) - 1 Then strFileB = strPathOdd & (Format$(Date, "MMM d ")) & strFileA Debug.Print strFileB strPath = strPathOdd Else strFileB = strPathEven & (Format$(Date, "MMM d ")) & strFileA strPath = strPathEven End If 'save current directory, Dir command munges it strCurDir = CurDir
'check to see if the file already exists, if not: 'save it after deleting all files older than 48 hours 'if it does, resave it immediately.
'Document extension, make sure we delete only .doc files strExt = "*.doc"
If (Dir(strFileB) > "") Then ActiveDocument.SaveAs FileName:=strFileB
Else 'go to backup directory, delete old *.doc files so that folder doesnt get too big ChDir (strPath) sFile = Dir(CurDir() & "\" & strExt) Do Until Len(sFile) = 0 If DateDiff("h", FileDateTime(sFile), Date) > 48 Then Kill (strPath & sFile) End If sFile = Dir() Loop
'do save of even/odd day backup ActiveDocument.SaveAs FileName:=strFileB ChDir (strCurDir) End If
'Close the document ActiveDocument.Close
'Error handler for missing flash drive On Error GoTo oops
'Copy source document to flash drive FileCopy strFileA, strFlash
'reopen source document Documents.Open strFileA
End
oops: If Err.Number = 61 Then MsgBox "USB Flash Drive is Full! Delete some stuff to make room!", vbExclamation Kill strFlash 'Remove the partial file Else MsgBox "The USB flash drive doesnt seem to be plugged in!", vbExclamation End If
'reopen source document Documents.Open strFileA
End Sub
Russ - 31 Dec 2006 01:45 GMT Jimc, I'm still learning VBA, too. But, I'd say that if your code is doing what you want then you are finished, because it looks 'tight' to me. Good work. You must have programmed in other programming languages before, because the code seems to follow proper technique and is well commented, except for indenting, i.e., some if...else...end if clauses, to make things easier to read. If you use the edit toolbar in the VBA Editor, then it is easy to select lines of code and control their indentation or 'commentation'. See http://word.mvps.org/faqs/macrosvba/MaintainableCode.htm for a place to start learning more.
> If you have any ideas, let me know...this is my first > attempt at VBA.
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
|
|
|