Hi, is it possible to read lines from text file from the end to the beginning?
Or is it possible WriteLine to specific row number?
I need read big files, so I need use StreamReader in reverse or StreamWriter to write line into second row.
Thanks
Technology Tips and News
Hi, is it possible to read lines from text file from the end to the beginning?
Or is it possible WriteLine to specific row number?
I need read big files, so I need use StreamReader in reverse or StreamWriter to write line into second row.
Thanks
I don't believe either one is possible, but if you could explain exactly what the objective is we might be able to come up with an alternate solution.
There are other ways of handling large files in Powershell without having to resort to StreamReader or StreamWriter.
Hi Petericek,
try this one:
.Synopsis Gets last Lines of a file. .EXAMPLE Get-Tail -Path "c:\Temp\BigData.csv" #> function Get-Tail { param ( $Path, [int]$Last = 20, [int]$CharsPerLine = 500, [Switch]$follow ) $item = (Get-item $path) if (-not $item) {return} $Stream = $item.Open([System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::ReadWrite) $reader = New-Object System.IO.StreamReader($Stream) if ($charsPerLine * $last -lt $item.length) { $reader.BaseStream.seek((-1 * $last * $charsPerLine) ,[system.io.seekorigin]::End) } $reader.readtoend() -split "`n" -replace "\s+$","" | Select-Object -last $Last | write-host if ($follow) { $Global:watcher = Register-FileSystemWatcher -Path $path -MessageData $reader` -On "Changed" -Process { $event.MessageData.readtoend() -split "`n" -replace "\s+$","" | write-host } $oldConsoleSetting = [console]::TreatControlCAsInput [console]::TreatControlCAsInput = $true while ($true) { if ([console]::KeyAvailable) { $key = [system.console]::readkey($true) if (($key.modifiers -band [consolemodifiers]"control") and ($key.key -eq "C")) { write-host -ForegroundColor red "Terminating..." ; break } else { if ([int]$key.keyCHAR -eq 13) { [console]::WriteLine() } else { [console]::Write($key.keyCHAR) }} } else {Start-Sleep -Milliseconds 250} } [console]::TreatControlCAsInput = $oldConsoleSetting Unregister-Event $watcher.name } $Stream.Close() $reader.Close() }