我需要获取所有文件,包括属于特定类型的子文件夹中存在的文件.
我正在做这样的事情,使用Get-ChildItem:
Get-ChildItem "C:\windows\System32" -Recurse | where {$_.extension -eq ".txt"}
Run Code Online (Sandbox Code Playgroud)
但是,它只返回文件名而不是整个路径.
显然,在PowerShell(第3版)中并非所有$null都是相同的:
>function emptyArray() { @() }
>$l_t = @() ; $l_t.Count
0
>$l_t1 = @(); $l_t1 -eq $null; $l_t1.count; $l_t1.gettype()
0
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
>$l_t += $l_t1; $l_t.Count
0
>$l_t += emptyArray; $l_t.Count
0
>$l_t2 = emptyArray; $l_t2 -eq $null; $l_t2.Count; $l_t2.gettype()
True
0
You cannot call a method on a null-valued expression.
At line:1 char:38
+ $l_t2 = emptyArray; $l_t2 -eq $null; $l_t2.Count; $l_t2.gettype()
+ ~~~~~~~~~~~~~~~
+ CategoryInfo …Run Code Online (Sandbox Code Playgroud) 我正在编写一个遍历目录的递归函数,并复制其中的每个文件和文件夹.我在函数中的第一个检查是查看传入的路径是否有子节点.为了找到这个,我使用以下方法:
[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)
}
}
Run Code Online (Sandbox Code Playgroud)
在大多数情况下,我的函数可以工作,但我的代码会说当一个目录实际上有一个文件时它是空的.当我调试我的函数时,变量会说该文件夹有子项但变量的长度为0.任何人都知道如何解决这个问题?