Create a log file, every N lines.

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Cristhian Vic. Cristhian Vic. 2 days, 11 hours ago.

  • Author
    Posts
  • #73333
    Profile photo of Cristhian Vic.
    Cristhian Vic.
    Participant

    Hi.

    I'm creating a log file, but i want to make a file every 1,000 lines, named with the day's date. And also want to parse on those logs.

    Example:
    01.06.2017.log
    02.06.2017.log

    And after that, search in each file the field required.

    Any ideas?
    Here's what i got:

    $Saved = Get-Content C:\CC\Saved.txt
    $LogRepeated = "C:\CC\LogRepeated.txt"
    $LogSaved = "C:\CC\Saved.txt"
    $uuid = "1234"
    
    
      If ($Saved -contains $uuid)
      {
        Write-Output "File Already exist."
        Write-Output ($uuid) | Out-File -FilePath $LogRepeated -Encoding ASCII -Append
        Break;    
      }
      
      Else
      {
       #XML Generator Code
       Write-Output "File added."
       Write-Output ($uuid) | Out-File -FilePath $LogSaved -Encoding ASCII -Append      
      }
  • #73337
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    You could "outsource" these two requirements in two seperate functions:

    Meta code!!
    1st function: Check in all existing files if the UUID is already there -> Output "Boolian" (True or False)
    2nd function: Check the last/newest file if it has already 1000 lines
    	- if not -> add the given UUID 
    	- if yes -> create a new file and add the given UUID
  • #73376
    Profile photo of Cristhian Vic.
    Cristhian Vic.
    Participant

    This is what i get, it works fine but i have an issue.

    After create the first file, the rest only save one field.

    File 1.txt – Good. Save all fields.
    File 2.txt – Only save one field.
    File 3.txt – Only save one field.
    Etc.

    $date = Get-Date -Format "dd-MM-yyyy"
      If (!(Test-path C:\CC\Logs_Guardados\"Guardados - $date".txt))
      {
    		write-Output ("# Folios Guardados #") | Out-File -FilePath C:\CC\Logs_Guardados\"Guardados - $date".txt -Encoding ASCII -Append				
      }
     
    $CFDIsGuardados = Get-Content C:\CC\Logs_Guardados\"Guardados - $date".txt
    $LogRepetidos = "C:\CC\Logs_Repetidos\Repetidos - $date.log"
    $LogGuardados = "C:\CC\Logs_Guardados\Guardados - $date.txt"
    $fecha = Get-Date -Format G
    $folio = "3333 "
    
      If (Select-String -Path C:\CC\Logs_Guardados\*.txt -Pattern $folio)
      {
        Write-Output "El CFDI ya existe."
        Write-Output ($folio + " - " + $fecha) | Out-File -FilePath $LogRepetidos -Encoding ASCII -Append
      }
      
      Else
      {
         If($CFDIsGuardados.Count -le 2)
         {
          Write-Output "CFDI agregado."
          Write-Output ($folio + " - " + $fecha) | Out-File -FilePath $LogGuardados -Encoding ASCII -Append      
         }
         Else
         {
          $fileN = Get-ChildItem C:\CC\Logs_Guardados -Recurse -File | Measure-Object | %{$_.Count}
          $fileN = $fileN+1
          $LogGuardados = "C:\CC\Logs_Guardados\Guardados - $date - $fileN.txt"
          Write-Output ($folio + " - " + $fecha) | Out-File -FilePath $LogGuardados -Encoding ASCII -Append
          Write-Output "CFDI agregado."
         }
    
      }
    

You must be logged in to reply to this topic.