- Offizieller Beitrag
Kommandozeilen sind immer gleich aufgebaut:
Teil_1: eine ausführbare Datei mit/ohne Pfad
Teil_2: Parameter 1
...
Teil_n: Parameter n -1
Im Normalfall sollten Pfade so angegeben werden, dass sie bei enthaltenen Leerzeichen in Stringbegrenzer gekapselt werden. Leider halten sich nicht alle Funktionen daran (Registry Einträge).
Deshalb habe ich diese Funktion erstellt, die bei nicht vorhandenen Stringbegrenzern nach der ersten angegebenen ausführbaren Datei sucht und damit den Pfad für diese Datei abschließt.
Alle anderen Parameter sind durch Leerzeichen getrennt.
#include 'CmdLineParse.au3'
Local $sCmd = 'C:\Program Files\Intel\bin\iWrap.exe /CMD:7 %1'
Local $aRes = _CmdLine_Parse($sCmd)
For $i = 0 To UBound($aRes) -1
ConsoleWrite($aRes[$i] & @CRLF)
Next
;-- TIME_STAMP 2019-07-17 19:28:05
Opt('MustDeclareVars', 1)
; #FUNCTION# ====================================================================================================================
; Name ..........: _CmdLine_Parse
; Description ...: Splits a command line into its elements. Standard separator between parts is the space.
; If this character is part of path or arguments, they must be enclosed in string delimiters.
; The first part of command line is a executable file path. The function recognise this also with spaces in it and none
; string marker encapsulation.
; Syntax ........: _CmdLine_Parse($_sLine[, $_sDelimParam = ' '])
; Parameters ....: $_sLine - The command line to be parsed.
; $_sDelimParam - [optional] Separator between parts. Default is ' '.
; Return values .: Array, each part of command line in one entry.
; Author ........: BugFix
; ===============================================================================================================================
Func _CmdLine_Parse($_sLine, $_sDelimParam=' ')
Return StringSplit(StringTrimRight(__ParseRecursive($_sLine, $_sDelimParam), 1), Chr(1), 2)
EndFunc ;==> _CmdLine_Parse
Func __ParseRecursive($_sLine, $_sDelimParam, $_iCall=1)
Local $sDelimResult = Chr(1), $sResult = '', $sTmp, $sOut
Local $sDelimString = ''
Local $s1st = StringMid($_sLine, 1, 1)
If $s1st = "'" Or $s1st = '"' Then $sDelimString = $s1st
Local $sPart, $sPattExe, $aMatch
If $sDelimString <> '' Then
$sPart = StringFormat('%s[^%s]+%s', $sDelimString, $sDelimString, $sDelimString)
Else
; Check if executable file path has spaces in it -- only for 1st call
If $_iCall = 1 Then
Local $aExec[] = ['.exe','.com','.bat','.cmd','.scr']
Local $iMatch, $iPos = 1024, $sExt = ''
For $i = 0 To 4
$iMatch = StringInStr($_sLine, $aExec[$i])
If $iMatch > 0 And $iMatch < $iPos Then
$iPos = $iMatch
$sExt = $aExec[$i]
EndIf
Next
If $sExt = '' Then ; none executable in line, use part to first space
$sPart = StringFormat('[^%s]+', $_sDelimParam)
Else
$sPart = StringFormat('(?i).+%s', $sExt)
EndIf
Else
$sPart = StringFormat('[^%s]+', $_sDelimParam)
EndIf
EndIf
$aMatch = StringRegExp($_sLine, $sPart, 1)
$sResult &= $aMatch[0] & $sDelimResult
$sTmp = StringTrimLeft($_sLine, StringLen($sResult))
If StringLen($sTmp) > 0 Then
$_iCall += 1
$sOut = $sResult & __ParseRecursive($sTmp, $_sDelimParam, $_iCall)
Else
$sOut = $sResult
EndIf
Return $sOut
EndFunc
Alles anzeigen
EDIT:
Kleine formelle Änderung im Skript. Ich hatte nicht auf dem Schirm, dass AutoIt in RegEx-Funktionen das insensitiv Flag hat '(?i)'. Hatte daher die Lua-Variante: '[upper lower]' gebastelt.
Hier im Text geändert, nicht im Anhang - bitte selbst anpassen bei Bedarf (funktionell nicht erforderlich).