RefinePath()

Post your working scripts, libraries and tools
User avatar
Learning one
Posts: 119
Joined: 04 Oct 2013, 13:59
Location: Croatia
Contact:

RefinePath()

22 Jan 2015, 12:34

RefinePath() allows you to use relative paths, absolute paths, and it may ease your life if you are working with portable drives. Some examples:

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

Run, % RefinePath("Joe USB3:\Music\Song.mp3")	; use drive name (label) instead of letter - useful for USB sticks.
Run, % RefinePath("\Documents\Info.txt") ; "A_ScriptDrive" effect - start with a backslash to refer to the same drive where the script is
Run, % RefinePath("..\..\App.exe") ; go 2 levels up relative to the current folder (A_ScriptDir by default) and run an App located there
; and more functionalities...
For more info, see the comments in the code. Download: RefinePath.ahk
Last edited by Learning one on 11 Feb 2015, 15:58, edited 1 time in total.
User avatar
Chef
Posts: 50
Joined: 14 Nov 2013, 13:01

Re: RefinePath()

22 Jan 2015, 22:11

Nice, but isn't it supposed to return the full path?
Your function returns the exact input.

Test code:

Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus



Image

I have an unfinished but similar function, thanks to emmanuel d

Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus

User avatar
evilC
Posts: 3461
Joined: 27 Feb 2014, 12:30

Re: RefinePath()

23 Jan 2015, 06:55

So this always returns an absolute path, even if you enter relative?

Sounds useful, and not always for the most obvious reasons... sometimes in code you end up with silly paths like ..\folder\..\folder\..\folder, I take it tames those properly?
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

Re: RefinePath()

23 Jan 2015, 07:24

I use this for simple cases: abs_path := ComObjCreate("Scripting.FileSystemObject").GetAbsolutePathName(pathspec), pathspec is relative to A_WorkingDir
User avatar
Learning one
Posts: 119
Joined: 04 Oct 2013, 13:59
Location: Croatia
Contact:

Re: RefinePath()

23 Jan 2015, 10:47

@Chef: Ooops, thanks for report, I updated the code. Try again, it should work fine now. Btw, when you use variable, you have to add it to the variables collection first - see the documentation. See also changes below; res\folder is no longer valid - you have to use backslash at beginning; \res\folder.
@evilC:Yes, it returns absolute path (if it is possible to retrieve it). It doesn't tame things like ..\folder\..\folder\..\folder yet. Currently it only supports double dots segments at the beginning of the path like ..\..\..\folder
@Coco:Nice :)

* * *

RefinePath() updated to v1.01
  • fixed - bugs
  • improved - the code doesn't use FileExist() any more - that was the wrong approach
  • changed - to refer to A_ScriptDir, you have to start with backslash. Example: RefinePath("\SubFolder\button.png"). In previous version, that first backslash was optional.
  • changed - RefinePath.MapDrives() method is not automatically called any more. For details see documentation.

P.S. maybe RefinePath should be renamed to GetAbsolutePath ?
carno
Posts: 79
Joined: 20 Jun 2014, 16:48

Re: RefinePath()

23 Jan 2015, 14:50

How about simply calling it GetPath?
User avatar
joedf
Posts: 5825
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: RefinePath()

23 Jan 2015, 16:21

FilePath() like FileExist()
Wamker

Re: RefinePath()

23 Jan 2015, 21:16

There are many functions to work with path, so path_<...> is good because it will be easy to keep it in your lib.
tuncay
Posts: 16
Joined: 28 May 2014, 12:36

Re: RefinePath()

06 Feb 2015, 18:35

Learning one wrote:[*]changed - to refer to A_ScriptDir, you have to start with backslash. Example: RefinePath("\SubFolder\button.png"). In previous version, that first backslash was optional.


Why is this changed? No backslash "path" or ".\path" could start from current working dir.
User avatar
Learning one
Posts: 119
Joined: 04 Oct 2013, 13:59
Location: Croatia
Contact:

Re: RefinePath()

07 Feb 2015, 04:04

Ok, I plan to do the following changes:
A_ScriptDrive - start with a backslash

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

RefinePath("\SubFolder\button.png") ; example output: "C:\SubFolder\button.png"
RefinePath("\SubFolder")
A_ScriptDir - omit a backslash or start with ".\"

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

RefinePath("SubFolder\button.png") ; example output: "C:\Scripts\SubFolder\button.png"
RefinePath("SubFolder")
RefinePath(".\SubFolder\button.png")
RefinePath(".\SubFolder")

The following will remain the same:
Level(s) up

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

RefinePath("..\MyScript.ahk")		; one level up - example output: "C:\MyScript.ahk"
RefinePath("..\..\CCleaner.exe") ; two levels up
Drive labels

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

RefinePath("Joe USB3:\Music\Song.mp3") ; example output: "F:\Music\Song.mp3"
RefinePath("Joe USB3:\")
Variables (in unusual way)

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

RefinePath.Variables.Insert("variable", "C:\Music\Metallica")
RefinePath("?variable?\Master Of Puppets.mp3") ; example output: "C:\Music\Metallica\Master Of Puppets.mp3"
RefinePath("?variable?\One.mp3")
RefinePath("?A_ScriptDir?\Image.png") ; use built-in wariable (in unusual way)
Absolute paths

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

RefinePath("F:\Music\Song.mp3") ; example output: "F:\Music\Song.mp3" - exactly the same as input
Misc - changing default options

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

RefinePath.RelativeTo := "D:\AnyFolder"	; default is A_ScriptDir
RefinePath.RelativeTo := A_WorkingDir
RefinePath.VariableSign := "%" ; default is ?
Any suggestions/tips? Does it look OK and work as you expect now?
P.S. tuncay, nice to see you're back to AHK ;)
tuncay
Posts: 16
Joined: 28 May 2014, 12:36

resolvePath()

07 Feb 2015, 07:31

Here is my attempt of this issue. I tried to resolve the multi level dot parts. Dot or nothing in front of path means relative to current working dir. Backslash means scripts directory. I did not test fully here, just wrote it today (got bored and I like to play with strings). Off course you can borrow any structure to enhance your script. This is only a basic idea how it could be done without checking much or all the other integrated enhancements like variables. Performance wise, I think its not fast, but that does not matter to me. And I am doing it in old fashion, as I was absent for years. Feel free to modify and republish any part.

And if someone copies the script, so please don't remove the license header.

Code: [Select all] [Expand] [Download] (test_resolvePath.ahk)GeSHi © Codebox Plus



Code: [Select all] [Expand] [Download] (resolvePath.ahk)GeSHi © Codebox Plus

User avatar
Learning one
Posts: 119
Joined: 04 Oct 2013, 13:59
Location: Croatia
Contact:

Re: RefinePath()

11 Feb 2015, 16:05

RefinePath() updated to v1.02

Thanks for demonstrating your attempt of this issue Tuncay.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: lsh and 14 guests