-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
get-contentFast is opening files read-write #1
Comments
Interesting, I look forward to your pull request! Could you include before and after times on a really big text file? Maybe 10,000 lines or something? I think that would be really interesting to see. |
Interesting read on MSDN (what I can make of it). It's all about being a reader, but then lower down mentions it can use the write method. Sheesh, make up your mind! :)
|
Hi Martin
I downloaded Get-ContentFast, but had problems getting it to run.
So below are a few lines copied from your code that show the error you'll
get if you use get-ContentFast.ps1 on a file that is open for writing, like
the log file I tested with.
It takes less than 0.1s to read 200,000 lines from a file of 9.5MB
Regards, John
*Execution output:*
.\Get-ContentFastTest.ps1
$readOnly is False
New-Object : Exception calling ".ctor" with "1" argument(s): "The process
cannot access the file 'C:\Program Files
(x86)\StorageCraft\ImageManager\Logs\ftp[pmrrep.advantage.co.nz%gls1]-4.log'
because it is being used by another
process."
At C:\temp\Get-ContentFastTest.ps1:14 char:11
+ $File = New-Object System.IO.StreamReader -Argument $PathName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object],
MethodInvocationException
+ FullyQualifiedErrorId :
ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
.\Get-ContentFastTest.ps1 -readonly
$readOnly is True
204686 lines read
Elapsed time: 00:00:00.0986366
*Get-ContentFastTest.ps1:*
param (
[switch] $readOnly
)
$startTime = get-date
$PathName = "C:\Program Files (x86)\StorageCraft\ImageManager\Logs\ftp[
pmrrep.advantage.co.nz%gls1]-4.log"
Write-Verbose "Reading $PathName..."
$fs = [IO.File]::Open($PathName, [IO.FileMode]::Open,
[IO.FileAccess]::Read, [IO.FileShare]::ReadWrite)
"`$readOnly is $readOnly"
if ( $readOnly) {
$File = New-Object System.IO.StreamReader -Argument $fs
} else {
$File = New-Object System.IO.StreamReader -Argument $PathName
}
$lineCount = 0
While (($Line = $File.ReadLine()) -ne $null) { # Need -ne $null in case
line equates to false (empty, "0", etc)
$lineCount++
}
"$lineCount lines read"
"Elapsed time: $((get-date)-$startTime)"
…On Tue, 14 May 2019 at 22:22, MartinPugh ***@***.***> wrote:
Interesting read on MSDN (what I can make of it). It's all about being a
reader, but then lower down mentions it can use the write method. Sheesh,
make up your mind! :)
StreamReader is designed for character input in a particular encoding, whereas the Stream class is designed for byte input and output. Use StreamReader for reading lines of information from a standard text file.
The Read(Char[], Int32, Int32) and Write(Char[], Int32, Int32) method overloads read and write the number of characters specified by the count parameter. These are to be distinguished from BufferedStream.Read and BufferedStream.Write, which read and write the number of bytes specified by the count parameter. Use the BufferedStream methods only for reading and writing an integral number of byte array elements.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#1?email_source=notifications&email_token=AF47FEJWMOGDCVWRB7NPRETPVKHIFA5CNFSM4HMUEQN2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVLBB5I#issuecomment-492179701>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AF47FENELDCCA7J77PFDCVTPVKHIFANCNFSM4HMUEQNQ>
.
|
Nice work. Do you know how contribute your code to the repository? |
I'm pretty sure that:
$File = New-Object System.IO.StreamReader -Argument $PathName
opens the file read-write, which will fail if another process has the file open, not allowing writing.
This should open the file readonly as long as reading is allowed by the process that has the file open:
$fs = [IO.File]::Open($PathName, [IO.FileMode]::Open, [IO.FileAccess]::Read, [IO.FileShare]::ReadWrite)
$File = New-Object System.IO.StreamReader -Argument $fs
The text was updated successfully, but these errors were encountered: