. It was something I put together pretty quickly, but it's a proof of concept.
If you want to find a relative path of just one file and don't want to go through the GUI, you simply drag & drop the working directory over the executable icon and then drag & drop the path/file you want to make relative. The script will exit in 10 seconds if the user hasn't dropped the path/file they wish to make relative.
E.G. "PathGetRelative.exe" "C:\From\" and hit 'Enter' then "PathGetRelative.exe" "C:\To\" and hit 'Enter,' voila a MsgBox will appear, but there is 10 seconds to do it in!
Code: Select all
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=Clipboard.ico
#AutoIt3Wrapper_Outfile=..\PathGetRelative.exe
#AutoIt3Wrapper_Res_Comment=PathGetRelative
#AutoIt3Wrapper_Res_Description=PathGetRelative
#AutoIt3Wrapper_Res_Fileversion=0.1
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
Global $ConfigFile = @ScriptDir & "\PathGetRelative.ini"
$position_x = IniRead($ConfigFile, "Window", "x", -1)
$position_y = IniRead($ConfigFile, "Window", "y", -1)
#NoTrayIcon
#include <File.au3>
#include "MessageHandler.au3"
#include <Misc.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Global $CommandLineExit, $To, $Relative, $sFrom, $sTo, $RelativePath, $Checkbox1, $Error = "An error unexpected error occurred"
If _Singleton("PathGetRelative", 1) = 0 Then
$Remote_ReceiverID_Name = "PathGetRelativeCmdLine"
_SendData($CmdLine[1], $Remote_ReceiverID_Name)
Exit
Else
If $CmdLine[0] <> 0 Then
$From = $CmdLine[1]
$Local_ReceiverID_Name = "PathGetRelativeCmdLine"
_SetAsReceiver($Local_ReceiverID_Name)
_SetReceiverFunction("Received")
Do
Sleep(50)
$CommandLineExit += 1
Until $CommandLineExit = 200 Or $To <> ""
If $CommandLineExit = 200 Then
MsgBox(0, "Error", "The operation timed out")
Exit
EndIf
Generate($From, $To)
If $Error = "" Then
MsgBox(0, "Relative path was copied to the clipboard", $Relative)
Exit
Else
MsgBox(0, "Error", $Error)
Exit
EndIf
Else
Main()
EndIf
EndIf
Func Main()
#Region ### START Koda GUI section ###
$Form1 = GUICreate("PathGetRelative", 506, 209, $position_x, $position_y, -1, BitOR($WS_EX_ACCEPTFILES, $WS_EX_WINDOWEDGE))
$sFrom = GUICtrlCreateInput("<Drag & Drop>", 8, 32, 385, 21)
$sTo = GUICtrlCreateInput("<Drag & Drop>", 8, 88, 385, 21)
$RelativePath = GUICtrlCreateInput("", 8, 144, 385, 21)
$Label1 = GUICtrlCreateLabel("Select working directory [FROM]", 8, 8, 160, 17)
$Label2 = GUICtrlCreateLabel("Select destination file/directory [TO]", 8, 64, 176, 17)
$Label3 = GUICtrlCreateLabel("The relative path FROM working directory TO destination is", 8, 120, 283, 17)
$Button1 = GUICtrlCreateButton("Generate", 160, 176, 75, 25, 0)
$Button2 = GUICtrlCreateButton("Exit", 248, 176, 75, 25, 0)
$Button3 = GUICtrlCreateButton("Dir", 400, 32, 43, 25, BitOR($BS_ICON, $WS_GROUP))
GUICtrlSetImage(-1, @SystemDir & "\shell32.dll", -4, 0)
$Button4 = GUICtrlCreateButton("File", 400, 88, 43, 25, BitOR($BS_ICON, $WS_GROUP))
GUICtrlSetImage(-1, @SystemDir & "\shell32.dll", -135, 0)
$Button5 = GUICtrlCreateButton("Dir", 448, 88, 43, 25, BitOR($BS_ICON, $WS_GROUP))
GUICtrlSetImage(-1, @SystemDir & "\shell32.dll", -4, 0)
$Button6 = GUICtrlCreateButton("Copy to clipboard", 400, 144, 91, 25, $WS_GROUP)
$Checkbox1 = GUICtrlCreateCheckbox("Always on top", 8, 184, 75, 17, BitOR($BS_CHECKBOX, $BS_AUTOCHECKBOX, $BS_PUSHLIKE, $WS_TABSTOP))
$About = GUICtrlCreateButton("About", 416, 184, 75, 17, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
SetWindow()
EnableDragDrop()
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
SaveConfig()
Exit
Case $Button2
SaveConfig()
Exit
Case $Button1
Generate(GUICtrlRead($sFrom), GUICtrlRead($sTo))
Case $Button3
$var = FileSelectFolder("Choose a folder.", "")
If Not @error Then
GUICtrlSetData($sFrom, $var)
EndIf
Case $Button5
$var = FileSelectFolder("Choose a folder.", "")
If Not @error Then
GUICtrlSetData($sTo, $var)
EndIf
Case $Button4
$var = FileOpenDialog("Choose a file", @ScriptDir, "All files (*.*)")
If Not @error Then
GUICtrlSetData($sTo, $var)
EndIf
Case $Button6
$value = GUICtrlRead($RelativePath)
If $value <> "" Then
ClipPut($value)
MsgBox(0, "", "Relative path is copied to the clipboard")
EndIf
Case $Checkbox1
$status = GUICtrlRead($Checkbox1)
If $status = $GUI_CHECKED Then
WinSetOnTop("PathGetRelative", "", 1)
ElseIf $status = $GUI_UNCHECKED Then
WinSetOnTop("PathGetRelative", "", 0)
EndIf
Case $About
MsgBox(64, "About", "PathGetRelative v0.1 (MOD)")
Case $GUI_EVENT_DROPPED
Select
Case @GUI_DropId = $sFrom
$controlID = $sFrom
GUICtrlSetData($controlID, @GUI_DragFile)
Case @GUI_DropId = $sTo
$controlID = $sTo
GUICtrlSetData($controlID, @GUI_DragFile)
EndSelect
EndSwitch
WEnd
EndFunc ;==>Main
Func EnableDragDrop()
GUICtrlSetState($sFrom, $GUI_DROPACCEPTED)
GUICtrlSetState($sTo, $GUI_DROPACCEPTED)
EndFunc ;==>EnableDragDrop
Func Generate($From = "", $To = "")
$Relative = _PathGetRelative($From, $To)
If @error = 1 Then
$Error = "Working directory and destination target are the same"
If $CmdLine = 0 Then MsgBox(48, "Error", $Error)
GUICtrlSetData($RelativePath, "")
ElseIf @error = 2 Then
$Error = "A relative path is impossible"
If $CmdLine = 0 Then MsgBox(48, "Error", $Error)
GUICtrlSetData($RelativePath, "")
Else
$Error = ""
GUICtrlSetData($RelativePath, $Relative)
If $CmdLine <> 0 Then ClipPut($Relative)
Return $Relative
EndIf
EndFunc ;==>Generate
Func Received($Text)
$To = $Text
EndFunc ;==>Received
Func SaveConfig()
$status = GUICtrlRead($Checkbox1)
If $status = $GUI_CHECKED Then
IniWrite($ConfigFile, "Window", "OnTop", 1)
ElseIf $status = $GUI_UNCHECKED Then
IniWrite($ConfigFile, "Window", "OnTop", 0)
EndIf
$position = WinGetPos("PathGetRelative")
IniWrite($ConfigFile, "Window", "x", $position[0])
IniWrite($ConfigFile, "Window", "y", $position[1])
Exit
EndFunc ;==>SaveConfig
Func SetWindow()
$status = IniRead($ConfigFile, "Window", "OnTop", 0)
If $status = 1 Then
GUICtrlSetState($Checkbox1, $GUI_CHECKED)
WinSetOnTop("PathGetRelative", "", 1)
ElseIf $status = 0 Then
GUICtrlSetState($Checkbox1, $GUI_UNCHECKED)
WinSetOnTop("PathGetRelative", "", 0)
EndIf
EndFunc ;==>SetWindow