资源管理器内选中的文件-高级文件操作类fileplus.ahk

资源管理器内选中的文件-高级文件操作类fileplus.ahk

一、FilePlus类简介

这是一个基础类,用于实现对文件文件夹的自动化操作。目前主要有以下方法:

方法 Explorer_GetPath(hwnd=””)–获取当前管理器路径
方法 Explorer_GetAll(hwnd=””)–获取管理器目录
方法 Explorer_GetSelected(hwnd=””)–获取管理器已选择文件目录
方法 Explorer_GetWindow(hwnd=””)获取管理器窗口名字
方法 Explorer_Get(hwnd=””,selection=false)获取管理器目录获取
方法 File_OpenAndSelect(path, selfilearr) –打开并选中文件
方法 Files_OpenAndSelect(path, selfilearr) –打开并选中一个或多个文件

二、起因问题

有没有大神能帮我实现一下随机选中文件夹中的10个文件,下午折腾到现在就是弄不起来。

上述问题看似简单其实需要一系列复杂的操作才可以完成。

涵盖了随机数的操作、文件目录的获取、文件的选中等一系列操作。这个问题核心点和难点有两个,目录获取和文件选中,经过在AutoHotkey超高级群|中文社区,激烈讨论,这两个核心被攻破,下面给出各知识点的具体示例,至于后续的整合和其他应用这里不做探讨。

三、示例(应用场景)

示例一:打开文件目录并选中一个文件

#SingleInstance force
OpenAndSelect("E:c学习12.cpp")
return

OpenAndSelect(sFullPath)
{
	SplitPath sFullPath, , sPath
	FolderPidl := DllCall("shell32\\ILCreateFromPath", "Str", sPath)
	DllCall("shell32\\SHParseDisplayName", "str", sFullPath, "Ptr", 0, "Ptr*", ItemPidl := 0, "Uint", 0, "Uint*", 0)
	DllCall("shell32\\SHOpenFolderAndSelectItems", "Ptr", FolderPidl, "UInt", 1, "Ptr*", ItemPidl, "Int", 0)
	CoTaskMemFree(FolderPidl)
	CoTaskMemFree(ItemPidl)
}

CoTaskMemFree(pv) 
{
   Return   DllCall("ole32\CoTaskMemFree", "Ptr", pv)
}

示例二:多文件选中(v2版本)

示例三:获取文件路径、目录树、和已选择的目录

F9::
    path := Explorer_GetPath()
    all := Explorer_GetAll()
    sel := Explorer_GetSelected()
    MsgBox % path
    MsgBox % all
    MsgBox % sel
return
 
 
Explorer_GetPath(hwnd="")
{
   if !(window := Explorer_GetWindow(hwnd))
      return ErrorLevel := "ERROR"
   if (window="desktop")
      return A_Desktop
   path := window.LocationURL
   path := RegExReplace(path, "ftp://.*@","ftp://")
   StringReplace, path, path, file:///
   StringReplace, path, path, /, , All
 
   ; thanks to polyethene
   Loop
      If RegExMatch(path, "i)(?<=%)[da-f]{1,2}", hex)
         StringReplace, path, path, `%%hex%, % Chr("0x" . hex), All
      Else Break
   return path
}
 
Explorer_GetAll(hwnd="")
{
   return Explorer_Get(hwnd)
}
Explorer_GetSelected(hwnd="")
{
   return Explorer_Get(hwnd,true)
}
 
Explorer_GetWindow(hwnd="")
{
   ; thanks to jethrow for some pointers here
    WinGet, process, processName, % "ahk_id" hwnd := hwnd? hwnd:WinExist("A")
    WinGetClass class, ahk_id %hwnd%
 
   if (process!="explorer.exe")
      return
   if (class ~= "(Cabinet|Explore)WClass")
   {
      for window in ComObjCreate("Shell.Application").Windows
         if (window.hwnd==hwnd)
            return window
   }
   else if (class ~= "Progman|WorkerW")
      return "desktop" ; desktop found
}
 
Explorer_Get(hwnd="",selection=false)
{
   if !(window := Explorer_GetWindow(hwnd))
      return ErrorLevel := "ERROR"
   if (window="desktop")
   {
      ControlGet, hwWindow, HWND,, SysListView321, ahk_class Progman
      if !hwWindow ; #D mode
         ControlGet, hwWindow, HWND,, SysListView321, A
      ControlGet, files, List, % ( selection ? "Selected":"") "Col1",,ahk_id %hwWindow%
      base := SubStr(A_Desktop,0,1)=="" ? SubStr(A_Desktop,1,-1) : A_Desktop
      Loop, Parse, files, `n, `r
      {
         path := base "" A_LoopField
         IfExist %path% ; ignore special icons like Computer (at least for now)
            ret .= path "`n"
      }
   }
   else
   {
      if selection
         collection := window.document.SelectedItems
      else
         collection := window.document.Folder.Items
      for item in collection
         ret .= item.path "`n"
   }
   return Trim(ret,"`n")
}

 

 

最后,我对上面例子进行研究,梳理编写了高级文件操作类-fileplus(高级文件操作类).ahk ,有需要的可以下载使用。

类的使用示例和方法均已在类中注明

;===============
;文件高级操作类-来之Autohotkey中文社区https://www.autoahk.com/archives/18616
;===============
;方法 Explorer_GetPath(hwnd="")--获取当前管理器路径
;方法 Explorer_GetAll(hwnd="")--获取管理器目录
;方法 Explorer_GetSelected(hwnd="")--获取管理器已选择文件目录
;方法 Explorer_GetWindow(hwnd="")获取管理器窗口名字
;方法 Explorer_Get(hwnd="",selection=false)获取管理器目录获取
;方法 File_OpenAndSelect(path, selfilearr) --打开并选中文件
;方法 Files_OpenAndSelect(path, selfilearr) --打开并选中一个或多个文件

;示例
myfile:=New fileplus
F9::
    path := myfile.Explorer_GetPath()
    all := myfile.Explorer_GetAll()
    sel := myfile.Explorer_GetSelected()
    MsgBox % path
    MsgBox % all
    MsgBox % sel
    myfile.File_OpenAndSelect("E:\\lenovo\\Pictures\随机选中的样例.png") ;更换为自己的路径
    myfile.Files_OpenAndSelect("E:\\lenovo\\Pictures",["随机选中的样例.png","#ClipboardTimeout备注的解释.png"]) ;更换为自己的路径
return

四、下载

下载权限

查看
  • 免费下载
    评论并刷新后下载
    登录后下载

查看演示

  • {{attr.name}}:
您当前的等级为
登录后免费下载登录 小黑屋反思中,不准下载! 评论后刷新页面下载评论 支付以后下载 请先登录 您今天的下载次数(次)用完了,请明天再来 支付积分以后下载立即支付 支付以后下载立即支付 您当前的用户组不允许下载升级会员
您已获得下载权限 您可以每天下载资源次,今日剩余

给TA捐赠
共{{data.count}}人
人已捐赠
其他

谷歌翻译

2020-3-9 5:44:44

其他

超级热键

2020-3-9 5:46:44

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索