开发者

Counting Directory Error Message

whenever I run this below it gives the output but it also gives an error if I did not enter开发者_如何学运维 in a direcotry. Any idea how I can "hide" the error message that comes?

Checking Directory Count Read-Host : An error of type "System.Management.Automation.Host.PromptingException" has occurred. At C:\Users\lara\AppData\Local\Temp\5820c1b7-ee6c-47f1-9a6c-06de6dc9e68f.ps1:2 char:20 + $Source = Read-Host <<<< "Please enter first directory to check" + CategoryInfo : ResourceUnavailable: (:) [Read-Host], 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"
}

Update:

Thanks for all the help. I basically want to merge what I have below into another script that does folder comparision. I need to know how many files are in the directory I want to check and also the files that do not exist. I have the below code - I know it is really badly written! I just was not too sure how to get the $source and $target into the same IF statment so ended up making a basic mistake of having two IF statements!

Also - is there a way of instead of showing => and <= that I show "does not exist in $source" or "does not exists in $target" - I will be sending this code to another team and do not want to confuse them too much:

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"


I suggest you first test if the entered path exists before you try to list its content. I would also cast the result to an array, if the result of use Get-ChildItem is a scalar (one object only) it won't have a count property :

$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:

I didn't test the code but you need to reorder the commands. Also, do the compare on the Name property:

$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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜