我在使用以下命令时遇到一些问题:
Get-Content -Encoding Byte -ReadCount 0 $FilePath_All, $FilePath_Int | Set-Content -Encoding Byte -Force $ImpDayFPath
文件大小在200-300MB之间。
我得到了一个OutOfMemoryException。机器有8 4Gb空间,但在仅使用4 4Gb空间时出现错误。
我检查了Documentation
-ReadCount<Int64>
Specifies how many lines of content are sent through the pipeline at a time.
The default value is 1. A value of 0 (zero) sends all of the content at one time.
This parameter does not change the content displayed, but it does affect the time
it takes to display the content. As the value of ReadCount increases, the time
it takes to return the first line increases, but the total time for the operation
decreases. This can make a perceptible difference in very large items.
我只是写了0,因为我找不到具有基准测试性能的示例……
这些文件或多或少包含270k行。一个接一个地走可能会造成性能问题,因为我还有一个外部循环,时间将是O(n^2)
你知道我能用到的价值吗?
发布于 2014-10-15 19:33:46
如果您正在读取二进制数据,我不确定您是否可以根据我在官方文档中找到的内容,使用Get-Content cmdlet实际更改流经管道的行数。下面是我在TechNet here上找到的相关段落
“在读取和写入二进制文件时,请使用值Byte值作为Encoding动态参数,使用值0作为ReadCount参数。ReadCount值0将在一次读取操作中读取整个文件并将其转换为单个对象(PSObject)。默认的cmdlet值1将在每次读取操作中读取一个字节并将每个字节转换为单独的对象,这会在您使用Set-Content ReadCount将字节写入文件时导致错误。有关详细信息,请参阅示例。”
但是,使用这里的BinaryReader类http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx可以读入大文件(我假设是二进制格式)。或者对于巨大的二进制文件,使用UnmanagedMemoryStream .Net类,它不会限制所使用的内存量。详细信息可以在这里找到...http://msdn.microsoft.com/en-us/library/System.IO.UnmanagedMemoryStream(v=vs.110).aspx
发布于 2014-10-15 19:42:05
Compare-Object可用于比较文件的内容。考虑以下代码:
Compare-Object -ReferenceObject $(Get-Content .\file1.txt) -DifferenceObject $(Get-Content .\file2.txt)
默认行为是仅显示文件之间的差异。了解了这一点,我们可以像这样测试差异:
if (-not(Compare-Object -ReferenceObject $(Get-Content .\file1.txt) -DifferenceObject $(Get-Content .\file2.txt))) {
Write-Output 'Files are equal'
}
else {
Write-Output 'Files are not equal'
}
https://stackoverflow.com/questions/26379965
复制相似问题