繁体   English   中英

get-childitem获取子目录名称追加到脚本目录路径

[英]get-childitem get subdir name append on script directory path

我正在尝试编写PS脚本以在参数指定的目录中查找空目录。 “空目录”不应包含任何文件和子目录。

这是脚本:

    param (
[parameter (mandatory=$true,position=0)]
[string]$Path
)
$dirInfo = get-childitem $Path -recurse | ? {$_.PSIsContainer -eq $True} | % {get-childitem $_} | Measure-Object 
$dirInfo | ? {$dirInfo.count -eq 0} |  Select-Object Fullname

当我在“ C:\\ documents文件夹”上运行它时,出现以下错误:

**get-childitem : Cannot find path 'C:\Documents\ManualScripts\accounts' because it does not exist.
At C:\Documents\ManualScripts\Check-no-file-and-subdir-dir.ps1:5 char:79
+ $dirInfo = get-childitem $Path -recurse | ? {$_.PSIsContainer -eq $True} | % {ge ...
+                                                                               ~~
    + CategoryInfo          : ObjectNotFound: (C:\Documents\ManualScripts\accounts:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand**

C:\\ documents下的每个子文件夹都有很多类似的错误。 它将脚本本身所在的目录(C:\\ Documents \\ ManualScripts)附加到子目录名称。

做了我的研究仍然不知道。 任何输入表示赞赏。 谢谢:)

在上述发布之后,我对脚本进行了一些更改,但到目前为止仍无法正常工作:

param (
[parameter (mandatory=$true,position=0)]
[string]$Path
)

$Dirs = Get-ChildItem $Path -recurse | ? {$_.PSIsContainer -eq $True} | ForEach-Object -Process {$_.FullName}  #get list of all directories with whole path

foreach ($Dir in $Dirs) {
$emptyDir = Get-ChildItem $Dir | Measure | where {$_.Count -eq 0} | select-Object
}

上床睡觉之前,我相信我会有所进步。 现在脚本如下所示:

param (
[parameter (mandatory=$true,position=0)]
[string]$Path
)
$Dirs = Get-ChildItem $Path -recurse | ? {$_.PSIsContainer -eq $True} | ForEach-Object -Process {$_.FullName}  #get list of all directories with whole path
#$Dirs | ForEach-Object {Get-ChildItem $_} | ForEach-Object {measure} | where {$_.Count -eq 0} | Select-Object $Dirs 
ForEach ($Dir in $Dirs) {
$emptyDir = Get-ChildItem $Dir | Measure | where {$_.Count -eq 0} | Select-Object
$emptyDir
}

如果我运行,输出中会有东西(虽然不是我所期望的)

PS C:\Documents\ManualScripts> .\Check-no-file-and-subdir-dir-rev01.ps1
cmdlet Check-no-file-and-subdir-dir-rev01.ps1 at command pipeline position 1
Supply values for the following parameters:
Path: C:\documents


Count    : 0
Average  : 
Sum      : 
Maximum  : 
Minimum  : 
Property : 

Count    : 0
Average  : 
Sum      : 
Maximum  : 
Minimum  : 
Property : 
......
......

诸如“选择对象”之类的东西会选择“度量”的输出,而不是其下没有文件/子目录的目录的名称。

试试看:

Get-ChildItem $Path -Recurse -Force | 
Where-Object {$_.PSIsContainer -and (Get-ChildItem $_.FullName -Force | Measure-Object).Count -eq 0}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM