2

I'd like to write a .bat file that opens multiple folders if none of the aforementioned folders or files are open, and closes all of the multiple folders if any of them are open in Windows Explorer or being run by Windows.

I think I can solve the conditional problem with this method mentioned on the cmd.exe: complex conditions? post but I cannot get it to work for my needs for everything I've tried.

set COND=
if COND1 set COND=1
if COND2 set COND=1
if defined COND ...

I am not sure how I check for what folders are open in Windows Explorer but I found the close solution on the cmd: Open explorer window of folder if not already open post but I still cannot work out the correct logic to use after several tries.

Is there a simpler way to check for whether a folder is open, or a process is run?

3

Below is a batch file that you can set the values enclosed by double quotes and separate by commas to be the paths to the folders you want to open (i.e. <"FolderPathValue1">,<"FolderPathValue1">) in the SET oPenArray= batch variable.

This does use some dynamic PowerShell logic to get the folders that are opened and put those into a file so the batch FOR logic can iterate and use conditional logic accordingly.

To understand the PowerShell logic better if you wish, read over the applicable Supporting Resources links; otherwise, you only need to be concerned with putting the folder paths in the SET oPenArray= variable.

Batch Script

@ECHO OFF

SET oPenArray="C:\Folder\Test","C:\Folder123\Prod"

SET "tmpFile=%temp%\oFolder.lst"
IF EXIST "%tmpFile%" DEL /Q /F "%tmpFile%"

CALL :PowerShell
CD /D "%PowerShellDir%"
Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%'"
FOR /F "USEBACKQ TOKENS=*" %%A IN ("%tmpFile%") DO (
    FOR %%B IN (%oPenArray%) DO (
    IF /I NOT [%%~A]==[%%~B] EXPLORER "%%~B"
    )
)
EXIT

:PowerShell
SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
SET PSScript=%temp%\~tmpOpenFolders.ps1
IF EXIST "%PSScript%" DEL /Q /F "%PSScript%"

ECHO $app     = New-Object -COM 'Shell.Application'                             >> "%PSScript%"
ECHO $t       = $app.Windows() ^| Select-Object LocationURL                     >> "%PSScript%"
ECHO $x       = $t ^| %% {[string]$_.LocationURL -Replace "file:////", ""}      >> "%PSScript%"
ECHO $folders = $x.Replace("file:///", "").Replace("/","\").Replace("%%20"," ") >> "%PSScript%"
ECHO $folders ^| Out-File "%tmpFile%" -Encoding "ascii" -Append                 >> "%PSScript%"
GOTO :EOF

Supporting Resources

  • FOR /F
  • FOR /?

        tokens=x,y,m-n  - specifies which tokens from each line are to
                          be passed to the for body for each iteration.
                          This will cause additional variable names to
                          be allocated.  The m-n form is a range,
                          specifying the mth through the nth tokens.  If
                          the last character in the tokens= string is an
                          asterisk, then an additional variable is
                          allocated and receives the remaining text on
                          the line after the last token parsed.
        usebackq        - specifies that the new semantics are in force,
                          where a back quoted string is executed as a
                          command and a single quoted string is a
                          literal string command and allows the use of
                          double quotes to quote file names in
                          file-set.
    
  • IF

  • Get report of all open explorer windows

  • Replace()

  • ForEach-Object

    Standard Aliases for Foreach-Object: the '%' symbol, ForEach

  • Out-File
  • Call
  • Is this able to detect a particular folder being open? I have heard that regardless of how many folders are open, there would be only one instance of explorer.exe running... and also is it possible to refer to one particular folder in win explorer and close it? i only know tskill or taskkill commands to close processes but if used to kill explorer.exe all of the folders would be closed...and I cant find a command that closes a particular folder in cmd. – Jack the Ripper Oct 19 '18 at 10:51
  • I'd have to do more testing but if you have the PID of a particular folder, then yes you can kill that specific PID. I posted something here superuser.com/questions/1102108/… and also another here superuser.com/questions/1111762/… but I have to run out for a few hours but would have to test to give you a 100% for certain on that to confirm & verify based on your particular needs & understand how to differentiate between 2 of the same folders. – Pimp Juice IT Oct 19 '18 at 12:09
  • @JacktheRipper ... Let me know if you ran into any road blocks with what I provided, if it needs extended logic to do something more that I may have overlooked or not tested thoroughly enough to emulate such a situation to generate such an event, etc. I just need a clear understanding and since I cannot sit with you and talk about it or draw it out perhaps, let's see if you can help me further understand your needs or issues so I can further assist to try to get you a working solution that'll suffice for your needs. I am capable of helping you more, I'm a resource, and I'm here so use me!! – Pimp Juice IT Oct 20 '18 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.