写的有点复杂,仅供参考。
需要用 AutoHotkey_1.1.31以上版本,因为用到了Switch case语句(AutoHotkey1.1.31.00原生支持Switch-Case)
自测速度还可以,TC界面下移动结果的显示有点延迟,是TC自身原因。
/*
作者:sunwind1576157
时间:2019年12月12日
功能:在某文件夹下,按win+z 来归档文件,按文件的年份归到不同年份目录里
在当前目录中自动创建不同年份目录。
只处理文件,不处理子目录。
*/
#z::
OutWinClass:=""
OutCurrentFolder:=""
WinGet, OutWindowID, id, A
WinGetClass, OutWinClass, ahk_id %OutWindowID%
Switch OutWinClass
{
Case "TTOTAL_CMD":
OutCurrentFolder:=getTcFolder()
Case "ExploreWClass","CabinetWClass":
OutCurrentFolder:=getExplorerFolder()
Default:
return
}
Traversal(OutCurrentFolder)
return
Traversal(target)
{
ToolTip,正在处理%target%...
;R: Recurse into subdirectories (subfolders). If R is omitted, files and folders in subfolders are not included.
loop,%target%\*.*,DFR
{
SplitPath,A_LoopFileFullPath,,,OutExt
;~ ;只处理ahk文件 非ahk文件则忽略
;~ if (OutExt!="ahk")
;~ continue
FileGetTime,OutTime,%A_LoopFileFullPath%,M
StringLeft,OutYear,OutTime,4
SourcePattern:=A_LoopFileFullPath
DestinationFolder:=target . "\" . OutYear
;~ CopyFiles(SourcePattern,DestinationFolder)
MoveFiles(SourcePattern,DestinationFolder)
}
ToolTip,完成!
return
}
CopyFiles(SourcePattern, DestinationFolder, DoOverwrite = false)
; 复制匹配 SourcePattern 的所有文件和文件夹到 DestinationFolder 文件夹中且
; 返回无法复制的文件/文件夹的数目.
{
if !FileExist(DestinationFolder)
{
try
{
FileCreateDir,% DestinationFolder
ToolTip 创建%DestinationFolder%
}
catch
{
MsgBox % "创建目录失败" . DestinationFolder
}
}
; 复制文件
FileCopy, %SourcePattern%, %DestinationFolder%, %DoOverwrite%
ToolTip 归档 %DestinationFolder%
if ErrorLevel ; 报告每个出现问题的文件夹名称.
MsgBox Could not copy %SourcePattern% into %DestinationFolder%
}
MoveFiles(SourcePattern, DestinationFolder, DoOverwrite = false)
{
if !FileExist(DestinationFolder)
{
try
{
FileCreateDir,% DestinationFolder
ToolTip 创建%DestinationFolder%
}
catch
{
MsgBox % "创建目录失败" . DestinationFolder
}
}
; 移动匹配 SourcePattern 的所有文件和文件夹到 DestinationFolder 文件夹中且
; 返回无法移动的文件/文件夹的数目. 此函数需要 [v1.0.38+]
; 因为它使用了 FileMoveDir 的模式 2.
FileMove, %SourcePattern%, %DestinationFolder%, %DoOverwrite%
if ErrorLevel ; 报告每个出现问题的文件夹名称.
MsgBox Could not move %SourcePattern% into %DestinationFolder%.
}
getExplorerFolder()
{
loop,9
{
ControlGetText, folder, ToolbarWindow32%A_Index%, ahk_class CabinetWClass
} until (InStr(folder,"地址"))
folder:=StrReplace(folder,"地址: ","")
Switch folder
{
Case "桌面":
;~ folder:= A_Desktop
folder:= getSpec("Desktop")
Case "视频","库\视频":
folder:= getSpec("My Video")
Case "图片","库\图片":
folder:= getSpec("My Pictures")
Case "文档","库\文档":
;~ folder:= A_MyDocuments
folder:= getSpec("{F42EE2D3-909F-4907-8871-4C22FC0BF756}")
Case "音乐","库\音乐":
folder:= getSpec("My Music")
Case "下载":
folder:= getSpec("{7D83EE9B-2244-4E70-B1F5-5393042AF1E4}")
Case "收藏":
folder:= getSpec("Favorites")
Case "OneDrive":
folder:= getSpec("{24D89E24-2F19-4534-9DDE-6A6671FBB8FE}")
Case "此电脑","回收站", "网上邻居", "控制面板", "我的电脑","快速访问":
folder:="c:\Windows"
;Default 默认就是folder本身无需转换。
}
return folder
}
getTcFolder()
{
ClipSaved:=ClipboardAll
clipboard =
SendMessage 1075,2029,0,,ahk_class TTOTAL_CMD
ClipWait,2
OutDir=%clipboard%
Clipboard:=ClipSaved
ClipSaved=
return OutDir
}
getSpec(str)
{
Loop, Reg, HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders, KVR
{
if a_LoopRegType = key
value =
else
{
RegRead, value
if ErrorLevel
value = *error*
}
if (a_LoopRegName=str)
break
}
Transform,dir,Deref,%value%
return dir
/*
Templates
Start Menu
Startup
SendTo
Recent
Programs
Personal
PrintHood
NetHood
My Video
My Pictures
My Music
Local AppData
History
Favorites
Desktop
Cookies
Cache
AppData
*/
}