Windows – How to remove the last blank line of a wmic command

batchcommand linewindows

In a batch I want to use:

FOR /F "usebackq skip=1" %I IN (wmic datafile where Name^="D:\\TOTO.EXE" get Version) DO (ECHO %I)

and want to restrict the ECHO, not having an ECHO is on. for the last line that is empty.

How can I do that?

Best Answer

  • I don't want to have an ECHO for the last line that is empty

    FOR /F "usebackq skip=1" %I IN (wmic datafile where Name^="D:\\TOTO.EXE" get Version) DO (ECHO %I)
    

    There are a few things you need to change for the above to work in a batch file:

    1. Replace each single % with a double %%.

    2. Add backticks (`) around the command that is executed by for.

    3. Pipe | the wmic command to findstr /r /v "^$" to remove the blank lines (which will remove the ECHO is on. output.

    Putting it all together, use the following batch file (test.cmd):

    @echo off
    setlocal
    for /f "usebackq skip=1" %%i in (`wmic datafile where Name^="D:\\TOTO.EXE" get Version ^| findstr /r /v "^$"`) do (echo %%i)
    endlocal
    

    You will get a single line output, similar to the following:

    F:\test>test
    6.1.7600.16385