2

I know how to open a folder in explorer with python via :

subprocess.Popen(r'explorer /select,"C:\path\of\folder"')

But I don't know how to prevent my program from opening the folder if it is already "opened" in explorer. Is there a way to do this in Python (or via a VBA script maybe) ?

2 Answers2

0

Here is a interesting thread I found in which a working solution to list open folders is found with a VBS script, but I don't know how to use VBS, thus I am not able to solve the identifier excepted errors and make it work properly..

https://social.msdn.microsoft.com/Forums/vstudio/en-US/de63322b-7f94-4464-be72-2e174106da9c/get-file-explorer-all-opened-folders-path-in-vbnet?forum=vbgeneral

The code itself is :

Imports System.Runtime.InteropServices

Imports System.Text

Public Class Form1 Private Const WM_GETTEXT As Integer = &HD Private Const WM_GETTEXTLENGTH As Integer = &HE

<DllImport("user32.dll", EntryPoint:="FindWindowExW")> _
Private Shared Function FindWindowExW(ByVal hwndParent As System.IntPtr, ByVal hwndChildAfter As System.IntPtr, <InAttribute(), MarshalAs(UnmanagedType.LPTStr)> ByVal lpszClass As String, <InAttribute(), MarshalAs(UnmanagedType.LPTStr)> ByVal lpszWindow As String) As System.IntPtr
End Function

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As Integer, ByVal lParam As StringBuilder) As Integer
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ListBox1.Items.Clear()
    Dim hWinList As New List(Of IntPtr)

    'Get Each Explorer Windows Handle
    Dim hWnd As IntPtr = FindWindowExW(IntPtr.Zero, IntPtr.Zero, "CabinetWClass", Nothing)
    While Not hWnd.Equals(IntPtr.Zero)
        hWinList.Add(hWnd)
        hWnd = FindWindowExW(IntPtr.Zero, hWnd, "CabinetWClass", Nothing)
    End While

    'Loop threw each explorer window in the list and get the text from the Address combobox
    If hWinList.Count > 0 Then
        For Each hChld As IntPtr In hWinList
            Dim hChild1 As IntPtr = FindWindowExW(hChld, IntPtr.Zero, "WorkerW", Nothing)
            Dim hChild2 As IntPtr = FindWindowExW(hChild1, IntPtr.Zero, "ReBarWindow32", Nothing)
            Dim hChild3 As IntPtr = FindWindowExW(hChild2, IntPtr.Zero, "ComboBoxEx32", Nothing)
            Dim len As Integer = SendMessage(hChild3, WM_GETTEXTLENGTH, 0, Nothing)
            Dim sb As New StringBuilder(len + 1)
            SendMessage(hChild3, WM_GETTEXT, len + 1, sb)
            ListBox1.Items.Add(sb.ToString)
        Next
    End If

End Sub

End Class

-4

I'm not sure what you are going for but maybe something like this would help:

import os
for root, dirs, files in os.walk(Folder_Root, topdown=False):
    for name in dirs:
        full_path = os.path.join(root, name)
        #use Popen to open the folder here

So read through all the directories under Folder_Root and open each one with Popen. Each folder is only opened once. Just replace Folder_Root with an actual path.

theObserver
  • 127
  • 2
  • 11
  • What i am trying to do is to prevent my program from opening a folder if it has already been opened in explorer by the user, not open each sub directories of a folder. – 83101118101110 Aug 13 '17 at 17:15
  • This thread might help: https://stackoverflow.com/questions/12726218/name-of-files-opened-by-a-process-in-window – theObserver Aug 13 '17 at 17:34
  • Using psutil seems to be a good way to solve the problem, it is good at finding running binaries but it is harder for open folders since they are not processes. The solution could be to seek into the process "explorer.exe" but i did not find a proper way to do this. – 83101118101110 Aug 13 '17 at 18:03