如果文件夹尚未打开,请在资源管理器中打开

3

我知道如何使用Python打开资源管理器中的文件夹:

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

但是,如果该文件夹已经在资源管理器中“打开”,我不知道如何防止我的程序再次打开它。是否有一种方法可以在Python(或通过VBA脚本)中实现这一点?


好问题,但与Python不太相关。 - undefined
我正在尝试用Python来实现它,但这确实是一个非常普遍的问题,每个解决方案都可能有用,即使它不是使用Python实现的。 - undefined
3个回答

0

这是我找到的一个有趣的讨论串,其中通过一个 VBS 脚本找到了列出打开文件夹的工作解决方案,但我不知道如何使用 VBS,因此我无法解决“identifier excepted”错误并使其正常工作。

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

代码本身是:

Imports System.Runtime.InteropServices

导入 System.Text

公共类 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

结束类


0
这在Windows 10终端(cmd.exe)中有效:
start C:\folder

Python 3 代码:
import subprocess
folder = r'C:\folder'
execute = fr'start {folder}'
subprocess.Popen(execute, shell=True)

-4

我不确定你想要什么,但也许这样的东西会有所帮助:

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

所以遍历Folder_Root下的所有目录,并使用Popen打开每个目录。每个目录只会被打开一次。只需将Folder_Root替换为实际路径即可。


我想要做的是防止我的程序在用户已经通过资源管理器打开一个文件夹后再次打开它,而不是打开文件夹中的每个子目录。 - undefined
这个线程可能会有帮助:https://dev59.com/_Gcs5IYBdhLWcg3wlFA2 - undefined
使用psutil似乎是解决这个问题的好方法,它擅长查找正在运行的二进制文件,但对于打开的文件夹来说就比较困难,因为它们不是进程。解决方案可能是查找进程"explorer.exe",但我没有找到合适的方法来实现这一点。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接