none
How would I add the Time to the filename when while Zipping up Log Files

    Question

  • I have some code which performs Log File clean up.  But I would like to know the time when the logfile was made written into the file name at time that the log files get zipped.  I can not modify the code which actually builds the log files because this is inside of a third party object.

    The code i am interested in changing is:

     Sub LogFileClean()
                '   Proceses curent time and Zips up logfiles
                Dim LogFiles As New Zip
                Dim CurrentDay As DateTime = DateTime.Today
                Dim Tomorrow As DateTime = My.Settings.ZipTrigger
            Try
                If CurrentDay >= Tomorrow Then   '   Have advanced a day, run Zip on Logfile folder
                    Dim Direct As String = System.IO.Path.GetDirectoryName( _
                                            System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
                    Direct = Direct & "\LogFiles\LEP_Ferm_Console"
                    Dim LocalPath = New Uri(Direct).LocalPath
                    '   Set place to save the files
    
                    Dim filenames As String() = System.IO.Directory.GetFiles(LocalPath, "*.log")   '   only archive log files
                    Dim ZipToCreate As String = New Uri(Direct & "\Archives\Archive-" & DateTime.Today.ToString("yyyy-MM-dd") & ".zip").LocalPath  '   Writes log file name as yesterday
                    Dim filename As String
                    For Each filename In filenames
                        LogFiles.AddFile(filename)
                    Next
    
                    '   Write to Archive File
                    LogFiles.Save(ZipToCreate)
    
                    'Clean up
                    For Each filename In filenames
                        File.Delete(filename)
                    Next
                    My.Settings.ZipTrigger = CurrentDay.AddDays(1)   '   Assignes the last completed run as the new Last run
                    My.Settings.Save()
                    LogManager("Zipped up Log files, next run: " & My.Settings.ZipTrigger)
                Else
                    LogManager("Logfiles already Zipped")
                End If
    
             Catch ex As Exception
                PARCCommon.Logging.LogWriter.WriteException(ex)
            End Try
            End Sub

    The Zip Class which handles the process is:

    Imports System.IO
    Imports System.IO.Packaging
    Public Class Zip
       Public files As List(Of String)
    
        Public Sub New()
            files = New List(Of String)
        End Sub
    
        Public Sub AddFile(ByVal fn As String)
            files.Add(fn)
        End Sub
        Public Sub Save(ByVal filename As String)
            Try
                Dim archive As Package = ZipPackage.Open(filename, IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite)
                For Each f As String In files
                    Dim friendlyfilename As String = f.Replace(" ", "_")
                    Dim fileuri As String = "/" & IO.Path.GetFileName(friendlyfilename)
                    Dim relativeuri As New Uri(fileuri, UriKind.Relative)
                    Dim archivePart As PackagePart = archive.CreatePart(relativeuri, Net.Mime.MediaTypeNames.Application.Zip, CompressionOption.Normal)
                    Dim bites As Byte() = File.ReadAllBytes(f)
                    archivePart.GetStream().Write(bites, 0, bites.Length)
                Next
                archive.Close()
            Catch ex As Exception
                LogManager(ex.Message)
            End Try
        End Sub
    End Class
    

    Wednesday, April 19, 2017 4:31 PM

Answers

  • That is correct the Zipped File name does contain the Date in it.  However,  inside the Zipped file there are files which have file names as well.  i would like to put the time those files where written into their files names prior to zipping them into the Zipped file.   

    You will have to rename the files before adding them to the zip file. There are three date/time fields associated with a file, Created, Modified, and Accessed

    The IO namespace has methods to access these:

    IO.File.GetCreationTime(String)	
    IO.File.GetCreationTimeUtc(String)	
    IO.File.GetLastAccessTime(String)	
    IO.File.GetLastAccessTimeUtc(String)	
    IO.File.GetLastWriteTime(String)	
    IO.File.GetLastWriteTimeUtc(String)	
    

    As shown in your code, a date & time cannot be use as is for a filename due to the ":" and "/" characters, so the formatting has to be forced. The IO Namespace also has methods for renaming and getting filenames with and without extension and the extension. Those may be useful depending on how you want to name the files.

    • Marked as answer by Cam Evenson Sunday, April 23, 2017 3:19 AM
    Saturday, April 22, 2017 11:33 PM

All replies

  • Hi Cameron,

    Based on your code, I saw that you have add Date in your Zip filename, can you describe the problem you faced?

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 20, 2017 8:52 AM
    Moderator
  • That is correct the Zipped File name does contain the Date in it.  However,  inside the Zipped file there are files which have file names as well.  i would like to put the time those files where written into their files names prior to zipping them into the Zipped file.   
    Thursday, April 20, 2017 11:34 AM
  • That is correct the Zipped File name does contain the Date in it.  However,  inside the Zipped file there are files which have file names as well.  i would like to put the time those files where written into their files names prior to zipping them into the Zipped file.   

    You will have to rename the files before adding them to the zip file. There are three date/time fields associated with a file, Created, Modified, and Accessed

    The IO namespace has methods to access these:

    IO.File.GetCreationTime(String)	
    IO.File.GetCreationTimeUtc(String)	
    IO.File.GetLastAccessTime(String)	
    IO.File.GetLastAccessTimeUtc(String)	
    IO.File.GetLastWriteTime(String)	
    IO.File.GetLastWriteTimeUtc(String)	
    

    As shown in your code, a date & time cannot be use as is for a filename due to the ":" and "/" characters, so the formatting has to be forced. The IO Namespace also has methods for renaming and getting filenames with and without extension and the extension. Those may be useful depending on how you want to name the files.

    • Marked as answer by Cam Evenson Sunday, April 23, 2017 3:19 AM
    Saturday, April 22, 2017 11:33 PM