Search code examples
batch-filewindow

Get a list of all open windows using a batch file


New to batch files but so far have managed to get some handy tools working. But I have now got stuck on something I taught would be trivial.

I'am now working on a tool that needs to know what windows file explorer has open. I have had a look and all the examples I coming across are about either opening directories or listing content of specified directory. The closest examples I have found for what I'm trying to do are in higher level languages.


Solution

  • This will only list the last active Explorer window:

    @echo off
    setlocal enableDelayedExpansion
    for /f "skip=1 delims=" %%a in ('tasklist /fi "imagename eq explorer.exe" /v /fo:csv') do (
        set "info=%%a" & set "info=!info:","=^|!" & set info=!info:~1,-1!
        for /f "delims=| tokens=9" %%b in ("!info!") do if not "%%b"=="N/A" echo %%b
    )
    pause
    

    To list all open Explorer windows you'd have to use cmdow utility:

    for /f "tokens=8*" %%a in ('cmdow /t /b') do if %%a==explorer echo %%b