2011-08-09 72 views
1

每當我在下面運行時,它會給出輸出,但如果我沒有輸入進程,它也會給出錯誤。任何想法我怎麼能「隱藏」出現的錯誤信息?計數目錄錯誤消息

檢查目錄計數 讀取主機:發生類型爲「System.Management.Automation.Host.PromptingException」的錯誤。 在C:\用戶\拉拉\應用程序數據\本地的\ Temp \ 5820c1b7-ee6c-47f1-9a6c-06de6dc9e68f.ps1:2字符:20 + $源=讀主機< < < <「請輸入第一個目錄檢查「 + CategoryInfo:ResourceUnavailable:(:) [讀主持人],PromptingException + FullyQualifiedErrorId:System.Management.Automation.Host.PromptingException,Microsoft.PowerShell.Commands.ReadHostCommand

Write-Host "Checking Directory Count" -ErrorAction SilentlyContinue 
$Source = Read-Host "Please enter first directory to check" 

If($Source) 
{ 
    Write-host "There are " (Get-ChildItem $Source).Count "items in the ""$Source"" directory" 
} 
Else 
{ 
    Write-Host "Please enter a directory" 
} 

更新:

感謝您的幫助。我基本上想要將下面的內容合併到另一個文件夾比較的腳本中。我需要知道我想檢查的目錄中有多少個文件以及不存在的文件。我有下面的代碼 - 我知道這是寫得非常糟糕!我只是不太清楚如何讓$ source和$ target進入相同的IF語句,所以最終導致了兩個IF語句的基本錯誤!

另外 - 是否有一種方法,而不是顯示=>和< =我顯示「不存在於$源」或「不存在於$目標」 - 我將發送此代碼給另一個團隊和不想混淆它們太多:

Write-Host "Checking Directory Count and Folder comparision" -ErrorAction SilentlyContinue 

$Source = Read-Host "Please enter Source directory to check" 
$Target = Read-Host "Please enter Target directory to check" 

$child1 = Get-ChildItem -Path $Source 
$child2 = Get-ChildItem -Path $Target 

Compare-Object $child1 -DifferenceObject $child2 

Write-Host "" 
If($source -and (Test-Path -Path $source -PathType Container)) 
{ 
    "There are $(@(Get-ChildItem $Source).Count) items in the '$Source' directory" 
} 
Else 
{ 
    Write-Host "Please enter a directory" 
} 
If($source -and (Test-Path -Path $Target -PathType Container)) 
{ 
    "There are $(@(Get-ChildItem $Target).Count) items in the '$Target' directory" 
} 
Else 
{ 
    Write-Host "Please enter a directory" 
} 


Write-Host "" 
Write-Host "Any symbols with '<=' mean that the file Does NOT exist in TARGET" 
Write-Host "Any symbols with '=>' mean that the file Does NOT exist in SOURCE" 
+1

您發佈的內容對我來說運行得非常好。那是你遇到問題的實際代碼? – EBGreen

+0

嗨 - 謝謝你的回覆。它運行良好 - 問題是,當它要求一個directoy和一個用戶什麼都不輸入 - 只需按下取消 - 然後炸彈出來,錯誤上面說錯誤。我是否需要一些錯誤檢查來說明何時用戶沒有輸入任何內容,然後向用戶報告提示? – lara400

+1

不,我要說的是,無論我什麼都沒輸入,或者進入了一條道路,我沒有遇到任何錯誤。 – EBGreen

回答

1

我建議你在嘗試列出其內容之前先測試輸入的路徑是否存在。我還要把結果到一個數組,如果使用的結果獲得-ChildItem是一個標量(只有一個對象),它不會有一個Count屬性:

$Source = Read-Host "Please enter first directory to check" 

If($source -and (Test-Path -Path $source -PathType Container)) 
{ 
    "There are $(@(Get-ChildItem $Source).Count) items in the '$Source' directory" 
} 
Else 
{ 
    Write-Host "Please enter a directory" 
} 

UPADTE:

我沒不要測試代碼,但需要對命令重新排序。此外,請在名稱屬性上進行比較:

$Source = Read-Host "Please enter Source directory to check" 
$Target = Read-Host "Please enter Target directory to check" 

If($source -and (Test-Path -Path $source -PathType Container)) 
{ 
    "There are $(@(Get-ChildItem $Source).Count) items in the '$Source' directory" 
} 
Else 
{ 
    Write-Host "Please enter a source directory" 
    return 
} 

If($source -and (Test-Path -Path $Target -PathType Container)) 
{ 
    "There are $(@(Get-ChildItem $Target).Count) items in the '$Target' directory" 
} 
Else 
{ 
    Write-Host "Please enter a Target directory" 
    return 
} 


$child1 = Get-ChildItem -Path $Source 
$child2 = Get-ChildItem -Path $Target 

Compare-Object $child1 -DifferenceObject $child2 -Property Name 

Write-Host "" 
Write-Host "Any symbols with '<=' mean that the file Does NOT exist in TARGET" 
Write-Host "Any symbols with '=>' mean that the file Does NOT exist in SOURCE" 
+0

aahhhhh .........我現在看到謝伊。從來不知道測試路徑 - 我已經測試出來並且工作正常 - 感謝陣列位。只需要弄清楚如何添加第二個目錄並現在比較兩個directoies ... – lara400

+1

比較文件數量? –

+0

嗨謝 - 我已經用我所做的更新了主帖,希望你能看到我可以更容易的地方 - 可能需要一個函數或其他東西? – lara400