Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File Powershell-列出子目录并计算每个子目录的文件数_File_Powershell_Directory_Powershell 3.0_Powershell Ise - Fatal编程技术网

File Powershell-列出子目录并计算每个子目录的文件数

File Powershell-列出子目录并计算每个子目录的文件数,file,powershell,directory,powershell-3.0,powershell-ise,File,Powershell,Directory,Powershell 3.0,Powershell Ise,我有一个满是子目录的目录,其中有任意数量的0到300个文件 我想输出子目录名和该子目录中的文件数 到目前为止,无论子目录中有多少实际文件,我都得到了0 $dir = "C:\Folder\" $subFiles = (Get-ChildItem $dir -recurse | where-object {$_.PSIsContainer -eq $true }) $subFiles | % { Get-ChildItem -Path $_ -Force -Recurse | Measure-O

我有一个满是子目录的目录,其中有任意数量的0到300个文件

我想输出子目录名和该子目录中的文件数

到目前为止,无论子目录中有多少实际文件,我都得到了0

$dir = "C:\Folder\" 
$subFiles = (Get-ChildItem $dir -recurse | where-object {$_.PSIsContainer -eq $true })
$subFiles | % {
Get-ChildItem -Path $_ -Force -Recurse | Measure-Object | Select-Object -ExpandProperty Count
write-host "$_"
}
它有时还包括运行脚本的目录,即“C:\Users\Blah\documents and settings\startmen”,并导致错误


非常感谢您的任何帮助这正是我想要的:)它不太漂亮,但很管用

$dir = "C:\folder\"
$subFiles = (Get-ChildItem $dir -recurse | where-object {$_.PSIsContainer -eq $true })
$subFiles | % {
$path = $dir+$_
$files = Get-ChildItem -Path $path -Force -Recurse -File -ErrorAction SilentlyContinue
write-host "$_ Files:" $files.count
}

这正是我想要的:)它不是很漂亮,但它奏效了

$dir = "C:\folder\"
$subFiles = (Get-ChildItem $dir -recurse | where-object {$_.PSIsContainer -eq $true })
$subFiles | % {
$path = $dir+$_
$files = Get-ChildItem -Path $path -Force -Recurse -File -ErrorAction SilentlyContinue
write-host "$_ Files:" $files.count
}

您使用的至少是PowerShell 3.0,因为您使用的是
Get ChildItem
-File
参数,所以您不需要使用
where对象{$\u.PSIsContainer-eq$true}
。已替换为
-Directory
参数。循环浏览所有文件夹并收集文件夹名称及其文件数。我删除了文件计数的
-Recurse
,因为这可能会产生误导。如果它更适合你,就把它放回去。最后一个
Select Object
是确保输出的顺序,现在它是一个对象,您可以对其进行排序或执行任何您想要的操作

$dir = "C:\File" 
Get-ChildItem $dir -Recurse -Directory | ForEach-Object{
    [pscustomobject]@{
        Folder = $_.FullName
        Count = @(Get-ChildItem -Path $_.Fullname -File).Count
    }
} | Select-Object Folder,Count
Insight


您以前遇到过这些错误,因为您没有在
Get ChildItem
中调用完整路径,而只是调用了文件夹名。如果没有完整路径,则假定您正在当前目录中查找文件夹。通常是您的用户目录。

您使用的至少是PowerShell 3.0,因为您使用的是
-File
参数
Get ChildItem
,因此您不需要使用
where对象{$\u.PSIsContainer-eq$true}
。已替换为
-Directory
参数。循环浏览所有文件夹并收集文件夹名称及其文件数。我删除了文件计数的
-Recurse
,因为这可能会产生误导。如果它更适合你,就把它放回去。最后一个
Select Object
是确保输出的顺序,现在它是一个对象,您可以对其进行排序或执行任何您想要的操作

$dir = "C:\Folder\" 
$subFiles = (Get-ChildItem $dir -recurse | where-object {$_.PSIsContainer -eq $true })
$subFiles | % {
Get-ChildItem -Path $_ -Force -Recurse | Measure-Object | Select-Object -ExpandProperty Count
write-host "$_"
}
$dir = "C:\File" 
Get-ChildItem $dir -Recurse -Directory | ForEach-Object{
    [pscustomobject]@{
        Folder = $_.FullName
        Count = @(Get-ChildItem -Path $_.Fullname -File).Count
    }
} | Select-Object Folder,Count
Insight


您以前遇到过这些错误,因为您没有在
Get ChildItem
中调用完整路径,而只是调用了文件夹名。如果没有完整路径,则假定您正在当前目录中查找文件夹。通常是您的用户目录。

此计数方法对于远程目录似乎更快

$dir = "C:\Folder\" 
$subFiles = (Get-ChildItem $dir -recurse | where-object {$_.PSIsContainer -eq $true })
$subFiles | % {
Get-ChildItem -Path $_ -Force -Recurse | Measure-Object | Select-Object -ExpandProperty Count
write-host "$_"
}
$count = [System.IO.Directory]::GetFiles($_.Fullname).Count

对于远程目录,这种计数方法似乎更快

$count = [System.IO.Directory]::GetFiles($_.Fullname).Count

嗨,马特。有没有可能修改脚本,使其仅包含所提供路径中的子文件夹?@mrjaypiper这很简单。只需在示例脚本中的第一个和第二个gci上使用
-Depth
参数
Get ChildItem
我尝试了使用“-Depth 1”(也尝试了2),与原始脚本没有区别。@JayViper先生,也许我误解了,但您可能只需要删除
-Recurse
?这应该只处理直接子文件夹。例如:parent1(最上面的文件夹)和3个子文件夹(子文件夹1、子文件夹2和子文件夹3)。在子文件夹1到3中有更多的文件夹和文件。我想显示子文件夹1到3中的文件数。我尝试了不使用递归,它为路径下的所有子文件夹提供了0计数。嗨,Matt。有没有可能修改脚本,使其仅包含所提供路径中的子文件夹?@mrjaypiper这很简单。只需在示例脚本中的第一个和第二个gci上使用
-Depth
参数
Get ChildItem
我尝试了使用“-Depth 1”(也尝试了2),与原始脚本没有区别。@JayViper先生,也许我误解了,但您可能只需要删除
-Recurse
?这应该只处理直接子文件夹。例如:parent1(最上面的文件夹)和3个子文件夹(子文件夹1、子文件夹2和子文件夹3)。在子文件夹1到3中有更多的文件夹和文件。我想显示子文件夹1到3中的文件数。我尝试了不使用-recurse,它为路径下的所有子文件夹提供了0计数。