command file: Opening a window only if not already opened


  1. Posts : 20
    Windows 10 Home 64
       #1

    command file: Opening a window only if not already opened


    I am on a Windows 10 Home PC, ver. 21H1. I am a newbie to command files. I've written one to execute a program, and then display my local Temp folder. Unfortunately, if the Temp folder already is displaying in Explorer, the command opens a new instance of that window. How would I modify it to open the Temp window only if it has not already been opened, and otherwise, just give the open Temp window the focus? Thank you.

    Here's my code:

    @echo off
    "C:\Program Files\progManufacturer\progName\prog.exe" -l
    %SystemRoot%\explorer.exe %temp%
    exit
      My Computer


  2. Posts : 3,274
    Win10
       #2

    I don't if this will work for you as I don't know the behaviour of your prog.exe ( seems to work for notepad when I tried it as replacement for prog.exe )

    Batch file to start prog.exe, and then Explorer at %temp%, but only if it is not already open at %temp%

    Code:
    rem @echo off
    
    start "C:\Program Files\progManufacturer\progName\prog.exe" -l
    start %temp%
    timeout 2
    Powershell.exe -NoProfile -ExecutionPolicy Bypass -WindowStyle Minimized -command "Invoke-Item $env:TEMP"
    
    pause
    (If there are no errors, you can remove the rem in front of the echo command and the pause at the end. - if it doesn't work, hopefully one of the Gurus on Tenforums can delve deeper into it).
      My Computers


  3. Posts : 20
    Windows 10 Home 64
    Thread Starter
       #3

    Thank you so much! It worked (although, frankly, I don't understand the powershell command). Here's my completed code:

    "C:\Program Files\myProgManufacturer\MyProg\MyProg.exe" -l
    start %temp%
    timeout 1
    Powershell.exe -NoProfile -ExecutionPolicy Bypass -WindowStyle Minimized -command "Invoke-Item $env:TEMP"
    exit
      My Computer


  4. Posts : 3,274
    Win10
       #4

    If your batch file works without the Powershell command, you can remove that line. (I had only used it whilst testing a loss of focus on some occassions - basically it is the powershell equivalent of start %temp% ).
    Last edited by das10; 11 Jun 2021 at 03:18.
      My Computers


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 20:45.
Find Us




Windows 10 Forums