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


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

    DOS下查看目录、文件结构:

    tree /?
    以图形显示驱动器或路径的文件夹结构。
    TREE [drive:][path] [/F] [/A]
    /F   显示每个文件夹中文件的名称。
    /A   使用 ASCII 字符,而不使用扩展字符。

    image


    Powershell查看目录、文件结构:

    其实我们通过powershell命令也可以搭配tree命令使用,简单操作如下:

    Get-ChildItem D:SW_Office_Plus |tree /f
    Get-ChildItem D:SW_Office_Plus |tree /A

    Get-ChildItem :获取一个或多个指定位置中的项和子项。

    获取当前目录下文件夹名称:

    Get-ChildItem D:SW_Office_Plus | ?{$_.psiscontainer -eq $true}

    获取当前目录下文件名称:
    Get-ChildItem D:SW_Office_Plus | ?{$_.psiscontainer -eq $false}

    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

    欢迎关注微信公众号:小温研习社

  • 相关阅读:
    CSS 文档流布局以及盒子模型
    CSS样式继承和样式权重
    CSS选择器餐厅练习
    node.js读写json文件
    C#单例模式的2种实现方式,Lazy模式和双检锁模式
    The Entity Framework provider type 'MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.EntityFramework, Version=8.0.18.0,
    mysql主从配置
    nginx 限制ip并发数,nginx限制IP连接数的范例参考
    certbot 调用cloudflare api申请证书
    proxy_pass url 反向代理的坑
  • 原文地址:https://www.cnblogs.com/wenzhongxiang/p/10519786.html
Copyright © 2020-2023  润新知