1

For example I have this folder "C:\Series" and I have this script below which closes this folder when it is opened

@echo off

cd /d C:\ start C:\Windows\explorer.exe CD_Restored

rem Terminate process: taskkill /F /FI "WINDOWTITLE eq Music" /IM explorer.exe > nul

It works successfully, but when I open any folder inside it, this code surely doesn't work, and I need to close any opened folder inside it. I have over 15 subfolders inside it, so I can't make a script line for each of them, especially that I am always updating it. So could any one tell me how to make a script which closes a folder and its subfolders?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Omar Mejdi
  • 11
  • 1
  • 3
    It is clear that when the `WINDOWTITLE` is not `Music`, the `taskkill` command will not terminate it. Also batch files cannot interact with the GUI, so there is no way of dynamically automating this without the use of third party tools or another scripting language. – Compo May 31 '21 at 11:25
  • You can call Powershell script using this [solution](https://stackoverflow.com/questions/36992333/) – Daemon-5 May 31 '21 at 14:41
  • What about using a vbscript [How to close a specific folder with VBScript?](https://stackoverflow.com/questions/45971601/how-to-close-a-specific-folder-with-vbscript?answertab=active#tab-top) instead using a batch file ? – Hackoo Jun 01 '21 at 09:15

1 Answers1

0

Just give a try for this batch file that use a vbscript :

@echo off
Title Close Opened Folders
Set "MainFolderName=series"
Call :CloseAllOpenedFolders "%MainFolderName%"
Pause & Exit
::---------------------------------------------------------------------------------------
:CloseAllOpenedFolders <MainFolderName>
Set "VBSFILE=%Temp%\CloseAllOpenedFolders.vbs"
(
    echo Dim MainFolder,shell,oWindows,W
    echo MainFolder = "%~1"
    echo Set shell = CreateObject("Shell.Application"^)
    echo set oWindows = shell.windows
    echo For Each W in oWindows
    echo    If InStr(LCase(W.document.folder.self.Path^),LCase(MainFolder^)^)^> 0 Then
    echo        wscript.echo W.document.folder.self.Path
    echo        W.Quit
    echo    End If
    echo Next
)>"%VBSFILE%"
Cscript //NoLogo "%VBSFILE%" 
Exit /B
::---------------------------------------------------------------------------------------

EDIT : Powershell Solution

$mainFolder= "c:\series"
$shell = New-Object -ComObject Shell.Application
$window = $shell.Windows() | ? { $_.LocationURL -like "$(([uri]$MainFolder).AbsoluteUri)*" }
$window.document.Folder.Self.Path
$window | % { $_.Quit() }
Hackoo
  • 18,337
  • 3
  • 40
  • 70