Not another window to click…
This is a very short, but hopefully practical, post that stands as much an aide memoire for me as of benefit for you. Doing tasks that are repetitive or routine should be handled by the computer as far as it possible. Why waste time and effort when the computer can perform a task billions of times faster than we can, allowing us the opportunity to get on with something more useful.
Today I looking at automating a process involving Excel; some legacy code was rather sloppy at managing the instances of Excel it spawned and I ended up with a scrawling mess of Microsoft’s finest slowing my machine and draining memory which was becoming increasingly frustrating.
What should happen
At the code level, garbage collectors runs to sweep up any objects that are no longer required, but there’s nothing suitable at the OS level for us to tap into. We could look at using the Process ID perhaps and scripting something but that would be overkill for simple or one-off tasks. If all we want to do is to close all instances of a program then we can use the ‘taskkill’ command in Windows
There’s as a good article talking about the extra polish we can put on TaskKill so I won’t repeat them here but I suggest you check it out if you want to customise your code.
A note of caution should be taken than executing this code will shut all copies down without prompting to save.
Copy the following into a text editor and then save as YourFileName.bat or alternatively download the zip file from the link below
Windows Batch File:
taskkill /f /im Excel.exe
os.system(‘taskkill /f /im Excel.exe’)