2016-11-29 77 views
0

即時通訊目前試圖讓一個腳本來做到以下幾點:PowerShell的 - txt文件搜索

搜索文件的位置和獲得任何txt文件,.INI,config文件。 在來自用戶的輸入上過濾此文件。 將所有文件移到某個位置。

我有一個問題試圖餵養變量,它可能真的很簡單,但即時通訊正在努力弄清楚我需要做什麼而不需要手動分割字符串。有任何想法嗎?

$QQ = Read-Host -Prompt "String your searching for:" 
$QL = Read-Host -Prompt "Enter the file location you wish to search:" 
$FT = Get-ChildItem -Path "$QL" -recurse | where {$_.extension -eq ".txt"} | % {$_.fullname} 
$FI = Get-ChildItem -Path "$QL" -recurse | where {$_.extension -eq ".ini"} | % {$_.fullname} 
$FC = Get-ChildItem -Path "$QL" -recurse | where {$_.extension -eq ".config"} | % {$_.fullname} 
$FTS = Get-Content -Path "$FT" -Filter "$QQ" 
$FIS = Get-Content -Path "$FI" -Filter "$QQ" 
$FCS = Get-Content -Path "$FC" -Filter "$QQ" 
$FD = "C:\Search-$QQ" 
$FD1 = Get-ChildItem $FD 

function folder { 
    if ($FD -eq $Null) {New-Item "$FD" -ItemType directory}} 

function search{ 
    if ($FTS -ne $null){Copy-Item -Path $ft -Destination "$fd" | Write-Host "$FT" | Format-List} 
    if ($FIS -ne $null){Copy-Item -path $fi -Destination "$fd" | Write-Host "$FI" | Format-List} 
    if ($FCS -ne $null){Copy-Item -Path $fc -destination "$fd" | Write-Host "$FC" | Format-List} 
} 
folder 
search; 

正在接收錯誤的一個例子是(很明顯,它處理的字符串作爲一個在多個文件的問題)

獲取內容:找不到路徑「C: \ test \ Test \ 1c.config C:\ test \ Test \ 2c.config C:\ test \ Test \ 3c.config',因爲它不存在。

+0

伊夫固定它myselfjust需要將$ FT,$ FI,$ FC上的報價從$ FTS,$ FIS,$ FCS變量中刪除。 –

回答

1

馬上蝙蝠我看到folder功能將無法正常工作屬性作爲$FD因爲你分配一個字符串它,如果你需要檢查,如果該文件夾存在使用Test-Path永遠不會爲空。但與您的實際腳本相關的問題是您使用的get-content,它試圖將您的所有文件內容讀取到一個字符串數組中。從它的外觀你應該只是與Get-ChildItem返回的數組,並使用Test-Path來檢查你的路徑,而不是你奇怪的Get-Content方法(因爲文件的內容並不重要,只是它們是否存在或不)。您還需要合併一個循環來單獨處理每個數組元素,而不是像您一樣嘗試對它們進行處理。

+0

關於測試路徑的公平點。不過我關心文件的內容,因此試圖過濾內容。 –

+0

無論如何我只需要將$ FT,$ FI,$ FC上的報價從$ FTS,$ FIS,$ FCS變量中刪除即可。 –

0

(這個腳本是大規模的搜索應用程序文件找到像服務器ID或密碼硬編碼等值)

固定碼:

$QQ = Read-Host -Prompt "String your searching for:" 
$QL = Read-Host -Prompt "Enter the file location you wish to search:" 
$FT = Get-ChildItem -Path "$QL" -recurse | where {$_.extension -eq ".txt"} | % {$_.fullname} 
$FI = Get-ChildItem -Path "$QL" -recurse | where {$_.extension -eq ".ini"} | % {$_.fullname} 
$FC = Get-ChildItem -Path "$QL" -recurse | where {$_.extension -eq ".config"} | % {$_.fullname} 
$FTS = Get-Content -Path $FT -Filter "$QQ" 
$FIS = Get-Content -Path $FI -Filter "$QQ" 
$FCS = Get-Content -Path $FC -Filter "$QQ" 
$FD = "C:\Support\Search-$QQ" 
$FD1 = Test-Path $FD 

function folder { 
if ($FD1 -eq $false) {New-Item "$FD" -ItemType directory} 
} 
function search{ 
    if ($FTS -ne $null){Copy-Item -Path $ft -Destination "$fd" | Write-Host "$FT" | Format-List} 
    if ($FIS -ne $null){Copy-Item -path $fi -Destination "$fd" | Write-Host "$FI" | Format-List} 
    if ($FCS -ne $null){Copy-Item -Path $fc -destination "$fd" | Write-Host "$FC" | Format-List} 
} 

folder 
search; 
0

試試這個

$QL = Read-Host -Prompt "Enter the file location you wish to search:" 
if (-not (Test-Path $QL)) 
{ 
    write-host "Specified path ($QL) doesnt exists " 
    return; 
} 

$QQ = Read-Host -Prompt "String your searching for:" 
$FD = "C:\Search-$QQ" 
New-Item "C:\Search-$QQ" -ItemType directory -Force | out-null 

Get-ChildItem -Path "$QL" -recurse -include "*.txt", "*.ini", "*.config" | 
    select-string -Pattern "$QQ" -SimpleMatch | 
     %{ Copy-Item -Path $_.Path -Destination "$fd" -Force ; $_.Path }