2017-10-12 17 views
0

我在格式化這樣的CSV號碼清單:如何搜索目錄中的所有文件以獲取字符串(數字)列表?

po 
123 
456 
789 

我也有100多個文件的目錄。我需要檢查csv中每個字符串的每個文檔。如果其中一個數字位於其中一個文檔中,則需要將其移至其他文件。這是我到目前爲止:

$files = Get-ChildItem "\\wh1-app\SPS EDI\ARCHIVE IN\*.txt" 


foreach ($file in $files) { 

    $file = Get-Content $file 

    $pos = Import-Csv -Path C:\Users\ttilton\Desktop\pos.csv -OutVariable string -ErrorAction SilentlyContinue 

    foreach ($po in $pos) { 

     $containsWord = $file | %{$_ -match $po.po} 

     if ($containsWord -contains $true){ 

      write-host $file + " is " + $po.po 

      #Copy-Item $file C:\Users\ttilton\Desktop\pos\ 

     } 
    } 

但它似乎並沒有工作。它運行沒有錯誤,但它並沒有真正做任何事情。

任何想法?謝謝!!

+0

變化ErrorAction停止,所以你可以看到 –

+0

'Copy-Item'不會被執行,因爲'#'將整行變成註釋 –

+0

@ C.Helling據我所知,代碼'$ containsWord'是一個布爾數組你的建議「¨f'子句總會是真的 –

回答

0

對於我測試過的應該工作,我猜想你的遠程位置(\\wh1-app\SPS EDI\ARCHIVE IN\*.txt)(包含你的.txt文件)的(可訪問性)有問題。

要解決這一點,我要麼把幾的源文件的本地(如C:\temp\*.txt

或添加在你的腳本附加Write-Host命令。例如在這裏:

... 
foreach ($file in $files) { 
    Write-Host $file.name # Is there really a file picked up here? 
    $file = Get-Content $file 
    ... 

一些一般性的建議,以加速比你發揮作用:

  • $pos = Import-Csv -Path C:\Users\ttilton\Desktop\pos.csv -OutVariable string -ErrorAction SilentlyContinueforeach ($file in $files) {...循環的,你只需要讀取.csv文件一次。
  • 不要(繼續)讀取.txt如果你找到了一個匹配(一個單$True$containsWord足以驗證if ($containsWord -contains $true){...要停止在foreach循環參見:https://stackoverflow.com/a/10420522/1701026
+0

是的,我已經拿出了一大堆文件,現在在本地運行。它仍然很慢,但效果更好,之前感謝\t \t @TessellatingHeckler在上述評論中提供的建議。謝謝! –

相關問題