[AHK]按文件的年份歸檔到不同年份目錄裏

寫的有點複雜,僅供參考。

需要用 AutoHotkey_1.1.31以上版本,因爲用到了Switch case語句(AutoHotkey1.1.31.00原生支持Switch-Case

自測速度還可以,TC界面下移動結果的顯示有點延遲,是TC自身原因。

 

/*
作者:sunwind1576157
時間:2019年12月12日
更新網址:https://blog.csdn.net/liuyukuan/article/details/103517951
功能:在某文件夾下,按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
*/
 
}

 

 
發佈了333 篇原創文章 · 獲贊 269 · 訪問量 288萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章