-1

I have a batch program that opens up programs and files in specific order so I can have everything open up without even a single click when I sign in to the computer. This script will run automatically for both cold boots and by adapting the answer it will be possible to run the script to simply open windows not already opened without creating duplicates.

This bit of code works perfectly for almost everything I need by determining if a program is already running or not:

SETLOCAL EnableExtensions
set EXE=notepad++.exe
FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% goto FOUND
start C:\MEDIA\OFFICE\Notepad++\notepad++.exe
goto FIN
:FOUND
echo Notepad++ is already running.
:FIN

However I am not sure how to use this method (or what alternative methods exist) that will tell me if the following specific shell folder is open or not.

The desired/working path (My Documents) is:

start %windir%\explorer.exe shell:UsersFilesFolder\JAB Creations

The absolute path (My Computer) is:

D:\My Documents\JAB Creations

Important Clarifications

  • The directory is a symbolic link, that command if you are curious is Mklink /J "C:\Users\John\JAB Creations" "D:\My Documents\JAB Creations".
  • I do not want to use the absolute path, I want the directory opened under My Documents, not My Computer.
  • I have removed the garbage entries in Windows Explorer such as Libraries and Quick Access and I use My Documents (which is now called User Files). This command opens the directory via My Documents instead of having it listed under My Computer in example.
  • If you are testing you will need to enable Expand to open folder and Show all folders options in the Folder Options window under the View tab otherwise useful directories are hidden by default.

Important Prerequisites

First fix the folder options otherwise an attempt is pointless:

Fix Folder Options

Secondly this is what should visually occur:

Correct Windows Explorer Behavior

John
  • 1
  • 13
  • 98
  • 177
  • 1
    If you use it to "have everything open up without even a single click when I sign in to the computer", how can your programs already be open? When I sign in, the desktop and taskbar are always empty, which is a reasonable indication that I am not running any windowed programs. – Compo Jan 29 '20 at 11:45
  • What do you mean by *"a shell folder is open"*? do you want an Explorer window to show the content of this shell folder? – aschipfl Jan 29 '20 at 11:57
  • @aschipfl The shell folder is the folder being displayed via `My Documents` versus a regular folder open in `explorer.exe`. There are numerous copies of `explorer.exe` open simultaneously, the goal is to determine if the shell folder I specified is open or not in the context specified (listed as a child of `My Documents`, not listed as a child of `My Computer`). – John Jan 29 '20 at 12:22
  • Not only do I think you cannot do it via standard batch file commands, it is an unnecessary waste of time trying. If there are two of them open, simply close one of the windows. You have already informed us that there are already multiple explorer.exe processes running, one extra one isn't going to impact performance during the time it is auto opened and the time you take to click on the close button. Also, as already mentioned in my previous comment, when you sign in, why would your specific windows already be open? – Compo Jan 29 '20 at 13:04
  • 1
    after signing in who opens your folders before you run the script? Anyway there's already a duplicate question for [opening a folder explorer if it wasn't opened](https://stackoverflow.com/q/48084216/995714). But I don't think you can check whether it's opened in the context of My Computer or My Document, unless you attach a debugger to explorer.exe – phuclv Jan 29 '20 at 13:49
  • 2
    John, I know enough about how explorer works, and have read your question in its entirety. You've posted irrelevant working code and along side it, you've given us unnecessary information regarding symbolic links and renamed/redirected paths. It's strange that myself and @phuclv seem to have the same lack of comprehension, isn't it? We both noted that upon sign in, your specific window processes should not already be opened, and that it cannot be done with standard batch file commands. You're seeking an off topic "How can I?" answer, but StackOverflow is not about general programming. – Compo Jan 29 '20 at 14:33
  • @Compo Signing in may be done *either* after the computer has been off/cold or idle and locked to the Windows user screen. The point of this modification is to have one consolidated script that handles either scenario. This is adaptive/dynamic programming and it's also a very welled defined question. Do either of you or anyone else have recommendations for debuggers? If so I'll gladly look in to them and if relevant update the question accordingly. I believe the issue here is one of communication, the technical depth *should* elude the comprehension of what you have questioned... – John Jan 29 '20 at 14:53
  • If your PC is logged in from idle, e.g. locked, standby, hibernate, why would you be opening everything back up again. If those processes are supposed to be opened at all times, for your particular workflow, you wouldn't have closed one or all of them before going into idle mode, would you? If you specifically closed one or more of them because they were unnecessary, when you log back in, you shouldn't immediately require those unnecessary processes to be opened again, as your environment just before idle, should be the same, just after returning from idle. – Compo Jan 29 '20 at 15:22
  • 1
    Your question is off topic. It is a general programming one, and this site provides assistance with a specific issue with your supplied code. It is also off topic to request recommendations for software, i.e. debuggers. – Compo Jan 29 '20 at 15:26
  • I absolutely comprehend that your question is "how do I check whether Windows Explorer is open with a specific context and in a specific location from a [tag:batch-file] in [tag:windows-10]". Irrespective of the fact that both myself and @phuclv have answered that; your question is off topic. This site is to help you with a specific issue with your provided code. Your issue is that you have not researched, written, or provided that code. The site you should have posted your general programming, "how do I?" question on, is [Super User](https://superuser.com/questions/ask), not StackOverflow. – Compo Jan 29 '20 at 17:38
  • 1
    Having the last word, does not trump the site guidance! – Compo Jan 31 '20 at 12:25

1 Answers1

1

In Windows 10 explorer.exe feeds a window title of N/A by default, but there is a work-around if you are launching these explorer instances.

Opening explorer regularely (by running explorer.exe or opening the icon) will result in the following title query:

C:\Users\***>tasklist /fi "imagename eq explorer.exe" /fo list /v
Image Name:   explorer.exe
PID:          5320
Session Name: Console
Session#:     1
Mem Usage:    141,516 K
Status:       Running
User Name:    ***\***
CPU Time:     0:31:41
Window Title: N/A

While launching explorer.exe with the title flag (don't even need to specify a string) will force explorer to keep its title value accurate. So in your code above, call explorer.exe like so:

C:\Users\***>explorer.exe /Title

This will result in our title query returning the actual title (and therefore current open folder):

Image Name:   explorer.exe
PID:          13888
Session Name: Console
Session#:     1
Mem Usage:    45,964 K
Status:       Running
User Name:    ***\***
CPU Time:     0:00:00
Window Title: Documents

So now all you need to do is query all instances of explorer with tasklist /fi "imagename eq explorer.exe" /fo list /v and parse the returned data for the folder name you wish to identify, like below (I check for "Documents" in the title):

C:\Users\***>tasklist /fi "imagename eq explorer.exe" /fo list /v | find "Documents"
Window Title: Documents

C:\Users\***>

The above query will return 0 code or 1 code for found/not found. You can work with this to have your code check if a specific folder is open in windows explorer(.exe). I hope this has given you a good grasp on Windows 10's quirky explorer usage!

Compo
  • 36,585
  • 5
  • 27
  • 39
STR4NG3R
  • 56
  • 1
  • 3
  • the thing is, if the folder was opened previously without `/Title` then your method won't work. Using [`Shell.Windows().item().LocationUrl`](https://stackoverflow.com/a/56346797/995714) is more reliable. And your solution still doesn't differentiate the context of the opened folder (My Documents or My Computer) – phuclv Jan 30 '20 at 01:24
  • Interesting! I tested this out and I like the idea though after attempting to use the `/title` flag and combine the shell command even with quotes in varying order it always either ignored the shell command or opened another command prompt. How do I combine `/title` with `shell:UsersFilesFolder\JAB Creations` when running `explorer.exe`? – John Jan 30 '20 at 22:06
  • Hi John, I tried launching explorer via shell and see what you mean about it just opening a new command prompt. Odd. My suggested solution (and simplification) of your folder opening line below. Change `start %windir%\explorer.exe shell:UsersFilesFolder\JAB Creations` to `"explorer "//" /Title"`. the "//" is shorthand for "My Documents" default location. – STR4NG3R Jan 31 '20 at 15:46
  • Correction: `explorer "//" /Title` (notice I removed the outermost quotes) – STR4NG3R Jan 31 '20 at 15:53
  • `start %windir%\explorer shell:UsersFilesFolder\JAB Creations "//" /Title` does not work. I added clarifications about the "Show All Folders" and "Expand to Current Folder" in the Folder Options in the question near the bottom. Can you please try to adjust it and test with say `shell:UsersFilesFolder\Documents` please? I feel we're really close and I'd like to be able to accept the answer. :D – John Feb 03 '20 at 22:49
  • You said in your main post that the desired working folder was "My Documents". With my example, the `//` replaces 'shell:UsersFilesFolder\Documents'. Any user who runs `explorer "//" /Title` or `startexplorer "//" /Title` will get an explorer window that has a queryable title and goes straight to their "Documents" folder. In your latest response, you are essentially telling `start` to launch explorer and pre-load both `shell:UsersFilesFolder\JAB Creations` AND `Documents`, it won't know what to do with the extra variable. What is the location of JAB Creations? is it user/Jab or /user/docs/jab? – STR4NG3R Feb 03 '20 at 23:32
  • I've updated my answer to *visually* clarify. Your answer opens a Microsoft trash "Documents" under My Computer. My Documents folders do *not* belong under My Computer. You'll need to fix the Folder Options first (no reboot/log off/on needed). You may also need to right-click on your desktop, *Personalize* and show the "User Files" shortcut or (not sure) just **RUN** `%windir%\explorer shell:UsersFilesFolder`. – John Feb 04 '20 at 19:28