Search code examples
powershellfilezilla

Powershell to parse filezilla log


I need to pull the date, time and filename out of a filezilla log so I can insert them into a database.

This is what I have so far:

Select-String "C:\Scripts\testing\SSIS\testfiles\fzs-2014-04-15.log" -Pattern "STOR" -AllMatches
Foreach($line in $lines){
$line = $line -replace "C:\Path-to-logs\filezilla.log.*\) ",""
    $line = $line -replace "[AM|PM].* STOR ",""
    $line -split " "
}

I get the following results:

C:\path-to-logs\filezilla.log:114:(003173) 4/15/2014 3:04:20 AM - cwwsfge (192.168.250)> STOR NewFileupload.TXT
C:\path-to-logs\filezilla.log.log:210:(000182) 4/15/2014 6:21:21 AM - refect(192.168.250)> STOR Testfile_20140415
C:\path-to-logs\filezilla.log.log:662:(000179) 4/15/2014 6:27:13 AM - refect (192.168.2)> STOR FreeMoney.txt

So how do I get that info once I have it in the foreach???

Thanks!!


Solution

  • Editing to match your changes above. I think you can still get away with one regex -split and then picking off the elements that you need. Try this:

    Foreach($line in $lines){
    $parts = $line  -split '\) | - |\> STOR |:\('
        $logdate = [DateTime]::Parse($parts[2])
        $filename = $parts[4]
    
        write-host "Filename: $filename"
        write-host "Filedate: $($logdate.Date.ToShortDateString())"
        write-host "Filetime: $($logdate.TimeOfDay)`n"
    }
    

    Basically this is matching ") ", " - ", "> STOR " or ":(" and splitting the line across those. If the format is consistent, you should have 5 elements in each pass. I added some code to pick off the filename and parse the date and it yields this for the values in the example above:

    Filename: NewFileupload.TXT
    Filedate: 4/15/2014
    Filetime: 03:04:20
    
    Filename: Testfile_20140415
    Filedate: 4/15/2014
    Filetime: 06:21:21
    
    Filename: FreeMoney.txt
    Filedate: 4/15/2014
    Filetime: 06:27:13