PathX() : Split & modify a filepath easily

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

PathX() : Split & modify a filepath easily

29 Apr 2020, 16:00

PathX() is an alternative solution for SplitPath command. It breaks down a fullpath to individual parts and returns them as an associative array. Note that the results from PathX() will be different from SplitPath command. The common reason a filepath is split is to concatenate them back again to form a different filepath. PathX() can do this in one go. Custom key values can be provided as variadic parameters to substitute individual parts of the returned array.

PathX() is powered by two API functions


PathX(Filepath, [Variadic parameters]).
PathX() splits a filepath and returns filepath parts as an associative array with following keys:

  • 1) Drive
  • 2) Dir
  • 3) Fname (Filename without extension)
  • 4) Ext
  • 5) Folder (Concatenation of 1 & 2)
  • 6) File (Concatenation of 3 & 4)
  • 7) Full (Concatenation of 1,2,3 & 4)
Try the following example to see the key value pairs of the array returned:

Code: Select all

Px := PathX(A_AhkPath)

Msgbox,,  Key and values, % "" 
       . "Drive`t:`t"  . Px.Drive  "`n"
       . "Dir`t:`t"    . Px.Dir    "`n"
       . "Fname`t:`t"  . Px.fname  "`n"
       . "Ext`t:`t"    . Px.Ext    "`n"                 
       . "Folder`t:`t" . Px.Folder "`n"                 
       . "File`t:`t"   . Px.File   "`n"                 
       . "Full`t:`t"   . Px.Full
The first 4 keys Drive, Dir, Fname and Ext can be overwritten with custom values:

Code: Select all

MsgBox,, Change name,         % PathX(A_ScriptName, "Fname:Hello World").Full
MsgBox,, Change drive,        % PathX(A_ScriptName, "Drive:Z:").Full
MsgBox,, Change path to root, % PathX(A_ScriptName, "Dir:\").Full
MsgBox,, Change extension,    % PathX(A_ScriptName, "Ext:.txt").Full
Added in v0.67: _Ext key. See example

PathX() will resolve relative path to fullpath unconditionally:

Code: Select all

MsgBox,, One level up,        % PathX("\..\" . A_ScriptName).Full
MsgBox,, Two levels down, % PathX("Sub_folder\Sub_folder\" A_ScriptName).Full
One may pass multiple parameters as well as shorten the key names to two chars:
The following example shows how to substitute Drive and Fname in one go.
Note: Make sure there are no spaces between Key:Value unless it was intended.

Code: Select all

MsgBox,, New path, % PathX(A_ScriptName, "Dr:X:", "Fn:MyScript").Full
Keys Fprefix and Fsuffix can be used to add prefix/suffix to Fname

Code: Select all

MsgBox,, Prefix Fname,  % PathX(A_ScriptName, "fp:Copy of ").Full
MsgBox,, Suffix Fname,  % PathX(A_ScriptName, "fs:_001").Full
MsgBox,, Prefix/Suffix, % PathX(A_ScriptName, "fp:Backup_", "fs:" . "_" . A_Now).Full
Additional file extension can be kind of prefixed indirectly by suffixing Fname:

Code: Select all

MsgBox,, Prefix extension,  % PathX(A_AhkPath, "fs:.old").Full
Keys Dprefix and Dsuffix may be used to add prefix/suffix to Dir

Code: Select all

MsgBox,, Prefix/Suffix Dir,  % PathX(A_ScriptName, "Dr:N:", "Dp:\Backup", "Ds:001\").Full
Added in v0.66:
Keys Pprefix and Psuffix may be used to add prefix/suffix to Fullpath

Code: Select all

MsgBox,, Prefix fullpath,  % PathX(A_ScriptName, "pp:file:///").Full
MsgBox,, Prefix fullpath,  % PathX(A_ScriptName, "pp:\\?\").Full
MsgBox,, Suffix ADS name,  % PathX(A_ScriptName, "ps::HiddenData").Full


Within Loop, Files, A_LoopFileName is available but no built-in var for base file name (file name without extension).
PathX() can be used as follows:

Code: Select all

Loop Files, %A_AhkPath%\..\*.exe
  MsgBox % PathX(A_LoopFileLongPath).FName


The function:

Code: Select all

PathX(S, P*) {                                ; PathX v0.67 by SKAN on D34U/D35I @ tiny.cc/pathx
Local K,V,N,  U:={},   T:=Format(A_IsUnicode ? "{1:260}" : "{1:520}", ""),   dr:=di:=fn:=ex := T
  For K,V in P
      N := StrSplit(V,":",,2),  K := SubStr(N[1],1,2),  U[K] := N[2]
  DllCall("GetFullPathName", "Str",Trim(S,Chr(34)), "UInt",260, "Str",T, "Ptr",0)
  DllCall("msvcrt\_wsplitpath", "WStr",T, "WStr",dr, "WStr",di, "WStr",fn, "WStr",ex)
Return {"Drive":dr:=u.dr?u.dr:dr,"Dir":di:=(u.dp)(u.di=""?di:u.di)(u.ds),"Fname":fn:=(u.fp)(u.fn
     =""?fn:u.fn)(u.fs),"Ext":ex:=u._e!=""?(ex?ex:u._e):u.ex=""?ex:u.ex,"Folder":(dr)(di),"File"
     :(fn)(ex),"Full":(u.pp)(dr)(di)(fn)(ex)(u.ps) }
}
The function - Readable version
My Scripts and Functions: V1  V2
toralf
Posts: 868
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: PathX() : Split/Modify a filepath easily

30 Apr 2020, 01:31

That is a cool idea/script. Will come handy. Thanks for sharing
ciao
toralf
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: PathX() : Split/Modify a filepath easily

30 Apr 2020, 03:44

Thanks dear toralf :) :thumbup:
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: PathX() : Split/Modify a filepath easily

30 Apr 2020, 15:35

What a useful function - thank you! And, as always, your documentation and examples are great.
Regards,
burque505
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: PathX() : Split/Modify a filepath easily

30 Apr 2020, 16:21

burque505 wrote: What a useful function - thank you! And, as always, your documentation and examples are great.
Thanks for the feedback :) :thumbup:

Just added one more example:


Within Loop, Files, A_LoopFileName is available but no built-in var for base file name (file name without extension).
PathX() can be used as follows:

Code: Select all

Loop Files, %A_AhkPath%\..\*.exe
  MsgBox % PathX(A_LoopFileLongPath).FName
ozzii
Posts: 486
Joined: 30 Oct 2013, 06:04

Re: PathX() : Split & modify a filepath easily

01 May 2020, 03:24

Like always, very great function and examples.
Thank you
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: PathX() : Split & modify a filepath easily

03 May 2020, 03:34

@ozzii :) :thumbup:
My Scripts and Functions: V1  V2
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: PathX() : Split & modify a filepath easily

03 May 2020, 05:59

Assume-global mode wrote: If a function needs to access or create a large number of global variables, it can be defined to assume that all its variables are global (except its parameters) by making its first line either the word "global" or the declaration of a local variable.
My bold. Meaning that if you forget to declare one, it is assumed to be global. See,

Code: Select all

#include pathx.ahk
listvars
msgbox
I recommend you do

Code: Select all

pathx(...) {
	local
	; ...
}
Cheers, and thanks for sharing.
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

PathX() : code updated

03 May 2020, 07:08

Helgef wrote:if you forget to declare one, it is assumed to be global.
Yes, I know... somehow I missed. I usually call ListVars within the function to see which is where.
I need a haircut badly :(
Helgef wrote:Cheers, and thanks for sharing.
Thanks for caring. You're an invaluable asset to this community! :)


Code updated: Fixed local variables leaking into Global space
My Scripts and Functions: V1  V2
User avatar
SirSocks
Posts: 360
Joined: 26 Oct 2018, 08:14

Re: PathX() : Split & modify a filepath easily

03 May 2020, 08:47

This is great! The examples are excellent! Thank you very much.
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: PathX() : Split & modify a filepath easily

03 May 2020, 09:06

@SirSocks :) :thumbup:
My Scripts and Functions: V1  V2
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

PathX() v0.66

07 May 2020, 09:51

Code: Select all

/*
Code updated : v0.66
Keys Pprefix and Psuffix may be used to add prefix/suffix to Fullpath
*/

MsgBox,, Prefix fullpath,  % PathX(A_ScriptName, "pp:file:///").Full
MsgBox,, Prefix fullpath,  % PathX(A_ScriptName, "pp:\\?\").Full
MsgBox,, Suffix ADS name,  % PathX(A_ScriptName, "ps::HiddenData").Full
My Scripts and Functions: V1  V2
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

PathX() v0.67

17 May 2020, 17:25

Bug fix:
I was tring to rename a file to 0.0 with PathX(File, "Fn:0", "Ext:.0"), but function was returning filepath intact
since both 0 and .0 evaluates to false. Fixed code to check if keys are blank instead of evaluating a true.

New in 0.67

File extension can be forced with Ext key for File save dialog

Code: Select all

FileSelectFile, sFile, S2, %A_ScriptDir%, Save file, *.txt
If not (ErrorLevel)
   Msgbox % sFile := PathX(sFile, "ext:.txt").Full  ; extension .txt is enforced
Now added a _Ext key that will add extension only if not already present

Code: Select all

FileSelectFile, sFile, S2, %A_ScriptDir%, Save file, *.*
If not (ErrorLevel)
   Msgbox % sFile := PathX(sFile, "_ext:.txt").Full ; .txt is appended only if user doesn't specify any
jetrotal
Posts: 2
Joined: 29 Nov 2020, 12:45

Re: PathX() : Split & modify a filepath easily

01 Feb 2021, 10:52

@SKAN

One suggestion:
If we use pathX function on a folder

Code: Select all

pathX("C:\ My files")
fill the Fname with the folder name.

now it returns "", but it could return "My files".
Mika_erdo
Posts: 41
Joined: 30 Jul 2020, 17:23

Re: PathX() : Split & modify a filepath easily

01 Feb 2021, 23:46

SKAN you is the man.
Much appreciated

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 86 guests