list all files in a folder and get CRC-32 hashes

Get help with using AutoHotkey and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

list all files in a folder and get CRC-32 hashes

16 May 2018, 20:12

- The latest AutoHotkey v1 test build supports long paths.
Test build - Obj.Count(), OnError(), long paths, experimental switch-case - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=24&t=47682
- Here is a script to list all of the files in a folder and get CRC-32 hashes.

- I'm posting here in case anyone can make any suggestions re. optimisation. E.g.
- Repeated DllCall.
- Optimise to run for a long time without slowing the machine down. (Add sleeps? Set the process priority?)
- Anything else. Cheers.

Code: Select all

;list all files in a folder and get CRC-32 hashes

vDoSkipHtmDir := 1
vDoJeeGetDriveFriendlyName := 1
vDirOut := A_Desktop
vPathAhk := A_Desktop "\AutoHotkey_1.1.28.02-11+g79ff7c8\AutoHotkeyU32.exe"

vDirDefault := A_Desktop "\long path"
vDir1 := InputBox("which folder?",,, vDirDefault)
vDir1 := RTrim(vDir1, "\")
if (StrLen(vDir1) = 1)
	vDir1 .= ":"

;[AHK v1 test build that supports long paths]
;Test build - Obj.Count(), OnError(), long paths, experimental switch-case - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=24&t=47682
;[JEEAhk1FC.ahk]
;commands as functions (AHK v2 functions for AHK v1) - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=37&t=29689

;#SingleInstance force
ListLines("Off")
#KeyHistory 0
Menu, Tray, Click, 1
#NoEnv
AutoTrim, Off
;#UseHook

SplitPath(A_ScriptName,,,, vScriptNameNoExt)
Menu, Tray, Tip, % vScriptNameNoExt

#MaxMem 400

;ProcessSetPriority("AboveNormal")
;ProcessSetPriority("High")

vDate1 := A_Now, vDate2 := A_NowUTC

;MsgBox(A_AhkPath)
;if 0
if !(vPathAhk = A_AhkPath)
{
	;Run, "%vPathAhk%" "%A_LoopFileFullPath%"
	Run(Chr(34) vPathAhk Chr(34) " " Chr(34) A_ScriptFullPath Chr(34))
	ExitApp()
}

vDrive2 := vDrive := SubStr(vDir1, 1, 1)
if vDoJeeGetDriveFriendlyName
{
	Loop Files, % vDrive "\zzzz this drive is *", D
		vDrive2 := SubStr(A_LoopFileName, 20, 1)
}
if (StrLen(vList) > 1)
{
	Clipboard := vOutput
	return
}

VarSetCapacity(vOutput, 60000000*2) ;60 meg
vDetails := ;continuation section
(Join`s"`r`n"`s
"date started: " vDate1 " (local time)"
"date started: " vDate2 " (UTC)"
"folder: " vDir1

"path	date mod. (local)	date cre. (local)	size	hash (CRC-32)"
)
vOutput .= vDetails "`r`n"

hModule := DllCall("kernel32\LoadLibrary", "Str","ntdll.dll", "Ptr")

vOffset := 0 ;convert date local to UTC
vOffset := -1 ;convert date local to UTC
vCount := vCountL := 0
MsgBox("dir: " vDir1 "`r`n" "drive: " vDrive "`r`n" "drive (friendly): " vDrive2)
Loop Files, % "\\?\" vDir1 "\*", FR
{
	vCount++
	vPath := A_LoopFileFullPath
	if vDoSkipHtmDir && InStr(vPath, "_files\")
		continue
	vCountL++
	if (SubStr(vPath, 1, 4) = "\\?\")
		vPath := SubStr(vPath, 5)
	;SplitPath(vPath, vName, vDir, vExt, vNameNoExt, vDrive)
	if vDoJeeGetDriveFriendlyName
		StrPut(vDrive2, &vPath, 1)
	;, vDateM := DateAdd(A_LoopFileTimeModified, vOffset, "H")
	;, vDateC := DateAdd(A_LoopFileTimeCreated, vOffset, "H")
	, vDateM := A_LoopFileTimeModified
	, vDateC := A_LoopFileTimeCreated
	, vSize := A_LoopFileSize
	, vHash := JEE_FileGetHashCRC32("\\?\" vPath)
	, vHash := (vHash = "") ? "--------" : vHash
	;, vOutput .= vHash "`t" vDateM "`t" vDateC "`t" vSize "`t" vPath "`r`n"
	, vOutput .= vPath "`t" vDateM "`t" vDateC "`t" vSize "`t" vHash "`r`n"

	if (StrLen(vOutput) >= 50000000)
	{
		FileAppend(vOutput, vPath2, "UTF-8")
		vOutput := ""
		VarSetCapacity(vOutput, 60000000*2) ;60 meg
	}
}

vDate3 := A_Now, vDate4 := A_NowUTC

vDetails := ;continuation section
(Join`s"`r`n"`s
"date: " vDate1 " to " vDate3 " (local time)"
"date: " vDate2 " to " vDate4 " (UTC)"
"folder: " vDir1
"files listed: " vCountL
"file count: " vCount
)

vOutput .= "`r`n" vDetails "`r`n"
vPath2 := vDirOut "\z list files " vDate1 ".txt"

;Clipboard := vOutput
;Clipboard := vDetails "`r`n`r`n" vOutput
;FileAppend(vDetails "`r`n`r`n", vPath2, "UTF-8")
FileAppend(vOutput, vPath2, "UTF-8")
MsgBox("done")
DllCall("kernel32\FreeLibrary", "Ptr",hModule)
Run(vPath2)
return

#Include %A_ScriptDir%\Lib\JEEAhk1FC.ahk

;#Include %A_ScriptDir%\Lib\JEEHash.ahk

;==================================================

;get CRC-32 hash value, read file in chunks - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=5&t=31329
;AHK_Scripts/src/hash_checksum at master · jNizM/AHK_Scripts · GitHub
;https://github.com/jNizM/AHK_Scripts/tree/master/src/hash_checksum
;Wine API: RtlComputeCrc32
;https://source.winehq.org/WineAPI/RtlComputeCrc32.html

;based on CRC32_File() by jNizM
;however, the output is uppercase/no '0x'/always 8 characters
JEE_FileGetHashCRC32(vPath)
{
	if !(oFile := FileOpen(vPath, "r", "UTF-8"))
		return
	oFile.Seek(0)
	while (vBytesRead := oFile.RawRead(vData, 0x40000))
		vHash := DllCall("ntdll\RtlComputeCrc32", "UInt",vHash, "Ptr",&vData, "Int",vBytesRead, "UInt")
	oFile.Close()
	return Format("{:08X}", vHash)
}

;==================================================

;e.g. vDP:2 round down to 2 decimal places
;e.g. vDP:-2 round down to nearest 100

JEE_Floor(vNum, vDP:=0)
{
	if !vDP
		return Floor(vNum)
	else if (vDP > 0)
		return Format("{:0." vDP "f}", Floor(vNum * (10**vDP)) * (10**-vDP))
	else ;if (vDP < 0)
		return Floor(Floor(vNum * (10**vDP)) * (10**-vDP))
}

JEE_Ceil(vNum, vDP:=0)
{
	if !vDP
		return Ceil(vNum)
	else if (vDP > 0)
		return Format("{:0." vDP "f}", Ceil(vNum * (10**vDP)) * (10**-vDP))
	else ;if (vDP < 0)
		return Ceil(Ceil(vNum * (10**vDP)) * (10**-vDP))
}

;==================================================
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], kyuuuri, mikeyww, paul1965 and 40 guests