[
  {
    "Id": "142029",
    "ThreadId": "42743",
    "Html": "Hello<br>\r\n<br>\r\ntx 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.<br>\r\n<br>\r\nMy 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 &amp; 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. <br>\r\n<br>\r\nCan somebody help me pls?<br>\r\n<br>\r\n<br>\r\nHere is my code:<br>\r\n<br>\r\n<p>Private Sub ArchiveFiles()</p>\r\n<p>Dim strArchivePath as string<br>\r\nDim iWeeknumber As Integer = DatePart(DateInterval.WeekOfYear, Now)</p>\r\n<p>strArchivePath  = &quot;c:\\ARCHIVE\\&quot; &amp; Now.Year &amp; iWeeknumber &amp; &quot;\\&quot;<br>\r\nstrZIP = strArchivePath  &amp; &quot;Logging.zip&quot;</p>\r\n<p>Dim zipArchief As New Ionic.Utils.Zip.ZipFile(strZIP)</p>\r\n<p>arrBestanden = nfoDir.GetFiles(&quot;*.log&quot;)<br>\r\nFor Each nfoBestand In arrBestanden<br>\r\n    If (DateDiff(DateInterval.Day, nfoBestand.LastWriteTime, Now()) &gt; 7) And (nfoBestand.IsReadOnly = False) Then</p>\r\n<p>      zipArchief.AddFile(nfoBestand.FullName, &quot;&quot;)<br>\r\n      zipArchief.Save()<br>\r\n      nfoBestand.Delete()</p>\r\n<p>    End If<br>\r\nNext</p>\r\n<p>end sub</p>\r\n",
    "PostedDate": "2008-12-20T06:30:05.433-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "142034",
    "ThreadId": "42743",
    "Html": "First, I would move the save outside of the loop. No need to call it several times.<br>\r\n<br>\r\nAs 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.).<br>\r\n<br>\r\nIf you get an exception, what is the exception?<br>\r\n",
    "PostedDate": "2008-12-20T07:16:20.713-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "142037",
    "ThreadId": "42743",
    "Html": "If I were designing this, I would add all the files to a collection (a List&lt;String&gt;) in a loop.<br>\r\nThen 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.<br>\r\nThen in another For Each I would delete the files. <br>\r\n<pre>        Public Sub Run(ByVal args As String())\r\n\r\n            Dim strZIP as string = String.Format(&quot;SledgeLogging-{0}.zip&quot;, _\r\n                         DateTime.Now.ToString(&quot;yyyy.MM.dd.HH.mm.ss&quot;))\r\n\r\n            Dim LogFileDirectory as string = &quot;c:\\temp\\&quot;\r\n\r\n            Dim current as DateTime = DateTime.Now\r\n            Dim interval as new System.TimeSpan(7,0,0,0) ' 7 days<br>\r\n            Dim filesToArchive as New System.Collections.Generic.List(Of String)\r\n\r\n            Try\r\n                ' step 1: get the list of files to archive\r\n                Console.WriteLine(vbcrlf &amp; &quot;Finding files to archive in &quot; &amp; LogFileDirectory)\r\n\r\n                Dim AllLogFiles As String() = System.IO.Directory.GetFiles(LogFileDirectory, &quot;*.log&quot;)\r\n                For Each filename In AllLogFiles\r\n                    Dim lastwrite as DateTime = System.IO.File.GetLastWriteTime(filename)\r\n                    Dim attr as FileAttributes = System.IO.File.GetAttributes(filename)\r\n\r\n                   ' older than 7 days and not ReadOnly\r\n                    If (current - lastwrite &gt; interval  And  _\r\n                            (attr &amp; FileAttributes.ReadOnly) &lt;&gt; FileAttributes.ReadOnly) Then\r\n                      filesToArchive.Add(filename)\r\n                      Console.WriteLine(&quot;  &quot; &amp; filename)\r\n                    End If\r\n                Next\r\n\r\n                ' step 2: zip up those files in an archive\r\n                Console.WriteLine(vbcrlf &amp; &quot;Zipping those files into &quot; &amp; strZIP)\r\n                using  zipArchief As New Ionic.Utils.Zip.ZipFile(strZIP)\r\n                    For Each filename in filesToArchive\r\n                        zipArchief.AddFile(filename, &quot;&quot;)\r\n                    Next\r\n                    zipArchief.Comment = &quot;Archive created &quot; &amp; System.DateTime.Now.ToString(&quot;G&quot;) &amp; &quot; on machine &quot; &amp; _\r\n                        System.Environment.MachineName\r\n                    zipArchief.Save()\r\n                End Using \r\n         \r\n                ' step 3: after we are sure the zip has succeeded, delete the files\r\n                Console.WriteLine(vbcrlf &amp; &quot;Removing those files...&quot;)\r\n                For Each filename in filesToArchive\r\n                    ' uncomment the following line to really delete\r\n                    'System.IO.File.Delete(filename)\r\n                Next\r\n\r\n            Catch ex1 as Exception\r\n                Console.WriteLine(&quot;Exception!&quot; &amp; ex1.ToString())\r\n            End Try\r\n\r\n\r\n        End Sub\r\n</pre>\r\nI don't know &quot;old&quot; VB, so I used the managed .NET libraries for time comparison and file information. \r\n",
    "PostedDate": "2008-12-20T08:37:13.247-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "142134",
    "ThreadId": "42743",
    "Html": "<p>tx to you both, it is sometimes surprising that it the current society, people still want to help each other :D</p>\r\n<p>i'll give it a try and keep you posted</p>\r\n<p>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 ...</p>\r\n<p>mario</p>\r\n",
    "PostedDate": "2008-12-21T03:13:02.43-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "142310",
    "ThreadId": "42743",
    "Html": "<p>Tx Cheeso &amp; Mr JohnMandre</p>\r\n<p>&nbsp;</p>\r\n<p>it works now, fine &amp; fast!</p>\r\n<p>In my original programme i prefered to save before delete on every file to be sure but it works fine with the multiple loops!</p>\r\n<p>&nbsp;</p>\r\n<p>Merry Xmas to all!</p>\r\n<p>Mario</p>\r\n",
    "PostedDate": "2008-12-22T02:45:21.963-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  }
]