-
-
Save pa-0/8cfcd86b120cbabaf13c435efbcb9ca9 to your computer and use it in GitHub Desktop.
CreateHardLink for AHK v1 and v2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Requires AutoHotkey v2.0 | |
| ; Version: 2025.10.09.1 | |
| ; AHK v1 script by anonymous1184 (https://gist.github.com/8809edd5a0f212ecec688141da590a24) | |
| CreateHardLink(link, target) { | |
| if (FileExist(link)) { | |
| ErrorLevel := -1 | |
| return false ; Link already exists | |
| } | |
| attributes := FileExist(target) | |
| if (!attributes) { | |
| ErrorLevel := -2 | |
| return false ; target doesn't exist | |
| } | |
| if (InStr(attributes, "D")) { | |
| ErrorLevel := -3 | |
| return false ; Not a file | |
| } | |
| Loop Files, target, "F" | |
| target := A_LoopFileFullPath | |
| if (SubStr(link, 2, 1) = ":") { | |
| if (SubStr(link, 1, 1) != SubStr(target, 1, 1)) { | |
| ErrorLevel := -4 | |
| return false ; Not in the same drive | |
| } | |
| link := "\\?\" link | |
| } | |
| target := "\\?\" target | |
| success := DllCall("CreateHardLink", "Ptr", &link, "Ptr", &target, "Ptr", 0, "Int") | |
| ErrorLevel := !success | |
| return success | |
| } | |
| if (!A_IsCompiled) && (A_LineFile = A_ScriptFullPath) { | |
| ; Example | |
| tnow := A_tNow | |
| file := A_Desktop "\file." tnow ".txt" | |
| link := A_Desktop "\link." tnow ".txt" | |
| FileOpen(file, 0x1).Write("Hello World!") | |
| created := CreateHardLink(link, file) | |
| if (created) | |
| RunWait(link) | |
| else | |
| MsgBox("Error while creating the link.", "Error", 0x40010) | |
| FileDelete(A_Desktop "\*." tnow ".txt") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ; Version: 2023.05.22.2 | |
| ; https://gist.github.com/8809edd5a0f212ecec688141da590a24 | |
| /* | |
| now := A_Now | |
| file := A_Desktop "\file." now ".txt" | |
| link := A_Desktop "\link." now ".txt" | |
| FileOpen(file, 0x1).Write("Hello World!") | |
| created := CreateHardLink(link, file) | |
| if (created) | |
| RunWait % link | |
| else | |
| MsgBox 0x40010, Error, Error while creating the link. | |
| FileDelete % A_Desktop "\*." now ".txt" | |
| */ | |
| CreateHardLink(Link, Target) { | |
| if (FileExist(Link)) { | |
| ErrorLevel := -1 | |
| return false ; Link already exists | |
| } | |
| attributes := FileExist(Target) | |
| if (!attributes) { | |
| ErrorLevel := -2 | |
| return false ; Target doesn't exist | |
| } | |
| if (InStr(attributes, "D")) { | |
| ErrorLevel := -3 | |
| return false ; Not a file | |
| } | |
| loop Files, % Target, F | |
| Target := A_LoopFileLongPath | |
| if (SubStr(Link, 2, 1) = ":") { | |
| if (SubStr(Link, 1, 1) != SubStr(Target, 1, 1)) { | |
| ErrorLevel := -4 | |
| return false ; Not in the same drive | |
| } | |
| Link := "\\?\" Link | |
| } | |
| Target := "\\?\" Target | |
| success := DllCall("CreateHardLink", "Ptr", &Link, "Ptr", &Target, "Ptr", 0, "Int") | |
| ErrorLevel := !success | |
| return success | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment