0

I have a couple of log files (.txt) that are generated from an application that add new lines of text like these every second:

2/20/2016 1:22:47 PM 67.8
2/20/2016 1:22:48 PM 67.8 
2/20/2016 1:22:49 PM 67.7
2/20/2016 1:22:50 PM 67.6

My problem is that I cannot access the file using Get-Content because it throws an IO.Exception error. I cannot end the application because it is monitoring and cannot be stopped. I should also mention that the app also writes to another log file every 10 minutes but I can parse that log file perfectly fine. I can even open the active 10 minute log file in Notepad with no issues. I'm guessing with the other log updating every second it has a lock on the file.

Here is my parsing code for the 1 second log file:

$Log = Get-ChildItem -Path 'C:\Users\Public\App' | Where-Object {$_.Name -match "LOG"} | Sort LastWriteTime | Select -Last 1
$Number = Get-Content -Path "C:\Users\Public\App\$Log" -Tail 1 | Select-String -Pattern $regexSPL -AllMatches | % { $_.Matches } | % { $_.Groups }

Here is the error I receive when trying to parse the active 1 second log file:

Get-Content : The process cannot access the file 'C:\Users\Public\App\LOG_3_18_2016_12.26.30 AM.txt' 
because it is being used by another process.

Any help will be sincerely appreciated.

  • 1
    Are you able to open the "locked" file in notepad? Are you able to copy it to another location, and work on the copy? – G42 Mar 18 '16 at 13:17
  • I cannot open nor copy the file unfortunately. – KILLADELPHIA Mar 18 '16 at 13:40
  • I'm stumped; no idea where to go from there. [This](http://stackoverflow.com/questions/4400517/how-can-i-read-a-file-even-when-getting-an-in-use-by-another-process-exception) and [this](http://stackoverflow.com/questions/26741191/ioexception-the-process-cannot-access-the-file-file-path-because-it-is-being) are c# solutions to the problem; they use the .NET library which is also accessible to PowerShell. **Standard disclaimer:** don't test on production monitoring systems. Good luck! – G42 Mar 22 '16 at 09:15

1 Answers1

0

I'm afraid this isn't possible if the file is being read and wrote to that often it will have a permanent lock on it, the only way to access it would be to stop the process that's using it.

Dewi Jones
  • 785
  • 1
  • 6
  • 18