Splitpath with command line parameters

Get help with using AutoHotkey and its commands and hotkeys
zotune
Posts: 83
Joined: 17 Nov 2014, 17:57

Splitpath with command line parameters

27 Jul 2018, 04:59

Hi,

Is there a splitpath function that supports command line parameters? I tried to make my own but it fails on certain paths.

Code: Select all

;Working = C:\Program Files\Test\Test.exe -title someprogram
;Not working = C:\Program Files (x86)\Test\Test\Test.exe -inifile "C:\ProgramData\Test\Test\Config\Test.ini"

SplitPath(ProcessPath="",byref Directory="",byref Process="",byref Parameters="",byref Extension="",byref ProcessNoExt="")
{
	;Split directory, extension, and parameters... Regular splitpath doesn't work with parameters
	SplitPath, ProcessPath, Process, Directory, ExtensionAndParameters, ProcessNoExt ; ---> ExtensionAndParameters and ProcessNoExt fail here :(
	Extension:=StrSplit(ExtensionAndParameters,A_Space)
	Parameters:=SubStr(ExtensionAndParameters,(StrLen(Extension[1])+2))
	Extension:=Extension[1]
	if (Parameters != "")
		Process:=StrReplace(Process," " Parameters,"")
}
I only need it because ShellRun() library function separates file and parameters
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Splitpath with command line parameters

27 Jul 2018, 07:22

- You might want to separate the command line string into different parts, in a similar way to A_Args.
- Here's a link to a post with a function I wrote, which also mentions a function written by SKAN.
conversion logic, v1 = -> v1 := -> v2, two-way compatibility - Page 3 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 73#p134073
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
zotune
Posts: 83
Joined: 17 Nov 2014, 17:57

Re: Splitpath with command line parameters

14 Oct 2018, 07:00

Thanks. I ended up with this:

Code: Select all

SplitPath(ProcessPath="",byref Directory="",byref Process="",byref Parameters="",byref Extension="",byref ProcessNoExt="")
{
	Static SplitPath
	if !IsObject(SplitPath)
		SplitPath:={}

	if IsObject(SplitPath[ProcessPath]) ;already indexed
	{
		Directory:=SplitPath[ProcessPath,"Directory"]
		Process:=SplitPath[ProcessPath,"Process"]
		Parameters:=SplitPath[ProcessPath,"Parameters"]
		Extension:=SplitPath[ProcessPath,"Extension"]
		ProcessNoExt:=SplitPath[ProcessPath,"ProcessNoExt"]
		Return
	}
	else ;must index based on file exist
	{	
		Parameters:=""
		for a, File in StrSplit(ProcessPath," ")
		{
			Result.=File " "
			if InStr(FileExist(Result), "A")
			{
				Parameters:=StrReplace(ProcessPath,Result)
				if (Parameters = ProcessPath)
					Parameters:=""
				ProcessPathNoPar:=Trim(Result," ")
				break ;quit
			}
		}

		; Split directory, extension, and parameters... Regular splitpath doesn't work with parameters
		SplitPath, ProcessPathNoPar, Process, Directory, Extension, ProcessNoExt

		SplitPath[ProcessPath]:={Directory:Directory,Process:Process,Parameters:Parameters,Extension:Extension,ProcessNoExt:ProcessNoExt}
		Return
	}
}

Return to “Ask For Help”

Who is online

Users browsing this forum: AutoHotUser, mikeyww, PuzzledGreatly, Renegaduh and 37 guests