Search code examples
batch-filewmic

Execute WMIC command in nested for-loop


this is my first time creating a batch file and I'm relatively new to this coding language. I wish to create a batch file that scans through all DLL files within a folder to retrieve and write the version info into a new text file. The nested for-loop functioned well but I couldn't get the WMIC command to execute, can you help?

Upon double-clicking to run the batch file, this is what I will get:

    Description = Invalid query
    'WMIC DataFile Where "Name='mydirecotry\myfile.dll'" Get Version'
    Node - myPCname

Here are the codes I wrote:

@echo off

SETLOCAL ENABLEDELAYEDEXPANSION 

FOR %%I in (D:\mydirecotry\*.dll*) DO (

    SET Location='WMIC DataFile Where "Name='%%I'" Get Version'
    ECHO !Location!
    
    For /F "Tokens=1* Delims=" %%A In (!Location!) Do (      <---Where i think it went wrong
    
        For /F "Tokens=*" %%B In ("%%~A") Do Set "pver=%%B" 
        
        ECHO %%I - %pver% >> test.txt
    )
)
pause

Thanks in advance.


Solution

  • As the majority of my earlier comment is already within a supplied answer, I have decided to provide the alternative method I used within that comment, as an answer.

    For the task you've laid out in your question, you should be able to have a single line , with no set or for commands, just containing:

    @"%__AppDir__%wbem\WMIC.exe" /Output:"test.txt" DataFile Where "Drive='D:' And Path='\\mydirecotry\\' And Extension='dll'" Get Name, Version