AutoHotKey (AHK) SplitPath fix

by Andy Prevost

Thursday May 21 2026

AutoHotKey, also known as AHK, is an incredibly flexible scripting language. Very much basic-like. The "programming" is mostly linear, with some object-oriented functionality. 

As flexible as it is, it has short-comings. 

One of those is the function SplitPath. There are a lot of complaints about it from the user community. Some have come up with new functions to replace SplitPath. I must point out that I like the concept of SplitPath. Although not in the documentation, you can use it to generate variables based on a "path" that you give it. Those returns are: OutFileName, OutDir, OutExtension, OutNoExtension, and OutDrive.

The syntax for that is:
SplitPath MyPath, &OutFileName, &OutDir, &OutExtension, &OutNameNoExt, &OutDrive

An alternate syntax is simply:
Out := SplitPath MyPath

Then you can call each individual variable as an object as in:
Msgbox Out.OutFileName
Msgbox Out.OutDir
Msgbox Out.OutExtension
Msgbox Out.OutNameNoExt
Msgbox Out.OutDrive

Which to use depends on your circumstances.

It works as expected, except when the path includes switches. That's requires a fix from the original developers, but that doesn't look forthcoming any too soon.

Other developers have created work arounds, some quite extensive and most with extras that I really don't need. I'd just like the SplitPath to work as it is supposed to. If the path contains any switches, the switches must be removed from the return data.

Here's an example using:
url := "C:\Program Files\Google\Chrome\Application\chrome.exe –incognito"

Without a fix, SplitPath would return the filename as 'chrome.exe –incognito' and would return the extension as 'exe –incognito'. These are horrible errors.

I've written two utilities to solve these errors. Both work together to resolve the issue.

; remotes any switches (if the variable contains a space followed by a hyphen)
; and returns only the left portion of the variable
; by Andy Prevost
; license: MIT
stripSwitch(str) {
  If InStr(str, " -") {
    tStr := StrSplit(str, " -")
    str :=  tStr[1]
  }
  return str
}

; a direct replacement for "SplitPath" regular function with fixes
; removes switches from Paths (particularly software programs)
; returns exactly the same variables and value structures
; by Andy Prevost
; license: MIT
SplitPathX(Path, &OutFileName?, &OutDir?, &OutExtension?, &OutNameNoExt?, &OutDrive?) {
  SplitPath(Path, &OutFileName, &OutDir, &OutExtension, &OutNameNoExt, &OutDrive)
    OutFileName := stripSwitch(OutFileName)
    OutExtension := stripSwitch(OutExtension)
  return {OutFileName: OutFileName, OutDir: OutDir, OutExtension: OutExtension, OutNameNoExt: OutNameNoExt, OutDrive: OutDrive}
}

Obviously you need to change the function name, instead of calling "SplitPath", you need to call "SplitPathX" ... but the syntax is the same, and it will return variables and can also be used as objects.

Note: if using it as an object, you need to use parentheses as in:

Out := SplitPathX(url)

 

Next ▶

Post a Comment