How to use Ahk remove the duplicate images Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
alexsu
Posts: 33
Joined: 16 Jul 2020, 05:32

How to use Ahk remove the duplicate images

08 Aug 2020, 08:51

For example
There's a file, the file has many pictures, they have different names. but the format is same, such as they all are jpg.
I want to select those pictures, then click one button(such as F1)then, the duplicate images(Name is different, but the image's content is same) are removed.
Could anyone tell me how to write this AhK script ?
Thank you
User avatar
flyingDman
Posts: 855
Joined: 29 Sep 2013, 19:01

Re: How to use Ahk remove the duplicate images

08 Aug 2020, 13:22

I would compare the files on a binary level. Have a look at @LinearSpoon 's function here: https://www.autohotkey.com/boards/viewtopic.php?t=1662
User avatar
Chunjee
Posts: 673
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

Re: How to use Ahk remove the duplicate images

08 Aug 2020, 13:39

that would work for finding exact duplicates. but imo finding visually similar, with slight variations, might be slightly more useful for clearing out the users meme folder.
User avatar
flyingDman
Posts: 855
Joined: 29 Sep 2013, 19:01

Re: How to use Ahk remove the duplicate images  Topic is solved

08 Aug 2020, 16:17

As imageequal() also relies on DllCall("ntdll\RtlCompareMemory"..., I believe it does a binary comparison as well. The slightest variation between pictures will be detected.
Using the function i previously referred to, the following allows you to selected multiple files in a folder and report and show identical pictures in the same folder as the selected files. You can add code to delete one the files. Note: If the file selection contains duplicates, it will report the duplicate twice. This requires the functions here: https://autohotkey.com/board/topic/60985-get-paths-of-selected-items-in-an-explorer-window/ and FileCompare() (both included).

Code: Select all

#\::
slctd := Explorer_GetSelected()
fldr := Explorer_GetPath()
for x,y in strsplit(slctd,"`n")
	{
	Loop, Files, % fldr "\*.jpg"                                              ; change to your needs
		{
		if (FileCompare(y, A_LoopFileFullPath) and y != A_LoopFileFullPath)
			{
			gui, margin, 0,0 
			gui, -caption +border
			gui, Add, Picture, vp1 x0 y0 w600 h-1, % y
			gui, Add, Picture, vp2 x+5 y0 w600 h-1, % A_LoopFileFullPath
			gui, show		
			msgbox % y "`n" A_LoopFileFullPath "`nare the same"
			gui, destroy
			continue
			}
		}
	}
msgbox No further duplicates!	
return

FileCompare(file1, file2)
	{
	FileGetSize, sFile1, %file1%
	FileGetSize, sFile2, %file2%
	if (sFile1 != sFile2)
		return 0  ;Not the same size so impossible to have the same contents
	FileRead, bFile1, *c %file1%
	FileRead, bFile2, *c %file2%
	return sFile1 = DllCall("ntdll\RtlCompareMemory", "ptr", &bFile1, "ptr", &bFile2, "ptr", sFile1)
	}
;Pass a relative or absolute filename for each file1 and file2
;Returns true if the contents of file1 = file2, or false if there are differences
	
Explorer_GetPath(hwnd="")											; credit: Rapte_Of_Suzaku 
{
	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 
	Loop															; thanks to polyethene	
		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")
}

Return to “Ask For Help”

Who is online

Users browsing this forum: boiler, Epialis, Google [Bot], Hackerxd, hasantr, Rohwedder, swagfag, Xeo786, XShayanX and 53 guests