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.
GetFullPathName (kernel32.dll):
The path passed to the function will be converted to a full path unconditionally. If only a filename is passed, it will be resolved to a full path by prefixing the filename with A_WorkingDir. Any relative path will be converted to full path. For example: A_AhkPath . "\..\License.txt" would be converted to "C:\Program Files\AutoHotkey\License.txt" (assuming you did a default install.). If you are passing a fullpath, the first two chars must be a proper drive letter.. means if you pass a path like a file:/// URL, then it will be unconditionally prefixed with A_WorkingDir. GetFullPathName() will also clean up the filepath. It will convert any forward slashes present in filepath to back slashes and then remove any redundant ones. For eg. D:\/Folder\/\/\\\\file.ext will be cleaned up to D:\Folder\file.ext before it is split into parts. GetFullPathName() will fail if null filepath is passed or filepath is longer than MAX_PATH (260 TChars)
_wsplitpath (msvcrt.dll)
This function breaks down a filepath differently when compared to the output of SplitPath command. It splits a filepath into 4 parts (red/blue/red/blue) as follows:
Example path: C:\Program Files\AutoHotkey\AutoHotkey.exe
C: ; Drive - first 2 chars
\Program Files\AutoHotkey\ ; Dir - Every thing after first colon up till the last slash (last slash will be included)
AutoHotkey ; Fname - Every thing in between last slash and last period
.exe ; Ext - The last period and every thing after it
If anyone wants to test and validate my above statements, use two colons :: after drive letter as well as multiple slashes before the filename.
Then one can easily see how _wsplipath() does the chop chop.
_wsplitpath() will fail in any of the individual filepath part exceeds MAX_PATH (260 TChars).
The array returned by PathX() will contain all the four parts namely Drive, Dir, Fname, Ext. and additionally: Folder (Drive+Dir), File (Fname+Ext) and Full (Drive+Dir+Fname+Ext). The reason for returning the fullpath is that PathX() accepts variadic parameters through which any or 3 of the filepath parts can be replaced with new values. The Full key (in returned array) will contain the modified filepath as value.
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:
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.
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,
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
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