1

I have the following code assigned to a VAIO button:

C:\Windows\System32\calc.exe

But each time I press the VAIO button, I get a new instance of the calculator in a new window. What would be the command line that brings the calculator to front if there is already a window of it?

BTW: I tried the following, but it didn't work. It just behaves as the VAIO button:

start /B calc       # Doesn't work

4 Answers 4

3

The batch file to check if calc.exe is already started doesn't have to be as elaborate as uprego stated.

But first... Do you really want to switch to the current calc.exe? Or do you just want one calc.exe running. If you don't need to switch to the current one you can just make a batch file which kills the first and runs a new instance of calc.exe:

calc.bat:

@echo off
taskkill /f /im "calc.exe"
start calc.exe

The only downside is you lose the current result in your previous calculation.

If you don't want the current calc.exe to be killed there are also solutions to bring it to the front (but they will be somewhat more complicated). Let me know if you really want to switch to the current calc.exe and I'll try to do some further searching.

1

You can do this pretty easily with AutoHotkey and this script:

<yourkey>::
IfWinExist Calculator
    {
        WinActivate
    }
    else
    {
        Run calc.exe
        WinWait Calculator
        WinActivate
    }

The only thing here is to modify <yourkey> with the code of your VAIO button. To find it:

  • right click on the autohotkey icon in the tray
  • choose open
  • press CtrlK (or select Key History and Script Info in the View menu)
  • press your VIAO button, then F5.

At the bottom, on the second column, you'll find the SC code of the button. Replace <yourkey> by SCxxx (so SC123 if your SC code was 123).

0

You can build a more elaborated Microsoft batch script and assign it to the VAIO button. This script can:

  • Check a flag file in the temporal files folder.
    • If it exists, bring the window visible in the screen if it's running, else open the program.
    • If it does not exist, create it, open the program and wait for termination, then delete the flag file.

To me here the unresolved primitives would be create temporal file, find program by process name and bring the window visible in the scren. Those should be easy to find in Stack Overflow or the Internet in general.

4
0

You can create a shortcut for calc.exe and place it anywhere on your hard drive, then call this shortcut from your hot key, you do not even need a batch. Windows will take care of it for you. Alas, if there is an open calc.exe it will be shown. So your C:\Windows\System32\calc.exe call would be C:\Windows\System32\calc.exe.lnk given you place the .lnk file in C:\Windows\System32\

2
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Nov 3, 2023 at 14:40
  • Some claim it will not work on later versions of Windows: A new instance of calc.exe will be launched instead. Feb 26 at 7:34

You must log in to answer this question.