如何在PowerShell中递归检索具有特定扩展名的任何文件? [英] How to retrieve recursively any files with a specific extensions in PowerShell?

查看:92
本文介绍了如何在PowerShell中递归检索具有特定扩展名的任何文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于特定文件夹,我需要列出扩展名为 .js 的所有文件,即使嵌套在任何级别的子文件夹中也是如此.

For a specific folder, I need to list all files with extension .js even if nested in subfolders at any level.

输出控制台的结果应该是一行一行没有扩展名的文件名列表,以便轻松复制并粘贴到另一个应用程序中.

The result for the output console should be a list of file names with no extension line by line to be easily copy and pasted in another application.

目前我正在尝试这个,但在输出控制台中我得到了几个元信息而不是一个简单的列表.

At the moment I am trying this, but in output console I get several meta information and not a simple list.

Get-ChildItem -Path C:\xx\x-Recurse -File | sort length –Descending

你能给我一些提示吗?

推荐答案

如果不需要按长度排序,您可以使用 -Name 参数让 Get-ChildItem 只返回名称,然后使用 [System.IO.Path]::GetFileNameWithoutExtension() 去掉路径和扩展名:

If sorting by Length is not a necessity, you can use the -Name parameter to have Get-ChildItem return just the name, then use [System.IO.Path]::GetFileNameWithoutExtension() to remove the path and extension:

Get-ChildItem -Path .\ -Filter *.js -Recurse -File -Name| ForEach-Object {
    [System.IO.Path]::GetFileNameWithoutExtension($_)
}

如果需要按长度排序,请删除 -Name 参数并输出每个 FileInfo 对象的 BaseName 属性.您可以将输出(在两个示例中)通过管道传输到 clip,以将其复制到剪贴板:

If sorting by length is desired, drop the -Name parameter and output the BaseName property of each FileInfo object. You can pipe the output (in both examples) to clip, to copy it into the clipboard:

Get-ChildItem -Path .\ -Filter *.js -Recurse -File| Sort-Object Length -Descending | ForEach-Object {
    $_.BaseName
} | clip

如果您想要完整路径,但没有扩展名,请将 $_.BaseName 替换为:

If you want the full path, but without the extension, substitute $_.BaseName with:

$_.FullName.Remove($_.FullName.Length - $_.Extension.Length)

这篇关于如何在PowerShell中递归检索具有特定扩展名的任何文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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