RefinePath()

Post a reply


In an effort to prevent automatic submissions, we require that you type the text displayed into the field underneath.
Smilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :| :mrgreen: :geek: :ugeek: :arrow: :angel: :clap: :crazy: :eh: :lolno: :problem: :shh: :shifty: :sick: :silent: :think: :thumbup: :thumbdown: :salute: :wave: :wtf: :yawn: :facepalm: :bravo: :dance: :beard: :morebeard: :xmas: :HeHe: :trollface: :cookie: :rainbow: :monkeysee: :monkeysay: :happybday: :headwall: :offtopic: :superhappy: :terms: :beer:
View more smilies

BBCode is ON
[img] is OFF
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: RefinePath()

Re: RefinePath()

Post by Learning one » 11 Feb 2015, 16:05

RefinePath() updated to v1.02

Thanks for demonstrating your attempt of this issue Tuncay.

resolvePath()

Post by tuncay » 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]GeSHi © Codebox Plus



Code: [Select all] [Expand]GeSHi © Codebox Plus

Re: RefinePath()

Post by Learning one » 07 Feb 2015, 04:04

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

Code: [Select all]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]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]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]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]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]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]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 ;)

Re: RefinePath()

Post by tuncay » 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.

Re: RefinePath()

Post by Wamker » 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.

Re: RefinePath()

Post by joedf » 23 Jan 2015, 16:21

FilePath() like FileExist()

Re: RefinePath()

Post by carno » 23 Jan 2015, 14:50

How about simply calling it GetPath?

Re: RefinePath()

Post by Learning one » 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 ?

Re: RefinePath()

Post by Coco » 23 Jan 2015, 07:24

I use this for simple cases: abs_path := ComObjCreate("Scripting.FileSystemObject").GetAbsolutePathName(pathspec), pathspec is relative to A_WorkingDir

Re: RefinePath()

Post by evilC » 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?

Re: RefinePath()

Post by Chef » 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]GeSHi © Codebox Plus



Image

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

Code: [Select all] [Expand]GeSHi © Codebox Plus

RefinePath()

Post by Learning one » 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]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

Top