计数目录错误消息

发布于 2024-11-28 19:37:21 字数 2054 浏览 1 评论 0原文

每当我在下面运行它时,它都会给出输出,但如果我没有输入目录,它也会给出错误。知道如何“隐藏”出现的错误消息吗?

检查目录计数 Read-Host:发生了“System.Management.Automation.Host.PromptingException”类型的错误。 在 C:\Users\lara\AppData\Local\Temp\5820c1b7-ee6c-47f1-9a6c-06de6dc9e68f.ps1:2 字符:20 + $Source = 读取主机 <<<< “请输入第一个目录进行检查” + 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 语句!

另外 - 有没有办法不显示 =>和 <= 我显示“$source 中不存在”或“$target 中不存在” - 我将将此代码发送给另一个团队,并且不想让他们太困惑:

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"

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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梅倚清风 2024-12-05 19:37:21

我建议您在尝试列出其内容之前先测试输入的路径是否存在。我还将结果转换为数组,如果使用 Get-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:

我没有测试代码,但您需要重新排序命令。另外,对 Name 属性进行比较:

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

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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文