PathGetRelative

If you are currently developing portable freeware or planning to do so, use this forum to discuss technical implementation, seek out like-minded developers for partnership, or solicit interested users for beta testing.
Post Reply
Message
Author
crownixx
Posts: 403
Joined: Sat May 12, 2007 6:26 am

PathGetRelative

#1 Post by crownixx » Sun May 02, 2010 7:07 am

Image
Info
PathGetRelative is used to help you quickly generate the relative path given the working directory and the target file/directory.

Note:
The generated relative path will not have a trailing "\" as it was the
default design from the AutoIT User Defined Function _PathGetRelative

Download
PathGetRelative v0.1 ( 295.67 KB )

User avatar
guinness
Posts: 4118
Joined: Mon Aug 27, 2007 2:00 am
Contact:

Re: PathGetRelative

#2 Post by guinness » Sun May 02, 2010 7:58 am

Very Well Done! Cool you released it with the Source Code. An idea and probably won't be used but how about having drag and drop on the .EXE file itself using $CmdLine, so I drop one folder on the .EXE and then a second and voila it produces a MsgBox and copies to the Clipboard (ClipPut)

Just an idea :!:
Added 179 Applications: Portable

-.-
Posts: 325
Joined: Mon Oct 06, 2008 4:32 pm

Re: PathGetRelative

#3 Post by -.- » Sun May 02, 2010 9:52 am

i like it, doesnt it already do drag/drop? works for me

User avatar
guinness
Posts: 4118
Joined: Mon Aug 27, 2007 2:00 am
Contact:

Re: PathGetRelative

#4 Post by guinness » Sun May 02, 2010 9:58 am

On the GUI it does but I mean the application icon itself. I don't mind doing it! :)
Added 179 Applications: Portable

User avatar
guinness
Posts: 4118
Joined: Mon Aug 27, 2007 2:00 am
Contact:

Re: PathGetRelative

#5 Post by guinness » Mon May 03, 2010 12:16 am

I decided to MOD crownixx wonderful program by adding commandline support!

Place MessageHandler.au3 in the same directory as PathGetRelative.au3 and in MessageHandler.au3 change Opt("OnExitFunc", "CallBack_Exit") to #OnAutoItStartRegister("CallBack_Exit"). It was something I put together pretty quickly, but it's a proof of concept. MessageHandler.au3 is designed by ChrisL, it allows AutoIt scripts to communicate with another.

How it works: 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.

It works commandline but can only accept 1 path/file at a time.

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
I will delete this post at the request of the author.
Last edited by guinness on Sat May 15, 2010 3:38 am, edited 1 time in total.
Added 179 Applications: Portable

crownixx
Posts: 403
Joined: Sat May 12, 2007 6:26 am

Re: PathGetRelative

#6 Post by crownixx » Sat May 08, 2010 7:22 pm

Thanks all. Feel free to modify or add features in PathGetRelative.

guinness, the $commandline feature is a good idea. I'll compile your code later when i got into my pc.

User avatar
guinness
Posts: 4118
Joined: Mon Aug 27, 2007 2:00 am
Contact:

Re: PathGetRelative

#7 Post by guinness » Sat May 08, 2010 11:07 pm

Well no Thanks to you! There's a grammatical error, $Error = "An error unexpected error occurred" should read $Error = "An unexpected error occurred"

Are you looking at adding any new features?
Last edited by guinness on Sun May 09, 2010 1:47 am, edited 1 time in total.
Added 179 Applications: Portable

User avatar
Napiophelios
Posts: 610
Joined: Sun Mar 01, 2009 5:48 pm

Re: PathGetRelative

#8 Post by Napiophelios » Sun May 09, 2010 12:43 am

dude you misspelled grammar :lol:
...and I think its a grammatical error

User avatar
guinness
Posts: 4118
Joined: Mon Aug 27, 2007 2:00 am
Contact:

Re: PathGetRelative

#9 Post by guinness » Sun May 09, 2010 1:46 am

Thats embarrasing! Edited the offending post.

What I meant to say was congratualtions you passed!
Added 179 Applications: Portable

crownixx
Posts: 403
Joined: Sat May 12, 2007 6:26 am

Re: PathGetRelative

#10 Post by crownixx » Fri May 14, 2010 6:50 pm

I having problem compiling code. Anybody know why i got error in the MessageHandler.au3?

Code: Select all

ERROR: Opt() called with illegal argument 1: "OnExitFunc"
guinness wrote:Are you looking at adding any new features?
Nope. I don't have any ideas for it right now...

User avatar
guinness
Posts: 4118
Joined: Mon Aug 27, 2007 2:00 am
Contact:

Re: PathGetRelative

#11 Post by guinness » Sat May 15, 2010 3:37 am

Because this was removed in the new version of AutoIt. If you replace Opt("OnExitFunc", "CallBack_Exit") with #OnAutoItStartRegister("CallBack_Exit") this should work, forgot to mention in my post. (updated!)
Added 179 Applications: Portable

Post Reply