2014-10-10 12 views
0
$A1= get-content -path "File path a1" 
$B1= get-content -path "File path b1" 
$A2= get-content -path "File path a2" 
$B2= get-content -path "File path B2" 
$A3= get-content -path "File path a3" 
$B3= get-content -path "File path B3" 

#I want to select A.i and B.i in each loop 

while ($i -le 5) 
{ 
foreach ($line1 in "What to put here")) 
{ 
$found = $false 
foreach ($line2 in "What to put here")) 
{ 
if ($line1 -eq $line2) 
{ 
    write-host "Do something" 
} 
} 

我想知道我應該在「放什麼位置」,使 一)第一循環選擇A1和B1文件 20b使用)第二循環選擇A2和B2文件 等如何循環使用PowerShell其中循環值是字符串的組合和可變


你好, 下面的代碼的時間過長對大文件打印在每個文件什麼不同。你可以提出一些優化?

$i = 1 
while ($i -le 7) 
{ 
$t1 = Get-Content -Path $(($oldpath) +($(Get-Variable "F$i").Value) +($Format)) 
$t2 = Get-Content -Path $(($NEWpath) +($(Get-Variable "F$i").Value) +($Format)) 
$list1  = new-object 'system.collections.generic.list[string]' 
$list2  = new-object 'system.collections.generic.list[string]' 
$listshared = new-object 'system.collections.generic.list[string]' 
$found = $false 

foreach ($line1 in $t1) 
{ 
$found = $false 
foreach ($line2 in $t2) 
if (-not $found) 
{ 
    $list1.add($line1) 
} 
} 

foreach ($line2 in $t2) 
{ 
$found = $false 
foreach ($line1 in $t1) 

if (-not $found) 
{ 
    $list2.add($line2) 
} 
} 
Write-Host "Things only in new txt file:" -foreground "magenta" 
$list1 

Write-Host "Things only in old txt file:" -foreground "magenta" 
$list2 

Remove-Variable "t1" 
Remove-Variable "t2" 
$i++ 
} 

回答

0

可以使用例如:

Get-Variable -Name "A$i" |select Value 

,但它似乎是一個不好的做法,用一個變量爲每個文件...
但如果你比較路徑或文件沒有指定。 如果有文件,請與Get-Content結合使用。

UPDATE:

$i=1 
while ($i -le 5) 
{ 
$file1=Get-Content -Path $($(Get-Variable "A$i").Value) 
$file2=Get-Content -Path $($(Get-Variable "B$i").Value) 
    foreach ($line1 in $file1) 
    { 
     $found = $false 
     foreach ($line2 in $file2) 
     { 
      if ($line1 -eq $line2) 
      { 
       write-host "Do something" 
      } 
     } 

    } 
    Remove-Variable "file1" 
    Remove-Variable "file2" 
    $i++ 
} 

這是每一個變量

+0

我已經更新了變量定義在我的崗位無Get-Content版本。我正在比較定義的變量位置中的文件。當我使用Get-variable它不工作。我使用如下:foreach($ line1 in(Get-variable -Name「A $ i」| select Value))。既然我不是專家,請原諒我的天真查詢。 - Santosh Pillai 15分鐘前 – 2014-10-10 16:13:10

+0

@SantoshPillai您是否知道在您的代碼中,您沒有初始化$ i,並且您沒有增加它? – Deptor 2014-10-10 16:57:22

+0

抱歉,我沒有粘貼整個代碼$ i將遞增,通過數字..我把代碼 – 2014-10-10 16:57:55

相關問題