PowerShell 连接 Get-ChildItem 的输出 [英] PowerShell concatenate output of Get-ChildItem

查看:34
本文介绍了PowerShell 连接 Get-ChildItem 的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用正则表达式搜索文件的工作脚本.该脚本为每个文件返回 2 行:父文件夹 naùe 和文件名(与正则表达式匹配).

I have a working script that searches for files with a regular expression. The script returns 2 lines per file: the parent folder naùe, and the filename (matching the regex).

Get-ChildItem -Path "D:\test\" -Recurse -File |
  Where-Object { $_.BaseName -match '^[0-9]+$' } |
    ForEach-Object { $_.FullName -Split '\',-3,'SimpleMatch' } | 
     select -last 2 | 
       Out-File "D:\wim.txt"

某个系统需要将输出放在一行上,例如与 \ 或类似字符连接.我怎样才能做到这一点?

A certain system needs to have the output on one line, concatenated with for example \ or a similar character. How can I achieve this please ?

非常感谢!

推荐答案

Get-ChildItem -Path D:\test -Recurse -File | 
  Where-Object { $_.BaseName -match '^[0-9]+$' } | 
    ForEach-Object { ($_.FullName -split '\\')[-2,-1] -join '\' } |                          #'
      Out-File D:\wim.txt

  • ($_.FullName -Split '\\')[-2,-1] 从文件路径中提取最后 2 个组件
  • -join '\' 将它们重新连接在一起.
    • ($_.FullName -Split '\\')[-2,-1] extracts the last 2 components from the file path
    • and -join '\' joins them back together.
    • 请注意,除了行格式问题之外,您的原始命令没有按预期工作,因为 |select -last 2 应用于整体输出,而不是每个匹配文件;因此,即使有多个匹配的文件,你也只会得到最后一个匹配文件的父目录和文件名.

      Note that, aside from the line-formatting issue, your original command does not work as intended, because | select -last 2 is applied to the overall output, not per matching file; thus, even if there are multiple matching files, you'll only ever get the parent directory and filename of the last matching file.

      因此,上面的命令直接在 ForEach-Object 块中提取最后 2 个 \ 分隔的路径组件-split 操作,以便 每个文件返回 2 个(连接的)组件.

      The command above therefore extracts the last 2 \-separated path components inside the ForEach-Object block, directly on the result of the -split operation, so that 2 (joined) components are returned per file.

      顺便说一句,$_.FullName -split '\', -3, 'SimpleMatch' 中的 -3 不是提取最后3个令牌;它目前被有效地视为与 0 相同,这意味着返回 所有 结果标记;鉴于 -split 默认使用 regexes,并且表示文字 \ 需要转义为 \\, $_.FullName -split '\', -3, 'SimpleMatch'$_.FullName -split '\\' 是一样的,就是上面的解决方案使用的.

      As an aside, the -3 in $_.FullName -split '\', -3, 'SimpleMatch' does not extract the last 3 tokens; it is currently effectively treated the same as 0, meaning that all resulting tokens are returned; given that -split defaults to using regexes, and representing a literal \ requires escaping as \\, $_.FullName -split '\', -3, 'SimpleMatch' is the same as $_.FullName -split '\\', which is what the solution above uses.

      请注意,有一个绿灯-split 增强 将给出负面 <Max-substrings> values 未来的新含义,将当前的正数逻辑类似地应用于输入字符串的end;例如,-3 表示:返回最后 2 个组件以及它们之前输入字符串的剩余部分(结果标记仍然从左到右报告).

      Note that there is a green-lit -split enhancement that will give negative <Max-substrings> values new meaning in the future, applying the current positive-number logic analogously to the end of the input string; e.g, -3 would mean: return the last 2 components plus whatever is left of the input string before them (with the resulting tokens still reported from left to right).

      这篇关于PowerShell 连接 Get-ChildItem 的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆