尝试在读取2个文件时进行2次循环,从第一个文件中获取1个值的目的是将其与第二个文件中的1,2,3rd ......等值进行比较,然后返回到外部循环,取第2个值并进行比较按顺序排列第二个文件中的所有值,依此类推。
问题是它只在第一个文件中的第一个值上运行比较并停止。
代码:
Get-ExecutionPolicy
$main_list = [System.IO.File]::OpenText('c:\ping-workstations\main_list.txt')
$retired = [System.IO.File]::OpenText('c:\ping-workstations\retired.txt')
$main_i = 1
$retired_i = 1
[string]$main_list_line
[string]$retired_list_line
try
{
while(!$main_list.EndOfStream)
{
$main_list_line = $main_list.ReadLine()
while(!$retired.EndOfStream)
{
$retired_list_line = $retired.readline()
if($main_list_line -eq $retired_list_line)
{
write-host "`n`n`n match found for $main_list_line in main list `n`n`n`n"
add-content "c:\ping-workstations\matches-through-text-files.txt" "$main_list_line`n"
}
write-host "main iteration: $main_i / inside iteration: $retired_i`n - main value: $main_list_line ---- inside value: $retired_list_line`n`n"
$retired_1++
}
$main_i++
}
}
finally
{
$main_list.close()
$retired.close()
}
答案 0 :(得分:0)
内循环结束后,您需要重新打开$ retired文件。正如现在所写的那样,一旦内部循环遍历$ retired文件,$ retired.EndOfStream将永远保持为真。
try
{
while(!$main_list.EndOfStream)
{
$main_list_line = $main_list.ReadLine()
while(!$retired.EndOfStream)
{
$retired_list_line = $retired.readline()
if($main_list_line -eq $retired_list_line)
{
write-host "`n`n`n match found for $main_list_line in main list `n`n`n`n"
add-content "c:\ping-workstations\matches-through-text-files.txt" "$main_list_line`n"
}
write-host "main iteration: $main_i / inside iteration: $retired_i`n - main value: $main_list_line ---- inside value: $retired_list_line`n`n"
$retired_1++
}
$main_i++
$retired.close()
$retired = [System.IO.File]::OpenText('c:\ping-workstations\retired.txt')
}
}
finally
{
$main_list.close()
$retired.close()
}
作为旁注,您是否考虑过使用Powershell ISE?它具有调试功能;通过设置断点并单步执行代码,我能够快速发现问题。