How to truncate a .log file but retain present day logs

Welcome Forums General PowerShell Q&A How to truncate a .log file but retain present day logs

Viewing 2 reply threads
  • Author
    Posts
    • #228508
      Participant
      Topics: 1
      Replies: 0
      Points: 13
      Rank: Member

      I’m a newbie in terms of PowerShell. My problem is how I can truncate or remove -1 day logs and pass it to a new backup text file.

      First Attempt (fail) – because I can’t retain the source log with present-day logs in it or clear it.

      Second Attempt – I’m still learning how trim works but it failed too, I’m trying to look for ways to put it to a file.

      Sample data in the log, first characters are number line space with date and time stamp. Example:

      1: 14-May-2020 18:00:00.445: SYSTEM:[main] Logging started

      2: 14-May-2020 18:04:00.445: DEBUG [main]…..

       

       

      Hope someone can enlighten me, thank you in advance. Appreciate your help for a noob like me 🙂

       

       

    • #228514
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      Kernel, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

      When you post code, error messages, sample data or console output format it as code, please.
      In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
      Thanks in advance.

      When you read a log file with Get-Content you get strings. They don’t have a property LastWriteTime you could check. So you have to parse the strings and extract the string representation of the timestamps. Then you can convert this to type [DateTime] to be able to check if it meets the conditions you want.

      Here is an example of how you could start:

    • #228517
      Participant
      Topics: 8
      Replies: 562
      Points: 2,149
      Helping Hand
      Rank: Community Hero

      Kernel, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

      First, you are trying to access a property that doesn’t exist. There is no attribute for those lines to say when they were written. You can check for certain properties or all properties.

      Luckily you have a date/time stamp, so you could check that after you extract it. Unfortunately, you are breaking sections up with : so not the best choice to split with. You should be able to reconstruct your datetime object like this

      Now you can compare and output the appropriate lines. Note, you want to use -gt to get the entries that are within the last day, -lt shows you the older entries. You could achieve both goals like this.

      This will take the lines for the last 24 hours, stick them in “D:\TempOrder\ServerLog_$(get-date -f yyyy-MM-dd).txt”, and remove them from $oldlog. You may need to reverse the order of output if it adds the content back in the wrong order. Hope this helps.

      Edit: Based on your title, I think I got it backwards. If so, just switch -gt to -lt and the reverse will happen. Any logs older than the time specified will go to $oldlog and the rest will go to serverlog.

      • This reply was modified 4 months, 1 week ago by Doug Maurer.
Viewing 2 reply threads
  • The topic ‘How to truncate a .log file but retain present day logs’ is closed to new replies.