我正在使用PowerShell 2.0,我想要管理某个路径的所有子目录 . 以下命令输出所有文件和目录,但我无法弄清楚如何过滤掉文件 .
Get-ChildItem c:\mypath -Recurse
我已经尝试使用 $_.Attributes 获取属性,但后来我不知道如何构建 System.IO.FileAttributes 的文字实例来比较它 . 在 cmd.exe 它会
$_.Attributes
System.IO.FileAttributes
cmd.exe
dir /b /ad /s
使用这一个:
Get-ChildItem -Path \\server\share\folder\ -Recurse -Force | where {$_.Attributes -like '*Directory*'} | Export-Csv -Path C:\Temp\Export.csv -Encoding "Unicode" -Delimiter ";"
要具体回答原始问题(使用IO.FileAttributes):
Get-ChildItem c:\mypath -Recurse | Where-Object {$_.Attributes -and [IO.FileAttributes]::Directory}
我确实更喜欢Marek的解决方案( Where-Object { $_ -is [System.IO.DirectoryInfo] } ) .
Where-Object { $_ -is [System.IO.DirectoryInfo] }
For PowerShell versions less than 3.0:
Get-ChildItem 返回的 FileInfo 对象具有"base"属性 PSIsContainer . 您只想选择那些项目 .
Get-ChildItem
FileInfo
PSIsContainer
Get-ChildItem -Recurse | ?{ $_.PSIsContainer }
如果你想要目录的原始字符串名称,你可以这样做
Get-ChildItem -Recurse | ?{ $_.PSIsContainer } | Select-Object FullName
For PowerShell 3.0 and greater:
dir -Directory
在PowerShell 3.0中,它更简单:
Get-ChildItem -Directory #List only directories Get-ChildItem -File #List only files
Get-ChildItem -dir #lists only directories Get-ChildItem -file #lists only files
如果您喜欢别名,请使用
ls -dir #lists only directories ls -file #lists only files
要么
dir -dir #lists only directories dir -file #lists only files
要递归子目录,请添加 -r 选项 .
-r
ls -dir -r #lists only directories recursively ls -file -r #lists only files recursively
在PowerShell 4.0,PowerShell 5.0(Windows 10)和PowerShell Core 6.0(Windows 10,Mac和Linux)上进行了测试 .
更清洁的方法:
Get-ChildItem "<name_of_directory>" | where {$_.Attributes -match'Directory'}
我想知道PowerShell 3.0是否有一个只返回目录的开关;添加似乎是合乎逻辑的事情 .
使用:
dir -r | where { $_ -is [System.IO.DirectoryInfo] }
从PowerShell v2和更新版本(k表示您开始搜索的文件夹):
Get-ChildItem $Path -attributes D -Recurse
如果您只想要文件夹名称,而不是其他任何内容,请使用:
Get-ChildItem $Path -Name -attributes D -Recurse
如果要查找特定文件夹,可以使用以下命令 . 在这种情况下,我正在寻找一个名为 myFolder 的文件夹:
myFolder
Get-ChildItem $Path -attributes D -Recurse -include "myFolder"
此方法需要较少的文本:
ls -r | ? {$_.mode -match "d"}
dir -Directory -Recurse | Select FullName
这将为您提供根结构的输出,其中仅包含目录的文件夹名称 .
使用下面的脚本可以实现更具可读性和简单性的方法:
$Directory = "./" Get-ChildItem $Directory -Recurse | % { if ($_.Attributes -eq "Directory") { Write-Host $_.FullName } }
希望这可以帮助!
Get-ChildItem \\myserver\myshare\myshare\ -Directory | Select-Object -Property name | convertto-csv -NoTypeInformation | Out-File c:\temp\mydirectorylist.csv
以下是哪一项
获取目标位置的目录列表: Get-ChildItem \\myserver\myshare\myshare\ -Directory
Get-ChildItem \\myserver\myshare\myshare\ -Directory
仅提取目录名称: Select-Object -Property name
Select-Object -Property name
将输出转换为CSV格式: convertto-csv -NoTypeInformation
convertto-csv -NoTypeInformation
将结果保存到文件: Out-File c:\temp\mydirectorylist.csv
Out-File c:\temp\mydirectorylist.csv
接受的答案提到了
得到"raw string" . 但实际上将返回 Selected.System.IO.DirectoryInfo 类型的对象 . 对于原始字符串,可以使用以下内容:
Selected.System.IO.DirectoryInfo
Get-ChildItem -Recurse | ?{ $_.PSIsContainer } | % { $_.FullName }
如果值连接到字符串,则差异很重要:
与 Select-Object 令人惊讶 foo\@{FullName=bar}
Select-Object
foo\@{FullName=bar}
与 ForEach -operator预期: foo\bar
ForEach
foo\bar
我的解决方案基于TechNet文章Fun Things You Can Do With the Get-ChildItem Cmdlet .
Get-ChildItem C:\foo | Where-Object {$_.mode -match "d"}
我在我的脚本中使用它,它运行良好 .
您将要使用 Get-ChildItem 以递归方式获取所有文件夹和文件 . 然后将输出管道传输到只接收文件的 Where-Object 子句中 .
# one of several ways to identify a file is using GetType() which # will return "FileInfo" or "DirectoryInfo" $files = Get-ChildItem E:\ -Recurse | Where-Object {$_.GetType().Name -eq "FileInfo"} ; foreach ($file in $files) { echo $file.FullName ; }
15 回答
使用这一个:
要具体回答原始问题(使用IO.FileAttributes):
Get-ChildItem c:\mypath -Recurse | Where-Object {$_.Attributes -and [IO.FileAttributes]::Directory}
我确实更喜欢Marek的解决方案(
Where-Object { $_ -is [System.IO.DirectoryInfo] }
) .For PowerShell versions less than 3.0:
Get-ChildItem
返回的FileInfo
对象具有"base"属性PSIsContainer
. 您只想选择那些项目 .如果你想要目录的原始字符串名称,你可以这样做
For PowerShell 3.0 and greater:
在PowerShell 3.0中,它更简单:
如果您喜欢别名,请使用
要么
要递归子目录,请添加
-r
选项 .在PowerShell 4.0,PowerShell 5.0(Windows 10)和PowerShell Core 6.0(Windows 10,Mac和Linux)上进行了测试 .
更清洁的方法:
我想知道PowerShell 3.0是否有一个只返回目录的开关;添加似乎是合乎逻辑的事情 .
使用:
从PowerShell v2和更新版本(k表示您开始搜索的文件夹):
如果您只想要文件夹名称,而不是其他任何内容,请使用:
如果要查找特定文件夹,可以使用以下命令 . 在这种情况下,我正在寻找一个名为
myFolder
的文件夹:此方法需要较少的文本:
使用:
这将为您提供根结构的输出,其中仅包含目录的文件夹名称 .
使用下面的脚本可以实现更具可读性和简单性的方法:
希望这可以帮助!
使用:
以下是哪一项
获取目标位置的目录列表:
Get-ChildItem \\myserver\myshare\myshare\ -Directory
仅提取目录名称:
Select-Object -Property name
将输出转换为CSV格式:
convertto-csv -NoTypeInformation
将结果保存到文件:
Out-File c:\temp\mydirectorylist.csv
接受的答案提到了
得到"raw string" . 但实际上将返回
Selected.System.IO.DirectoryInfo
类型的对象 . 对于原始字符串,可以使用以下内容:如果值连接到字符串,则差异很重要:
与
Select-Object
令人惊讶foo\@{FullName=bar}
与
ForEach
-operator预期:foo\bar
我的解决方案基于TechNet文章Fun Things You Can Do With the Get-ChildItem Cmdlet .
我在我的脚本中使用它,它运行良好 .
您将要使用 Get-ChildItem 以递归方式获取所有文件夹和文件 . 然后将输出管道传输到只接收文件的 Where-Object 子句中 .