1

I have this code that detects when the Excel.exe task is terminated:

:Excel
tasklist /nh /fi "imagename eq Excel.exe" | find /i "Excel.exe" >nul && (
goto Excel
) || (
goto Continue
)

:Continue

I want to modify this code to make it detect when I close an Excel workbook called

Database.xlsm

This is beacause I need to close ALL the Excel workbooks that I have open in the moment I execute the batch to make it go to

:Continue

I want it go to :Continue only when the Database.xlsm document is closed.

I hope you can help me. Thank you!

2 Answers2

2

You could try the following code, using findstr to filter for the window title of the process:

:Excel
timeout 1 /NOBREAK
tasklist /V /FI "IMAGENAME eq excel.exe" /FO LIST | findstr /RIXC:"Window Title: *Microsoft Excel - Database.xlsm" > nul && (
    goto :Excel
) || (
    goto :Continue
)
:Continue

You may need to adapt the partial string Microsoft Excel - Database.xlsm to your actual Excel window title.

I inserted timeout 1 /NOBREAK after line :Excel to avoid heavy CPU load.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • Why use `/R` (regex) and `/C` (literal search string) together? I would think that they were mutually exclusive. – lit Jun 21 '18 at 13:23
  • No, they are not; `/C` ensures the spaces to become part of the search string, `/R` defines the whole string as "regular expression" (as `/C` always defaults to literal mode); not using `/C` like `findstr [/R] "abc def"` actually defines two search strings `abc` and `def`; check out also [this thread](https://stackoverflow.com/q/8844868)... – aschipfl Jun 21 '18 at 13:39
0

try experiment with the windowtitle filter..

first use the /V flag to see teh window title.. then try filtering for it

ie

tasklist /v /FI "imagename eq Excel.exe" /FI "windowtitle eq Microsoft Excel - Database.xlsm" 

it may/may not work depending on exact excel and windows versions

or you can filter afterwards using your find

ShoeLace
  • 3,476
  • 2
  • 30
  • 44
  • The exact title in my case is "Database.xlsm - Excel" I tried this code but after close the Database.xlsm the CMD stuck in this part, so the command goes to :Excel all the time. Any other suggestion please? Thanks – Santiago Cuartas Rmrz Jun 21 '18 at 07:23