我正在编写一个遍历目录并复制其中的每个文件和文件夹的递归函数。我在函数中的第一个检查是查看传入的路径是否有子路径。为了找出这一点,我使用以下方法:
[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。有人知道如何解决这个问题吗?