-
-
Save Hill-98/cec377910e9b20d182d1002a0abb84e1 to your computer and use it in GitHub Desktop.
AutoHotKey v1 Windows symbolic link helper
This file contains 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
CreateSymbolicLink(link, target) | |
{ | |
FileGetAttrib, attrib, %target% | |
flag := InStr(attrib, "D") ? 0x1 : 0x0 | |
Return DllCall("CreateSymbolicLink", "Str", link, "Str", target, "Int", flag | 0x2) ? True : False | |
} | |
DeleteSymbolicLink(link) | |
{ | |
If (!IsSymbolicLink(link)) | |
{ | |
Return False | |
} | |
FileGetAttrib, attrib, %link% | |
If (InStr(attrib, "D")) | |
{ | |
FileRemoveDir, %link% | |
} | |
Else | |
{ | |
FileDelete, %link% | |
} | |
Return ErrorLevel ? False : True | |
} | |
GetSymbolicLinkTarget(link) | |
{ | |
linkHandle := DllCall("CreateFile", "Str", link, "Int", 0, "Int", 0x1 | 0x2, "Ptr", 0, "Int", 3, "Int", 0x02000000, "Ptr", 0) | |
If (linkHandle == -1) | |
{ | |
Return "" | |
} | |
BUFSIZE := 1024 | |
VarSetCapacity(targetNtPath, BUFSIZE) | |
Loop | |
{ | |
result := DllCall("GetFinalPathNameByHandle", "Ptr", linkHandle, "Str", targetNtPath, "Int", BUFSIZE, "Int", 0x2) | |
If (result == 0) | |
{ | |
Return "" | |
} | |
If (result <= BUFSIZE) | |
{ | |
Break | |
} | |
Else | |
{ | |
BUFSIZE := result * 2 | |
VarSetCapacity(targetPath, BUFSIZE) | |
} | |
} | |
DllCall("CloseHandle", "Ptr", linkHandle) | |
targetPath := "" | |
DriveGet, drives, List | |
Loop, Parse, drives | |
{ | |
driveRootPath := A_LoopField . ":\" | |
driveHandle := DllCall("CreateFile", "Str", driveRootPath, "Int", 0, "Int", 0x1 | 0x2, "Ptr", 0, "Int", 3, "Int", 0x02000000, "Ptr", 0) | |
If (linkHandle == -1) | |
{ | |
Continue | |
} | |
VarSetCapacity(driveNtPath, BUFSIZE) | |
result := DllCall("GetFinalPathNameByHandle", "Ptr", driveHandle, "Str", driveNtPath, "Int", BUFSIZE, "Int", 0x2) | |
DllCall("CloseHandle", "Ptr", driveHandle) | |
If (result == 0) | |
{ | |
Continue | |
} | |
nPos := InStr(targetNtPath, driveNtPath) | |
If (nPos == 1) | |
{ | |
targetPath := driveRootPath . SubStr(targetNtPath, StrLen(driveNtPath) + 1) | |
Break | |
} | |
} | |
VarSetCapacity(driveNtPath, 0) | |
VarSetCapacity(targetNtPath, 0) | |
Return targetPath | |
} | |
IsSymbolicLink(link) | |
{ | |
attrib := DllCall("GetFileAttributes", "Str", link) | |
If (attrib == -1) { | |
Return False | |
} | |
Return attrib & 0x400 ? True : False | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment