Get Explorer's children in batch

倖福魔咒の 提交于 2020-02-04 21:07:51

问题


I created folder shortcuts for my taskbar and I would like them to stop launching a new explorer every time

So I decided to create a batch script, howover I can not get the kids from explorer.exe

@echo off
pushd
tasklist /nh /fi "imagename eq explorer.exe C:\Users\danil\Desktop\ISO" | find /i "explorer.exe C:\Users\danil\Desktop\ISO" > nul ||(start explorer.exe C:\Users\danil\Desktop\ISO)

回答1:


The issue with your attempt is that tasklist will list only one instance of explorer.exe but not the titles of each window openned.

With some edits over this I've created listWindows.bat - it will list all visible windows names and their coresponding executable. So you can try this:

 call listWindows.bat|findstr /i /b /e "explorer::Downloads" >nul 2>nul || (
    start "" explorer.exe "C:\Users\%username%\Downloads"
 )

To check the windows you need to start you can just try this:

call listWindows.bat|findstr /i /b  "explorer::"



回答2:


You cannot check the opening folders by checking the command line options, because the arguments stay the same across the whole life of the process even after you changed to some other folders in that window. You need to use scriptable shell objects to get the addresses.

Here's a PowerShell script to open a folder if it's not already opened in explorer

$folder = 'C:\Users\danil\Desktop\ISO'
$folderOpened = $false
foreach ($w in (New-Object -ComObject Shell.Application).Windows()) {
    if ($w.LocationURL -ieq ([uri]$folder).AbsoluteUri) { $folderOpened = $true; break }
}
if (-not $folderOpened) { Invoke-Item $folder } # or start $folder

Below is an equivalent hybrid batch-jscript snippet

@if (@CodeSection == @Batch) @then

@echo off
cscript //e:jscript //nologo "%~f0" %*
exit /b

@end

// JScript Section

var objShell = new ActiveXObject("shell.application");
var objShellWindows;
objShellWindows = objShell.Windows();

if (objShellWindows != null)
{
    var folder = "file:///C:/Users/danil/Desktop/ISO"; // the folder you want to open
    var folderOpened = 0;
    for (var objEnum = new Enumerator(objShellWindows); !objEnum.atEnd(); objEnum.moveNext())
    {
        if (folder == objEnum.item().LocationUrl)
        {
            folderOpened = 1;
            break;
        }
    }
    if (!folderOpened) // open the folder if it's not already opened
        objShell.Explore(folder); // or objshell.Open(folder)
}

Each explorer window is represented by an InternetExplorer object that can be retrieved from the Shell.Windows() collection. You need to use a file URI scheme instead of a normal Windows path, but it works. Of course you can even further change it to switch to the folder window if it's being opened



来源:https://stackoverflow.com/questions/48084216/get-explorers-children-in-batch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!