Powershell-查询当前文件目录层级结构

wenzhongxiang
wenzhongxiang
管理员
304
文章
0
粉丝
Windows Powershell字数 497阅读模式

日常工作中我们往往有需要导出当前共享环境或磁盘文件目录层级结构等的需求,最早在目录少的情况下我们使用CMD下tree 命令可以很清晰的看到目录、文件层级结构,那么我们又如何通过powershell直观显示或导出某文件目录或盘符目录层级结构呢?

DOS下查看目录、文件结构:文章源自小温Talk.听闻 | 小温Talk-https://azureyun.azurewebsites.net/2574.html

tree /?
以图形显示驱动器或路径的文件夹结构。
TREE [drive:][path] [/F] [/A]
/F   显示每个文件夹中文件的名称。
/A   使用 ASCII 字符,而不使用扩展字符。文章源自小温Talk.听闻 | 小温Talk-https://azureyun.azurewebsites.net/2574.html

image文章源自小温Talk.听闻 | 小温Talk-https://azureyun.azurewebsites.net/2574.html


Powershell查看目录、文件结构:文章源自小温Talk.听闻 | 小温Talk-https://azureyun.azurewebsites.net/2574.html

其实我们通过powershell命令也可以搭配tree命令使用,简单操作如下:文章源自小温Talk.听闻 | 小温Talk-https://azureyun.azurewebsites.net/2574.html

Get-ChildItem D:\SW_Office_Plus |tree /f
Get-ChildItem D:\SW_Office_Plus |tree /A 文章源自小温Talk.听闻 | 小温Talk-https://azureyun.azurewebsites.net/2574.html

Get-ChildItem :获取一个或多个指定位置中的项和子项。文章源自小温Talk.听闻 | 小温Talk-https://azureyun.azurewebsites.net/2574.html

获取当前目录下文件夹名称:文章源自小温Talk.听闻 | 小温Talk-https://azureyun.azurewebsites.net/2574.html

Get-ChildItem D:\SW_Office_Plus | ?{$_.psiscontainer -eq $true}文章源自小温Talk.听闻 | 小温Talk-https://azureyun.azurewebsites.net/2574.html

获取当前目录下文件名称:
Get-ChildItem D:\SW_Office_Plus | ?{$_.psiscontainer -eq $false}文章源自小温Talk.听闻 | 小温Talk-https://azureyun.azurewebsites.net/2574.html

image

接下来进入我们今天的主题内容,如何查看当前目录下文件层级,具体命令如下:

Get-ChildItem -Recurse -Directory -Depth 3 |select FullName

Get-ChildItem D:\SW_Office_Plus  -Recurse -Directory -Depth 3 |select Fullname

image

如果需要对结果进行导出,可通过如下命令操作:

Get-ChildItem -Recurse -Directory -Depth 3 |select FullName | Export-Csv d:\fullname.csv -Encoding UTF8 –NoTypeInformation


PS.补充:导出文件、文件目录名称、创建时间、格式等等信息:

Get-ChildItem -Path D:\SW_Office_Plus -Recurse |`
foreach{
$Item = $_
$Type = $_.Extension
$Path = $_.FullName
$ParentS = ($_.Fullname).split("\")
$Parent = $ParentS[@($ParentS.Length - 2)]
$ParentPath = $_.PSParentPath
$ParentPathSplit = ($_.PSParentPath).split("::")
$ParentPathFinal = $ParentPathSplit[@($ParentPathSplit.Length -1)]
#$ParentPath = [io.path]::GetDirectoryName($myDirectory)
$Folder = $_.PSIsContainer
$Age = $_.CreationTime
$Path | Select-Object `
    @{n="Name";e={$Item}},`
    @{n="Created";e={$Age}},`
    @{n="Folder Name";e={if($Parent){$Parent}else{$Parent}}},`
    @{n="filePath";e={$Path}},`
    @{n="Extension";e={if($Folder){"Folder"}else{$Type}}},`
    @{n="Folder Name 2";e={if($Parent){$Parent}else{$Parent}}},`
    #@{n="Folder Path";e={$ParentPath}},`
    @{n="Folder Path 2";e={$ParentPathFinal}}`
}| Export-Csv d:\Folder.csv -Encoding UTF8 -NoTypeInformation

image

导出后格式如下,可自行筛选,该脚本内容具体可参考该link

image

文章末尾固定信息

 
  • 本文由 wenzhongxiang 发表于03/12/2019 21:27:32
  • 转载请务必保留本文链接:https://azureyun.azurewebsites.net/2574.html