Jump to content

Recommended Posts

I forgot to update this, I had responded to a topic with an updated PCRE for parsing individual items/options, so here's the updated code:

_StringParseParameters: Parse Parameters in a string: Returns an array as if a command-line parser split the elements, or if nothing found an @error.

Note there is a look-behind and look-ahead assertion to prevent grabbing quotes, while also allowing for cases where there are empty quotes (""):

Local $aParsedArray=StringRegExp($sStringToParse,'((?<=\s"|^")[^"]+(?=")|[^\s"]+)',3)

The only thing I'd do beforehand is expand any environment strings like %windir%. All you'd need for that is to just set the "ExpandEnvStrings" option, assign the string to itself (looks dumb, but accomplishes the needed expansion), then reset the option. (or call the ExpandEnvironmentStrings API function)

The old version of this can still be found in a post I had made in the thread ' - it also parses comma-separated arguments, though retaining quotes. That could easily be remedied though.

Edited by Ascend4nt
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By prizm1
      I am trying to pass multi-file selections from Windows Explorer into the $CmdLine argument parameters array. For example, and these are not the literal program/project names, I can pass three full path file names via both Window's Run dialog and Terminal into the $CmdLine array, i.e, "c:\Dir\Program.exe" "D:\Test\file1.txt" "D:\Test\file2.txt" "D:\Test\file3.txt". When checked in the running program, $CmdLine index [1] has the correct value of 3, index [1] is correct with "D:\Test\file1.txt", index [2] with "D:\Test\file2.txt" and index [3] with "D:\Test\file3.txt". I get the same correct results in $CmdLine array when I start Program.exe with the same three file parameters using the Windows Terminal.
      Also, I have added a HKEY_CLASSES_ROOT\*\shell\command key to the Window's registry. The registry entry is with a "C:\Program.exe" "%1" "$2" "%3" string value to allow for renaming file(s) from Windows Explorer via the right-click menu. With just one parameter placeholder, "%1" in the string value, a selected file will appear in $CmdLine. $CmdLine[0] is 1 and $Cmdline[1] contains the selected full path file name. Unfortunately, when I change the registry string value to include two more passing arguments, "%2" "%3", $CmdLine[0] =3 (as it should since three file path names are passed to Program.exe), $CmdLine[1] contains "D:\Test\file1.txt", but $CmdLine[2] and [3] are just empty strings.
      It appears to me that Windows is not passing the second and third ("%2" and "%3") selected file's to Autoit, but just the first parameter "%1".
      Is there some way that I can select more than one file in Window's Explorer and pass the full file paths into the $CmdLine array?
      I know that AutoIt has the _WinAPI_GetOpenFileName function, but I would like to select files directly from Windows Explorer to bring into $CmdLine.
      File Renaming Application Reg Key.reg
    • By rudi
      once more I have to see, that my knowledge about XML is too poor, even to use this existing XML UDF
      If it should be a better approach to make use of StringRegEx, or maybe a totally different approach, any suggestions will be mostly appreciated.
      I downloaded the ZIP XML_1.1.1.13 and tried to get how to use it from the sample AU3 files included in that ZIP file, well, I don't really get it😵
      What I want to do:
      My mobile phone's backup contain all the contacts in an XML file. This is an example of such a file, backupinfo.xml, shortened to show just one fictive entry:
      <?xml version="1.0" encoding="utf-8" ?> <ContactRecords> <contact> <structuredName> <displayName>Max Mustermann</displayName> <givenName>Max</givenName> <familyName>Mustermann</familyName> <prefixName /> <middleName /> <suffixName /> <phoneticGivenName /> <phoneticMiddleName /> <phoneticFamily /> </structuredName> <organization> <company>Schlosserei Mustermann GmbH</company> <department /> <title /> </organization> <phones> <phone> <number>07652 881 8181</number> <type>home</type> <customLabel /> </phone> <phone> <number>0 800 435 2758</number> <type>work</type> <customLabel /> </phone> <phone> <number>+49 160 4321 222</number> <type>mobile</type> <customLabel /> </phone> </phones> <emails /> <addresses /> <ims /> <WebSites /> <Events /> <note /> <nickName /> <photo /> <netPhone /> <groups> <group> <groupName>Ungrouped</groupName> </group> </groups> </contact> </ContactRecords>  
      Basically I want to search my mobile's contacts for phone numbers to, to see, what contact they belong to. Propably it will make the task much more easy, when standardizing the mobile's contact XML to hold all numbers in "international-notation-without-any-whitespaces" (thats no problem):
      <?xml version="1.0" encoding="utf-8" ?> <ContactRecords> <contact> <structuredName> <displayName>Max Mustermann</displayName> <givenName>Max</givenName> <familyName>Mustermann</familyName> <prefixName /> <middleName /> <suffixName /> <phoneticGivenName /> <phoneticMiddleName /> <phoneticFamily /> </structuredName> <organization> <company>Schlosserei Mustermann GmbH</company> <department /> <title /> </organization> <phones> <phone> <number>+4976528818181</number> <type>home</type> <customLabel /> </phone> <phone> <number>+498004352758</number> <type>work</type> <customLabel /> </phone> <phone> <number>+491604321222</number> <type>mobile</type> <customLabel /> </phone> </phones> <emails /> <addresses /> <ims /> <WebSites /> <Events /> <note /> <nickName /> <photo /> <netPhone /> <groups> <group> <groupName>Ungrouped</groupName> </group> </groups> </contact> </ContactRecords>  
      What I would like to get from the XML file as a result, e.g. searching the phone number "+491604321222"
      Present in XML  or not (true) if present then return the values for... type (mobile) givenname (Max) familyname (Mustermann) company (Schlosserei Mustermann GmbH) optional: Other node names and their values within that contact, when not "empty" (like in this examle <emails /> or <middlename />)  
      The values from #2 - #6 would be perfect when returned in a 2D Array, Col0 = name, Col1 = value.
      Regards, Rudi.
    • By Luigi
      Someone can help-me to translate this Python's code to AutoIt?
      Python (source: https://repl.it/repls/InstructiveDarkslategreyJackrabbit)
      str = 'age=12,name=bob,hobbies="games,reading",phrase="I\'m cool!"' key = "" val = "" dict = {} parse_string = False parse_key = True # parse_val = False for c in str: print(c) if c == '"' and not parse_string: parse_string = True continue elif c == '"' and parse_string: parse_string = False continue if parse_string: val += c continue if c == ',': # terminate entry dict[key] = val #add to dict key = "" val = "" parse_key = True continue elif c == '=' and parse_key: parse_key = False elif parse_key: key += c else: val+=c dict[key] = val print(dict.items()) Python's output:
      [('phrase', "I'm cool!"), ('age', '12'), ('name', 'bob'), ('hobbies', 'games,reading')] AutoIt
      #include-once #include <Array.au3> #include <StringConstants.au3> Global $opt $opt = "estado = """" , cep = """", idade=32, nome = ""Luismar"", campo=""campo = campo""" $opt = "age=12,name=bob,hobbies=""games,reading"",phrase=""I\'m cool!""" ConsoleWrite($opt & @LF) Local $arr = StringSplit($opt, "", $STR_CHRSPLIT) Local $key = "" Local $val = "" Local $dict = ObjCreate("Scripting.Dictionary") Local $parse_string = False Local $parse_key = True Local $c For $ii = 1 To $arr[0] $c = $arr[$ii] If $c == '"' And Not $parse_string Then $parse_string = True ContinueLoop ElseIf $c == """" And $parse_string Then $parse_string = False ContinueLoop EndIf If $parse_string Then $val &= $c ContinueLoop EndIf If $c = "," Then $dict.Add($key, $val) $key = "" $val = "" $parse_key = True ContinueLoop ElseIf $c == "=" And $parse_key Then $parse_key = False ElseIf $parse_key Then $key &= $c Else $val &= $c EndIf Next $dict.Add($key, $val) ; missing this line... For $each In $dict ConsoleWrite($each & " = " & $dict.Item($each) & @LF) Next AutoIt's output
      age=12,name=bob,hobbies="games,reading",phrase="I\'m cool!" age = 12 name = bob hobbies = games,reading  
      Best regards.
    • By Luigi
      I need write a command line to run an .exe with parameters, one this parameters is a password with special characters  like:   [ ] \ "  etc.
      The password is write between double cotes too.
      When run this command line, catch error.
      I buid this function, sometimes run ok, others show error.

      Someone can any idea?

      Best ragards
      #include <Array.au3> Global $G_AIX_DEFAULT_PASSWORD = "******" Func Scape_Word_To_MSDOS($var = "") ;~ https://www.robvanderwoude.com/escapechars.php Local $aCharacters[][2] = [ _ ["%", "%%"], _ ["^", "^^"], _ ["&", "^&"], _ ["<", "^<"], _ [">", "^>"], _ ["|", "^|"], _ ["'", "^'"], _ ["`", "^`"], _ [",", "^,"], _ [";", "^;"], _ ["=", "^="], _ ["(", "^("], _ [")", "^)"], _ ["!", "^^!"], _ ["\", "\\"], _ ["[", "\["], _ ["]", "\]"], _ ['"', '\"""'], _ [".", "\."], _ ["*", "\*"], _ ["?", "\?"] _ ] Local $iSearch $var = StringSplit($var, "", $STR_NOCOUNT) Local $iSize = UBound($var, $UBOUND_ROWS) - 1 For $ii = 0 To $iSize $iSearch = _ArraySearch($aCharacters, $var[$ii]) If $iSearch >= 0 Then $var[$ii] = $aCharacters[$iSearch][1] Next Return _ArrayToString($var, "") EndFunc ;==>Scape_Word_To_MSDOS Local $password = "-Dk5iFB2UjOt[-x|""" & "" Local $password_scape = Scape_Word_To_MSDOS($password) ConsoleWrite("[" & $password & "]" & @LF) ConsoleWrite("[" & $password_scape & "]" & @LF)  
    • By Burgs
        I would like to be able to write a script to send commands to the console for creation of Gstreamer pipelines.  I was thinking of something similar to this:
      Local $iPID = Run("C:\Windows\System32\cmd", "", @SW_MAXIMIZE) ;THIS OPENS THE CONSOLE...!!! if $iPID == 0 Then ConsoleWrite(@CRLF & "I DID NOT OPEN CMD...error: " & @error & @CRLF) if $iPID <> 0 Then ConsoleWrite(@CRLF & "I OPENED CMD...!!!" & @CRLF) $hCmd = WinGetHandle("C:\WINDOWS\system32\cmd.exe") if $hCmd <> 0 Then WinActivate($hCmd) ;ensure command console is active... $sOutput = Send("cd C:\gstreamer\1.0\x86_64\bin" & @CRLF, $SEND_RAW) $sOutput = Send("gst-launch-1.0 videotestsrc ! autovideosink" & @CRLF, $SEND_RAW) Sleep(3000) ControlSend($hCmd, "", "", "exit" & @CR) EndIf ;$hCmd NOT "0"... I don't really know if this is the best way to open the console and send commands into it.  I'm also not sure about how to best catch any errors that may occur...likely this needs to be accomplished with the STDOUTREAD command however I've not had experience using it before and therefore would appreciate some advice that anybody may offer. 
      Basically I'm seeking guidance on how to best automate the opening of the console, sending lines of commands to be executed, and handling any potential errors in the execution of those commands...I thank you in advance.  Regards.
  • Create New...