This project is read-only.

Simple Archive System

Dec 20, 2008 at 2:30 PM

tx for this great zip library. I want to use it for a weekly archive system but I a stuck on something and I do not know where to look for the answer. I think it has something to do with files in use but I tried putting a pause in the program (as a test) but it did not work either.

My code is pretty simple, viz. retrieving all the logs in a folder and when they are more than 7 days old, putting them in a zip & deleting the original. Then when it is finished, it will be copied to an other location on the network. The problem is on the delete statement. 

Can somebody help me pls?

Here is my code:

Private Sub ArchiveFiles()

Dim strArchivePath as string
Dim iWeeknumber As Integer = DatePart(DateInterval.WeekOfYear, Now)

strArchivePath  = "c:\ARCHIVE\" & Now.Year & iWeeknumber & "\"
strZIP = strArchivePath  & ""

Dim zipArchief As New Ionic.Utils.Zip.ZipFile(strZIP)

arrBestanden = nfoDir.GetFiles("*.log")
For Each nfoBestand In arrBestanden
    If (DateDiff(DateInterval.Day, nfoBestand.LastWriteTime, Now()) > 7) And (nfoBestand.IsReadOnly = False) Then

      zipArchief.AddFile(nfoBestand.FullName, "")

    End If

end sub

Dec 20, 2008 at 3:16 PM
First, I would move the save outside of the loop. No need to call it several times.

As far as the delete statement, try to delete the file in Explorer and see if you get an error. Perhaps the file is in use (by a virus scanner, another backup app, etc.).

If you get an exception, what is the exception?
Dec 20, 2008 at 4:37 PM
Edited Dec 20, 2008 at 4:38 PM
If I were designing this, I would add all the files to a collection (a List<String>) in a loop.
Then I would For Each over the collection and create the zip file.  In the zip file I would insert a comment and a readme. I'd wrap the zipfile operations in a using statement, so I ensure the zip is closed when I am done with it.
Then in another For Each I would delete the files.
        Public Sub Run(ByVal args As String())

            Dim strZIP as string = String.Format("SledgeLogging-{0}.zip", _

            Dim LogFileDirectory as string = "c:\temp\"

            Dim current as DateTime = DateTime.Now
            Dim interval as new System.TimeSpan(7,0,0,0) ' 7 days
Dim filesToArchive as New System.Collections.Generic.List(Of String) Try ' step 1: get the list of files to archive Console.WriteLine(vbcrlf & "Finding files to archive in " & LogFileDirectory) Dim AllLogFiles As String() = System.IO.Directory.GetFiles(LogFileDirectory, "*.log") For Each filename In AllLogFiles Dim lastwrite as DateTime = System.IO.File.GetLastWriteTime(filename) Dim attr as FileAttributes = System.IO.File.GetAttributes(filename) ' older than 7 days and not ReadOnly If (current - lastwrite > interval And _ (attr & FileAttributes.ReadOnly) <> FileAttributes.ReadOnly) Then filesToArchive.Add(filename) Console.WriteLine(" " & filename) End If Next ' step 2: zip up those files in an archive Console.WriteLine(vbcrlf & "Zipping those files into " & strZIP) using zipArchief As New Ionic.Utils.Zip.ZipFile(strZIP) For Each filename in filesToArchive zipArchief.AddFile(filename, "") Next zipArchief.Comment = "Archive created " & System.DateTime.Now.ToString("G") & " on machine " & _ System.Environment.MachineName zipArchief.Save() End Using ' step 3: after we are sure the zip has succeeded, delete the files Console.WriteLine(vbcrlf & "Removing those files...") For Each filename in filesToArchive ' uncomment the following line to really delete 'System.IO.File.Delete(filename) Next Catch ex1 as Exception Console.WriteLine("Exception!" & ex1.ToString()) End Try End Sub
I don't know "old" VB, so I used the managed .NET libraries for time comparison and file information.
Dec 21, 2008 at 11:13 AM

tx to you both, it is sometimes surprising that it the current society, people still want to help each other :D

i'll give it a try and keep you posted

my code was an extract and i am happy to see that my thinking corresponds to yours. I only tried it i one step and without the using ...


Dec 22, 2008 at 10:45 AM

Tx Cheeso & Mr JohnMandre


it works now, fine & fast!

In my original programme i prefered to save before delete on every file to be sure but it works fine with the multiple loops!


Merry Xmas to all!