Question

I am running the following batch script:

@echo off
tasklist /nh /fi "Windowtitle eq Export to PDF - DOORS" | find /i "Export to PDF - DOORS" >nul && (
echo PDF is running
) || (
echo PDF is not running
)

This only echos "PDF is running" if the window is currently active. Perhaps I am using the wrong command (tasklist). Is there a way to find in the full list of open windows?

Was it helpful?

Solution

I was able to get what I needed with a VB Script (Thanks @JoshGuzman for the idea):

Set Word = CreateObject("Word.Application")
Set Tasks = Word.Tasks

For Each Task in Tasks
  If Task.Visible Then
    If Task.name = "Export to PDF - DOORS" Then
      Wscript.Echo "PDF is Running"
    Else 
      Wscript.Echo "PDF is not Running"
    End If
  End If
Next

Word.Quit

Then call the VB script with wscript myScript.vbs from the command prompt or a batch file.

OTHER TIPS

If you are willing to use Microsoft's PowerShell instead of cmd.exe (it also works on my old Windows XP, I just had to install it manually; on newer Windows versions it is preinstalled), you can install WASP as a snap-in and then do this:

Select-Window | Format-Table processid,processname,title -AutoSize

AUSFÜHRLICH: Enumerating all windows

ProcessId ProcessName  Title
--------- -----------  -----
     7452 powershell   Windows PowerShell V2 (CTP3)
     2688 chrome       cmd - tasklist show all windows - Stack Overflow - Google Chrome
     2688 chrome       List all open window titles - PowerShellCommunity.org - Windows PowerShell Discussion Forums ...
     3572 TOTALCMD     Total Commander 8.0 - Scrum-Master.de  Inh. Alexander Kriegisch
     4152 eclipse      Java - dummy2/src/de/scrum_master/aop/log4j/Log4jAspect.aj - Eclipse Platform - Java, Scala, ...
     5608 Foxit Reader quick5A4.pdf - Foxit Reader
     2812 TextPad      TextPad - [C:\Dokumente und Einstellungen\Robin\Eigene Dateien\java-src\dummy2\bin\log4j.prop...

As you can see, both chrome windows are listed whereas the on-board command Get-Process only lists one window per process, just like tasklist in cmd.exe:

Get-Process | Where {$_.mainwindowtitle} | Format-Table id,name,mainwindowtitle -AutoSize

  Id Name         MainWindowTitle
  -- ----         ---------------
2688 chrome       cmd - tasklist show all windows - Stack Overflow - Google Chrome
4152 eclipse      Java - dummy2/src/de/scrum_master/aop/log4j/Log4jAspect.aj - Eclipse Platform - Java, Scala, Aspec...
5608 Foxit Reader quick5A4.pdf - Foxit Reader
7452 powershell   Windows PowerShell V2 (CTP3)
2812 TextPad      TextPad - [C:\Dokumente und Einstellungen\Robin\Eigene Dateien\java-src\dummy2\bin\log4j.properties]
3572 TOTALCMD     Total Commander 8.0 - Scrum-Master.de  Inh. Alexander Kriegisch

This is the solution...

@echo off
tasklist /FI "WINDOWTITLE eq Export to PDF - DOORS" | find /i "Image Name" >nul && (
echo PDF is running
) || (
echo PDF is not running
)

This is what happens

tasklist /FI "WINDOWTITLE eq myscript.bat - Notepad"

If tasklist found any matches then will output:

Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
notepad.exe                   3212 Console                    1     22,704 K

So you have to use find /i "Image Name" because Image Name will appear if myscript.bat - Notepad Windows Title exist.

Oh, I almost forgot if not find matches then the output would:

INFO: No tasks are running the specified criteria Which match.

Note that the Words "Image Name" isn't in this output.

Hope this help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top