Search code examples
windowscmdwmic

Print File Descriptions from all files in a folder, wmic command


This works

for /r "D:\FOLDER\" %i in (*) do @echo %i

But this doesn't

for /r "D:\FOLDER\" %i in (*) do @wmic datafile where name=%i get Description

using reference from first command, I wrote wmic command. but It doesn't work.

gives multiple errors...

Node - <MACHINE NAME>
ERROR:
Description = Invalid query

What's the problem here. How do I print file descriptions of all files in a folder.

Update: Added '' around %i

for /r "D:\FOLDER\" %i in (*) do @wmic datafile where name='%i' get Description

Now it gives me No Instance(s) Available. error?

Question: Why wmic datafile get description doesn't give file description in file properties dialog box? How to get the file description.

enter image description here


Solution

  • You will get the same output and a lot faster using

    dir /s /b "D:\FOLDER\*"
    

    But, as the question is how to do it with queries to wmic for each file

    @echo off
        setlocal enableextensions disabledelayedexpansion
    
        for /r "d:\folder" %%a in (*) do (
            set "folder=%%~pa"
            for /f "tokens=* delims=." %%x in (".%%~xa") do (
                setlocal enabledelayedexpansion
                for %%b in ("!folder:\=\\!") do (
                    endlocal 
                    wmic datafile where ^
                      "drive='%%~da' and path='%%~b' and fileName='%%~na' and extension='%%~x'" ^
                    get description | find ":"
                )
            )
        )
    

    This code handles two "problems" in the wmic queries: paths need to have backslashes doubled and file extension are stored without starting dot.

    edited As it seems that the OP needs metadata from the file contents and not the data available from wmic,

    @if (@this==@isBatch) @then 
    @echo off
        setlocal enableextensions disabledelayedexpansion
    
        call :listFilesProperties "d:\folder"
        goto :eof
    
    :listFilesProperties folder    
        cscript //nologo //E:JScript "%~f0" "%~f1"
        goto :eof
    
    @end  
    var folderNameSpace = WScript.CreateObject("Shell.Application").NameSpace(WScript.Arguments.Item(0));
    
        for (
            var fileEnum = new Enumerator(folderNameSpace.Items());
            !fileEnum.atEnd();
            fileEnum.moveNext()
        ){
            var fileName = fileEnum.item();
            var fullPath = folderNameSpace.GetDetailsOf(fileName, 180);
            var title  = folderNameSpace.GetDetailsOf(fileName, 21);
            WScript.Echo( fullPath + '|' + title );
        };
    

    This is an hybrid batch/jscript file that will retrieve the properties of the files in the indicated folder, in this case the full path and file title.