; Folder2Junction 1.0 ; Description: Moves a folder to a new location, then makes a NTFS junction in the folder's original location pointing to its new location. ; Please see setup file for acknowledgements, license terms, etc. This AutoHotkey source is provided for educational purposes only. Use at your own risk. #NoEnv #NoTrayIcon If 0 < 1 { Gosub, Selectdefault ExitApp } If 0 > 1 { MsgBox, 48, Folder2Junction, Folder2Junction can process only 1 folder at a time. To change default destination for moving folders`, run the application without any parameters. ExitApp } Loop, %1%, 2 { ; FILE_ATTRIBUTE_REPARSE_POINT := 0x400 attributes := DllCall("GetFileAttributes","str",A_LoopFileLongPath) if (attributes != -1 && attributes & 0x400) { MsgBox, 16, Folder2Junction, Cannot move folder (nor subsequently create junction) because %A_LoopFileLongPath% is already a junction! ExitApp } loop { RegRead, target, HKEY_CURRENT_USER, Software\Folder2Junction, destinationfolder If ErrorLevel Gosub, Selectdefault FileSelectFolder, folderdestination, *%target%,, Select where to move FOLDER "%A_LoopFileName%" `&& its CONTENTS. After moving, a new JUNCTION "%A_LoopFileName%" will be automatically created in the original location`, %A_LoopFileDir% If ErrorLevel ExitApp folderdestination := RegExReplace(folderdestination, "\\$") ; Removes the trailing backslash, if present. IfExist %folderdestination%\%A_LoopFileName% { MsgBox, 49, Folder2Junction, The folder cannot be moved there because there is already a folder in "%folderdestination%" with the name "%A_LoopFileName%". Please select another destination for the folder "%A_LoopFileName%". IfMsgBox, Cancel ExitApp else continue } else break } FileMoveDir, %A_LoopFileLongPath%, %folderdestination%\%A_LoopFileName% If ErrorLevel { MsgBox, 16, Folder2Junction, Error moving folder...`n`nJunction NOT created. ExitApp } RunWait, %comspec% /c mklink /j "%A_LoopFileName%" "%folderdestination%\%A_LoopFileName%", %A_LoopFileDir% ; FILE_ATTRIBUTE_REPARSE_POINT := 0x400 attributes := DllCall("GetFileAttributes","str",A_LoopFileLongPath) if (attributes != -1 && attributes & 0x400) { MsgBox, 64, Folder2Junction, Done! New Location of Folder:`n`n%folderdestination%\%A_LoopFileName%`n`nA junction pointing there was successfully created. ExitApp } IfExist, %A_LoopFileLongPath% { MsgBox, 16, Folder2Junction, Error: The folder was moved`, but the junction might not have been created. ExitApp } MsgBox, 20, Folder2Junction, Error: The folder was moved`, but the junction could not be created. Would you like the folder moved back to its original location? IfMsgBox, Yes { FileMoveDir, %folderdestination%\%A_LoopFileName%, %A_LoopFileLongPath% If ErrorLevel { MsgBox, 16, Folder2Junction, Error occured while moving folder back to its original location. ExitApp } else MsgBox,, Folder2Junction, Done moving back folder. } ExitApp } Selectdefault: FileSelectFolder, target,,, Select default destination for moving folders (storing folder contents): If ErrorLevel ExitApp RegWrite, REG_SZ, HKEY_CURRENT_USER, Software\Folder2Junction, destinationfolder, %target% return