CSP Posted July 11, 2007 Share Posted July 11, 2007 I'm looking for a way to get a relative path between two folders if two absolute paths were given. Does anyone know of one? Also, is there a way to run a search on function descriptions? It seems like most of my questions will be about asking for the names of functions that does X. I'd like to figure it out myself without bothering you guys, but I can't seem to find a way to search the function docs. Link to comment Share on other sites More sharing options...
Developers Jos Posted July 11, 2007 Developers Share Posted July 11, 2007 (edited) I'm looking for a way to get a relative path between two folders if two absolute paths were given. Does anyone know of one? Also, is there a way to run a search on function descriptions? It seems like most of my questions will be about asking for the names of functions that does X. I'd like to figure it out myself without bothering you guys, but I can't seem to find a way to search the function docs.I have created this UDF and use it in AutoIt3Wrapper_GUI() Func DirGetRelativePath($Source,$target) ; This UDF will get the relative Path to the file ... written by JdeB ; ConsoleWrite('>$Source = ' & $Source & ' $target = ' & $target & @crlf ) $Source = StringReplace($Source,"/","\") $target = StringReplace($target,"/","\") Local $sz_sDrive, $sz_sDir, $sz_sFName, $sz_sExt Local $sz_tDrive, $sz_tDir, $sz_tFName, $sz_tExt _PathSplit($Source, $sz_sDrive, $sz_sDir, $sz_sFName, $sz_sExt) _PathSplit($target, $sz_tDrive, $sz_tDir, $sz_tFName, $sz_tExt) If $sz_sDrive <> $sz_tDrive then return $target EndIf ; Local $a_sDir = StringSplit($sz_sDir,"\") Local $a_tDir = StringSplit($sz_tDir,"\") For $x = 1 to $a_sDir[0] If $a_sDir[$x] <> $a_tDir[$x] Then ExitLoop Next Local $R_Path = "" If $a_sDir[0] >= $a_tDir[0] Then For $y = $x to $a_sDir[0] -1 $R_Path &= "..\" Next For $y = $x to $a_tDir[0] -1 $R_Path &= $a_tDir[$y] & "\" Next $R_Path &= $sz_tFName & $sz_tExt Else For $y = $x to $a_tDir[0] -1 $R_Path &= $a_tDir[$y] & "\" Next $R_Path &= $sz_tFName & $sz_tExt EndIf return $R_Path EndFunc Edited July 11, 2007 by JdeB SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
CSP Posted July 11, 2007 Author Share Posted July 11, 2007 I have created this UDF and use it in AutoIt3Wrapper_GUI()Thanks, that works.One small note is that this function seems to require a trailing \ or / at the end of target path.IE, "c:\windows\system\" => "c:\windows\system32" will result in "..\system32"BUT, "c:\windows\system" => "c:\windows\system32" seems to result in "system32"Nonetheless, thank you. Link to comment Share on other sites More sharing options...
wraithdu Posted July 2, 2008 Share Posted July 2, 2008 (edited) I found a bug if the source dir is totally different than the target dir and its part length is shorter, ie ("C:\Windows", "C:\Users\name\one"). I've modified it a bit, and I think it works correctly under all circumstances now. Any trailing "\" are removed from both source and target first, and the result lacks the preceding "\" as well. Thanks to Jos for the original function! Think I got it this time - expandcollapse popup; function by Jos, modified by wraithdu Func _DirGetRelativePath($source, $target) ; This UDF will get the relative Path to the file ... written by JdeB $source = StringReplace($source,"/","\") $target = StringReplace($target,"/","\") If StringRight($source, 1) == "\" Then $source = StringTrimRight($source, 1) If StringRight($target, 1) == "\" Then $target = StringTrimRight($target, 1) ;~ ConsoleWrite('->$source = ' & $source & ' $target = ' & $target & @crlf ) Local $sz_sDrive, $sz_sDir, $sz_sFName, $sz_sExt Local $sz_tDrive, $sz_tDir, $sz_tFName, $sz_tExt _PathSplit($source, $sz_sDrive, $sz_sDir, $sz_sFName, $sz_sExt) _PathSplit($target, $sz_tDrive, $sz_tDir, $sz_tFName, $sz_tExt) If $sz_sDrive <> $sz_tDrive Then Return $target EndIf Local $a_sDir = StringSplit($sz_sDir,"\") Local $a_tDir = StringSplit($sz_tDir,"\") For $x = 1 to $a_sDir[0] If $a_sDir[$x] <> $a_tDir[$x] Then ExitLoop Next If $x > $a_sDir[0] Then $x -= 1 Local $R_Path = "" If $a_sDir[0] >= $a_tDir[0] Or $x + 3 >= $a_sDir[0] Then For $y = $x to $a_sDir[0] $R_Path &= "..\" Next For $y = $x to $a_tDir[0] - 1 $R_Path &= $a_tDir[$y] & "\" Next $R_Path &= $sz_tFName & $sz_tExt Else $x += 1 For $y = $x to $a_tDir[0] - 1 $R_Path &= $a_tDir[$y] & "\" Next $R_Path &= $sz_tFName & $sz_tExt EndIf return $R_Path EndFunc Edited July 2, 2008 by wraithdu Link to comment Share on other sites More sharing options...
wraithdu Posted August 4, 2008 Share Posted August 4, 2008 Another option - http://www.autoitscript.com/forum/index.php?showtopic=77507 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now