列出文件夹中匹配类型的文件

列出文件夹中匹配类型的文件

在 Windows Search 中,我可以用来kind:=music OR kind:=video获取文件夹/子文件夹中属于该类型媒体的所有结果。

有没有办法用命令行来复制这个dir,或者我必须对导出的数据进行一些奇特的操作种类图 Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\KindMap获取要搜索的文件扩展名列表。然后执行以下操作

最终,我需要一个 csv 或等效文件,其中包含所有符合搜索条件的文件

答案1

第一批尝试只成功了一半,因为 findstr 屈服于文档、音乐、图片和视频等种类的扩展数量。

编辑第二个工作版本,其中包含 Kinds 扩展的(丑陋的)临时文件

:: Q:\Test\2018\07\20\SU_1341778.cmd
:: DirKind.cmd music x:\path\folder
@Echo off

:: Possible Kind_ type strings
Set "Kinds=calendar communication contact document email link music picture"
Set "Kinds=%Kinds% playlist program recordedtv searchfolder video"

Echo=%Kinds%|Findstr /i "%~1" 2>&1>Nul ||(Echo invalid Kind:%1 &TimeOut 5&Exit /B 1)

Set "TempFile=%temp%\Kind_%~1.ext"
:: Build Kind_ string enumerating extensions
Set "Key=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\KindMap"
( For /f "tokens=1,3" %%A in (
    'reg query "%Key%"^|find "%~1"'
  ) do Echo=%%A
) > "%TempFile%"

Echo Dir all files of Kind %1 in folder "%~2"
Call Set "Kind=%%Kind_%1%%"
For /f "delims=" %%A in (
  'Dir /B /A-d "%~2\*" ^| Findstr /i /E /G:"%TempFile%" '
) Do Echo %%A

示例输出

> Q:\Test\2018\07\20\SU_1341778.cmd link "%USERPROFILE%\Desktop"
Dir all files of Kind link in folder "C:\Users\LotPings\Desktop"
Access 2016.lnk
ClassicStartMenu.exe - Verknüpfung.lnk
Excel 2016.lnk
FreeCommander XE.lnk
Microsoft Edge.lnk
OneNote 2016.lnk
Outlook 2016.lnk
PowerPoint 2016.lnk
Publisher 2016.lnk
shutdown.exe.lnk
UltraVNC Server.lnk
UltraVNC Settings.lnk
UltraVNC Viewer.lnk
WinDirStat.lnk
Windows 10-Update-Assistent.lnk
Word 2016.lnk

答案2

每当我在目录中寻找特定的东西时,我都喜欢这行代码

Get-ChildItem -Recurse -ErrorAction SilentlyContinue -Path C:\Users\ -Include*.ext

所以你的例子是

Get-ChildItem -Recurse -ErrorAction SilentlyContinue -Path C:\Users\[Uname]\Downloads -Include *.mp3,*.mov; # etc

来自 get-help 文本

-Path <String[]>
    Specifies a path to one or more locations. Wildcards are permitted. The default location is the current directory (.).

-Recurse [<SwitchParameter>]
    Indicates that this cmdlet gets the items in the specified locations and in all child items of the locations.

-Include <String[]>
    Specifies, as a string array, an item or items that this cmdlet includes in the operation. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as *.txt. Wildcards are permitted.

    The Include parameter is effective only when the command includes the Recurse parameter or the path leads to the contents of a directory, such as C:\Windows\*, where the wildcard character specifies the contents of the C:\Windows directory.

我将在脚本文件夹中运行搜索,查找 PowerShell 和 Bash 文件作为示例:

Get-ChildItem -Recurse -ErrorAction SilentlyContinue -Path .\Desktop\scripts\ -Include *.ps1,*.sh

结果:

    Directory: C:\Users\[user]\Desktop\scripts\Bash


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        1/22/2018   8:03 PM             84 runit.sh


    Directory: C:\Users\[user]\Desktop\scripts\PS


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        4/19/2018   9:04 PM            173 pass_d.ps1
-a----         9/7/2017   4:45 PM            345 refresh.ps1
-a----        5/11/2018   9:05 PM           1589 test.ps1
-a----        4/20/2018   8:55 PM            273 wifi_list.ps1

这不需要参数kind:=music,kind:=video,但您可以提供带有文件扩展名的星号作为参数的数组-Include *.mp3,*.mov,并且可以类似地搜索多个目录-Path /some/directory,some/directory2。运行相同的一行命令将把| Export-Csv [filename].csv结果保存为 .csv 文件。

提示:PowerShell 具有制表符补全功能,因此您不必输入每个参数。get-ch<tab>> Get-ChildItemGet-ChildItem -i<tab>> Get-ChildItem -Include,等等。

相关内容