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?
推荐答案
尝试使用$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屋!