Seek 方法在 Powershell 脚本中不起作用
我正在尝试使用 .net API 来查找大型数据文件。由于某种原因我无法让它发挥作用。 这是我的代码:
function check_logs{
$pos = 8192
$count = 1
$path = 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\Log\ERRORLOG.2'
$br = 0
$reader = [System.IO.File]::OpenText($path)
$reader.DiscardBufferedData()
$reader.BaseStream.Seek(0, [System.IO.SeekOrigin]::Begin)
for(;;){
$line = $reader.ReadLine()
if($line -ne $null){$br = $br + [System.Text.Encoding]::UTF8.GetByteCount($line)}
if($line -eq $null -and $count -eq 0){break}
if($line -eq $null){$count = 0}
elseif($line.Contains(' Error:')){
Write-Host "$line $br"
}
}
}
如果我使用 0 作为 seek 方法的参数,它会按预期从头开始查找,但它还会在写入读取的行之前将 0 写入控制台。示例:
0
2011-08-31 09:26:36.31 Logon Error: 17187, Severity: 16, State: 1. 4101
2011-08-31 09:26:36.32 Logon Error: 17187, Severity: 16, State: 1. 4489
2011-08-31 09:26:38.25 Logon Error: 17187, Severity: 16, State: 1. 4929
2011-08-31 09:26:38.25 Logon Error: 17187, Severity: 16, State: 1. 5304
2011-08-31 09:26:43.75 Logon Error: 17187, Severity: 16, State: 1. 6120
如果我尝试使用 4096 而不是 0 进行查找,它只会写出:
4096
除了前两行之外,我本以为它会写出与第一行相同的行。
有人能看到问题吗?我还有另一个问题让我想到了这一点。有关更多背景信息,请参阅此
编辑:仍在尝试解决这个问题。有谁知道我还可以在哪里找到有关此问题的信息?是否可以向 Microsoft 脚本人员发送问题?
最好的问候
吉斯利
I´m trying to use the .net API to seek in a large data file. For some reason I am unable to make it work.
Here is my code:
function check_logs{
$pos = 8192
$count = 1
$path = 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\Log\ERRORLOG.2'
$br = 0
$reader = [System.IO.File]::OpenText($path)
$reader.DiscardBufferedData()
$reader.BaseStream.Seek(0, [System.IO.SeekOrigin]::Begin)
for(;;){
$line = $reader.ReadLine()
if($line -ne $null){$br = $br + [System.Text.Encoding]::UTF8.GetByteCount($line)}
if($line -eq $null -and $count -eq 0){break}
if($line -eq $null){$count = 0}
elseif($line.Contains(' Error:')){
Write-Host "$line $br"
}
}
}
If I use 0 as a parameter for the seek method it seeks from the beginning as expected but it also writes 0 out to the console before it writes the lines read. Example:
0
2011-08-31 09:26:36.31 Logon Error: 17187, Severity: 16, State: 1. 4101
2011-08-31 09:26:36.32 Logon Error: 17187, Severity: 16, State: 1. 4489
2011-08-31 09:26:38.25 Logon Error: 17187, Severity: 16, State: 1. 4929
2011-08-31 09:26:38.25 Logon Error: 17187, Severity: 16, State: 1. 5304
2011-08-31 09:26:43.75 Logon Error: 17187, Severity: 16, State: 1. 6120
If I try to seek using 4096 instead of 0 it only writes out:
4096
I would have thought it would write out the same lines as the first one did apart from the first two.
Can someone see the problem? I had another question that got me to this. For further background see this
EDIT: Still trying to figure this out. Does anyone know where else I could try to find information regarding this problem? Is it possible to send questions to the Microsoft scripting guy?
Best regards
Gísli
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Seek 方法返回流中的新位置,这就是打印出数字的原因。
至于为什么没有得到输出:
$reader.DiscardBufferedData()
的调用。The Seek method returns the new position within the stream, which is why you are having a number printed out.
As to why you are not getting an output:
$reader.DiscardBufferedData()
before the seek.所以我终于找到了答案。由于某种我不知道的原因,我必须使用二进制阅读器。
以下是我的完整功能:
帮助
感谢Gísli 的
So I finally found the answer. For some reason unknown to me I have to use a binary reader.
Here below is my complete function:
Thanks for the help
Gísli
我有类似的问题。控制台上打印出寻找的位置。我只需要将返回值分配给某个变量,就解决了问题。
因此,
我必须写一些类似的内容,而不是:
问候,
塞贾斯维五世
I had a similar problem. The seeked-off position was getting printed on the console. I just had to assign the return value to some variable, and that solved the problem.
So instead of:
I had to write something like:
Regards,
Thejasvi V