Function 聚焦还是打开文件夹?

Function 聚焦还是打开文件夹?,function,text,autoit,targeting,Function,Text,Autoit,Targeting,我在这个脚本中遇到了一些问题: If WinExists ("[CLASS:CabinetWClass]", "Address: C:\Users\Dad\Downloads") Then WinActivate ("[CLASS:CabinetWClass]", "Address: C:\Users\Dad\Downloads") Else Run("Explorer.exe" & "C:\Users\Dad\Downloads") Endif 如果我打开了下载的子目

我在这个脚本中遇到了一些问题:

If WinExists ("[CLASS:CabinetWClass]", "Address: C:\Users\Dad\Downloads") Then
    WinActivate ("[CLASS:CabinetWClass]", "Address: C:\Users\Dad\Downloads")
Else
    Run("Explorer.exe" & "C:\Users\Dad\Downloads")
Endif
  • 如果我打开了下载的子目录,如C:\Users\Dad\Downloads\Pictures,它将聚焦该窗口,而不是继续执行Else语句

  • 如果没有打开Windows资源管理器窗口,系统只会向我发出嘟嘟声,脚本就会关闭。我在这里的答案中找到了我的代码:


  • 我尝试为
    Run()
    函数和
    text
    参数标记此项。

    出于某种原因,以下代码不会出现两种不必要的行为:

    If WinExists ("[CLASS:CabinetWClass]", "Address: C:\Users\Dad\Downloads") Then
        WinActivate ("[CLASS:CabinetWClass]", "Address: C:\Users\Dad\Downloads")
    Else
        Run("Explorer.exe C:\Users\Dad\Downloads") #this line was changed.
    Endif
    

    这是一个你想要做的工作示例。基本上,您的代码与正在查找的目录的子字符串相匹配。这就是它激活具有相同子目录的窗口的方式

    FindorOpenExporer("C:\Users\Dad\Downloads")
    
    Func FindorOpenExporer($sPath)
        Local $aWinList = WinList("[CLASS:CabinetWClass]")
    
        ;if no Exporer windows are found
        If IsArray($aWinList) = False Then
            StartEplorer($sPath)
            Return 0
        EndIf
    
        ;if explorer windows are found
        For $i = 1 To UBound($aWinList) - 1
            $sWinText = WinGetText($aWinList[$i][1])
    
            ;activates the window and returns the window handle if it is found
            If StringInStr($sWinText, "Address: " & $sPath) Then
                WinActivate($aWinList[$i][1])
    
                ;returns the window handle
                Return $aWinList[$i][1]
            EndIf
        Next
    
        StartEplorer($sPath)
    EndFunc   ;==>FindorOpenExporer
    
    Func StartEplorer($sPath)
        Run("Explorer.exe " & $sPath)
    EndFunc   ;==>StartEplorer
    

    如果删除了“&”,则为2。已解决。由于某种原因,删除“&”也解决了1。所以,我很好奇为什么。问题是,当你像在你的问题中那样连接两个字符串“Explorer.exe”和“C:\Users\Dad\Downloads”时,你漏掉了一个空格。基本上,您尝试运行以下命令:
    Explorer.exeC:\Users\Dad\Downloads
    。这就是行不通。