打印目录树,但在Windows cmd上排除文件夹

edm*_*rto 6 windows powershell command-line

我想打印一个不包含文件夹的目录树.我已经知道打印树的基本方法如下:

tree /A > tree.txt
Run Code Online (Sandbox Code Playgroud)

我希望实现这样的目标:

tree /A [exclude folder node_modules] > tree.txt
Run Code Online (Sandbox Code Playgroud)

mkl*_*nt0 8

cmd.exe内部的tree命令不会支持不包括目录.

  • 如果你只需要排除名称的目录本身不能他们的整个子树(子目录和他们的后代),见nferrell的答案.

  • 如果您需要排除匹配给定名称的整个目录子树,则需要做更多工作 - 见下文.

下面是PowerShell的函数的源代码tree,其模拟的行为cmd.exetree指令的同时,也:

  • 提供按名称选择性排除子树
    注意:您可以指定多个名称分隔,,名称可以是通配符模式 - 请注意它们仅适用于目录名称,但不适用于完整路径.

  • 提供跨平台支持
    注意:请务必使用带有BOM的 UTF-8编码保存脚本,以使脚本无需正常运行-Ascii.

  • 提供切换-IncludeFiles到打印文件.

使用下面的函数加载,所需的命令如下所示:

tree -Exclude node_modules -Ascii > tree.txt
Run Code Online (Sandbox Code Playgroud)

运行tree -?Get-Help tree获取更多信息.


### `tree` source code (add to your `$PROFILE`, for instance; PSv4+):

function tree {
  <#
  .SYNOPSIS
  Prints a directory's subtree structure, optionally with exclusions.                        #'

  .DESCRIPTION
  Prints a given directory's subdirectory structure recursively in tree form,
  so as to visualize the directory hierarchy similar to cmd.exe's built-in
  'tree' command, but with the added ability to exclude subtrees by directory
  names.

  NOTE: Symlinks to directories are not followed; a warning to that effect is
        issued.

  .PARAMETER Path
  The target directory path; defaults to the current directory.
  You may specify a wildcard pattern, but it must resolve to a single directory.

  .PARAMETER Exclude
  One or more directory names that should be excluded from the output; wildcards
  are permitted. Any directory that matches anywhere in the target hierarchy
  is excluded, along with its subtree.
  If -IncludeFiles is also specified, the exclusions are applied to the files'
  names as well.

  .PARAMETER IncludeFiles
  By default, only directories are printed; use this switch to print files
  as well.

  .PARAMETER Ascii
  Uses ASCII characters to visualize the tree structure; by default, graphical
  characters from the OEM character set are used.

  .PARAMETER IndentCount
  Specifies how many characters to use to represent each level of the hierarchy.
  Defaults to 4.

  .PARAMETER Force
  Includes hidden items in the output; by default, they're ignored.

  .NOTES
  Directory symlinks are NOT followed, and a warning to that effect is issued.

  .EXAMPLE
  tree

  Prints the current directory's subdirectory hierarchy.

  .EXAMPLE
  tree ~/Projects -Ascii -Force -Exclude node_modules, .git

  Prints the specified directory's subdirectory hierarchy using ASCII characters
  for visualization, including hidden subdirectories, but excluding the
  subtrees of any directories named 'node_modules' or '.git'.

  #>

    [cmdletbinding(PositionalBinding=$false)]
    param(
      [parameter(Position=0)]
      [string] $Path = '.',
      [string[]] $Exclude,
      [ValidateRange(1, [int]::maxvalue)]
      [int] $IndentCount = 4,
      [switch] $Ascii,
      [switch] $Force,
      [switch] $IncludeFiles
    )

    # Embedded recursive helper function for drawing the tree.
    function _tree_helper {

      param(
        [string] $literalPath,
        [string] $prefix
      )

      # Get all subdirs. and, if requested, also files.
      $items = Get-ChildItem -Directory:(-not $IncludeFiles) -LiteralPath $LiteralPath -Force:$Force

      # Apply exclusion filter(s), if specified.
      if ($Exclude -and $items) {
        $items = $items.Where({ $name = $_.Name; -not $Exclude.Where({ $name -like $_ }, 'First') })
      }

      if (-not $items) { return } # no subdirs. / files, we're done

      $i = 0
      foreach ($item in $items) {
        $isLastSibling = ++$i -eq $items.Count
        # Print this dir.
        $prefix + $(if ($isLastSibling) { $chars.last } else { $chars.interior }) + $chars.hline * ($indentCount-1) + $item.Name
        # Recurse, if it's a subdir (rather than a file).
        if ($item.PSIsContainer) {
          if ($item.LinkType) { Write-Warning "Not following dir. symlink: $item"; continue }
          $subPrefix = $prefix + $(if ($isLastSibling) { $chars.space * $indentCount } else { $chars.vline + $chars.space * ($indentCount-1) })
          _tree_helper $item.FullName $subPrefix
        }
      }
    } # function _tree_helper

    # Hashtable of characters used to draw the structure
    $ndx = [bool] $Ascii
    $chars = @{
      interior = ('?', '+')[$ndx]
      last = ('?', '\')[$ndx]                                                                #'
      hline = ('?', '-')[$ndx]
      vline = ('?', '|')[$ndx]
      space = ' '
    }

    # Resolve the path to a full path and verify its existence and expected type.
    $literalPath = (Resolve-Path $Path).Path
    if (-not $literalPath -or -not (Test-Path -PathType Container -LiteralPath $literalPath) -or $literalPath.count -gt 1) { throw "'$Path' must resolve to a single, existing directory."}

    # Print the target path.
    $literalPath

    # Invoke the helper function to draw the tree.
    _tree_helper $literalPath

  }
Run Code Online (Sandbox Code Playgroud)

  • 这只是给了我太多参数 - &lt;FolderNameToBeExcluded&gt;; 有任何想法吗?尝试了 CMD 和 PowerShell。 (3认同)