Get-ChildItem.Length 错误 [英] Get-ChildItem.Length is Wrong
问题描述
我正在编写一个递归函数,它遍历一个目录并复制其中的每个文件和文件夹.我在函数中的第一个检查是查看传入的路径是否有子路径.为了找出这一点,我使用以下方法:
I am writing a recursive function that goes through a directory and copies every file and folder in it. The first check I have in the function is to see if the path passed in has children. To find this out, I use the following method:
[array]$arrExclude = @("Extras")
Function USBCopy
{
Param ([string]$strPath, [string]$strDestinationPath)
try
{
$pathChildren = Get-ChildItem -Path $strPath
if($pathChildren.Length -gt 0)
{
foreach($child in $pathChildren)
{
if($arrExclude -notcontains $child)
{
$strPathChild = "$strPath\$child"
$strDestinationPathChild = "$strDestinationPath\$child"
Copy-Item $strPathChild -Destination $strDestinationPathChild
USBCopy $strPathChild $strDestinationPathChild
}
}
}
}
catch
{
Write-Error ("Error running USBCopy: " + $Error[0].Exception.Message)
}
}
在大多数情况下,我的函数可以工作,但我的代码会说一个目录是空的,当它实际上有 1 个文件时.当我调试我的函数时,变量会说文件夹有子文件夹,但变量的长度为 0.有人知道如何解决这个问题吗?
For the most part my function works, but my code will say a directory is empty when it actually has 1 file in it. When I debug my function, the variable will say that the folder has children but the variable's length is 0. Anyone know how to get around this?
推荐答案
Try $pathChildren.Count
而不是 $pathChildren.Length
- 这将返回项目的数量在数组中.
Try $pathChildren.Count
instead of $pathChildren.Length
- that will return the number of items in the array.
这篇关于Get-ChildItem.Length 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!