List files in folder that match kind

1

In Windows Search I can use kind:=music OR kind:=video to get all results in a folder/ subfolders which are that type of media.

Is there a way to replicate this with a command like dir on the command line, or will I have to do some fancy manipulation with my exported kind map Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\KindMap to get a list of file extensions to search against. Then do something like this

Ultimately I'm after a csv or equivalent with all files matching the search criteria

Greedo

Posted 2018-07-20T08:07:41.090

Reputation: 365

Answers

2

First batch attempt working only halfway as findstr capitulates on the sheer number of extensions in Kinds documents,music,picture and video.

EDIT 2nd working version with an (ugly) temporary file containing the Kinds extensions

:: 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

Sample output

> 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

LotPings

Posted 2018-07-20T08:07:41.090

Reputation: 6 150

1

Whenever I'm looking for something specific in my directories I like this one liner

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

so an example in your case would be

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

From the get-help text

-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.

I'll run a search on a scripts folder looking for PowerShell and Bash files as an example:

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

Result:

    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

This won't take a kind:=music,kind:=video argument, but you can provide asterisks with file extensions as an array for the -Include *.mp3,*.mov argument and can search multiple directories similarly -Path /some/directory,some/directory2. Running that same one line command piped to | Export-Csv [filename].csv will save your results as a .csv file.

Tip: PowerShell has tab completion available so you don't have to type out every argument. get-ch<tab> > Get-ChildItem, Get-ChildItem -i<tab> > Get-ChildItem -Include, etc.

saniboy

Posted 2018-07-20T08:07:41.090

Reputation: 41