Jump to content

User input to have single and/or double quotes


Recommended Posts

Hi, 

I am trying to get user input saved that could contain single and/or double quotes, then save that into another .au3 file to be used later as variables. 

Code I am having a problem with:

Func WriteWindowsNames($arr)
    Local $VarArr[9]
    $VarArr[3] = "Global $UnityName = '" & $arr[3] & "'"
    ;Save results to a file
    WriteFileFromArray(@ScriptDir & '\WindowsNames.au3', $VarArr)
EndFunc

 

WriteFileFromArray is just my wrapper function of _FileWriteFromArray($path, $array)  with some extra checks.

Works fine if I have $arr[3] = 'Thats Bananas'
and that prints out to file as:

Global $UnityName = 'Thats Bananas'

But if $arr[3] = 'That's Bananas' with the extra single quote, 
and that prints out to file as:

Global $UnityName = 'That's Bananas'


When that is read by AutoIt, it sometimes breaks or doesn't work. 

How can I adjust my $VarArr[3] line to accept or check for single/double quotes from the user?  Can it be done without an if statement, because then I will need 10 times for the others similar to it?

Edited by Mavnus04
Added more code
Link to comment
Share on other sites

Func WriteWindowsNames($arr)
    Local $VarArr[9]
    $VarArr[3] = "Global $UnityName = '" & $arr[3] & "'"
    ;Save results to a file
    WriteFileFromArray(@ScriptDir & '\WindowsNames.au3', $VarArr)
EndFunc

 

WriteFileFromArray is just my wrapper function of _FileWriteFromArray($path, $array)  with some extra checks. 

I removed the other variables from the array to keep it short. 

Edited by Mavnus04
Link to comment
Share on other sites

I home this helps. I'm sure that your code concept is impractical. :( 

Global $UnityName = 'That''s Bananas'
ConsoleWrite($UnityName & @CRLF)
WriteWindowsNames($UnityName)
Func WriteWindowsNames($arr)
    Local $VarArr[9]
    $VarArr[3] = 'Global $UnityName = "' & $arr & '"'
    ;Save results to a file
;~     WriteFileFromArray(@ScriptDir & '\WindowsNames.au3', $VarArr)
    MsgBox(0,"", $VarArr[3], 30)
EndFunc

 

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

1 minute ago, argumentum said:

I home this helps. I'm sure that your code concept is impractical. :( 

Global $UnityName = 'That''s Bananas'
ConsoleWrite($UnityName & @CRLF)
WriteWindowsNames($UnityName)
Func WriteWindowsNames($arr)
    Local $VarArr[9]
    $VarArr[3] = 'Global $UnityName = "' & $arr & '"'
    ;Save results to a file
;~     WriteFileFromArray(@ScriptDir & '\WindowsNames.au3', $VarArr)
    MsgBox(0,"", $VarArr[3], 30)
EndFunc

 

This only helps if the user input uses single quotes.  I need to cover if the user inputs single and/or double quotes.
That's Bananas"
"That's Bananas"
"Thats Bananas"

Link to comment
Share on other sites

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Example()
Func Example()
    #Region ### START Koda GUI section ### Form=
    Local $Form1 = GUICreate("Form1", 335, 84)
    Local $Input1 = GUICtrlCreateInput("Input1 ' """, 8, 8, 121, 21)
    Local $Button1 = GUICtrlCreateButton("Button1", 136, 8, 75, 25)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                GUIDelete()
                Exit
            Case $Button1
                MsgBox(0,"",GUICtrlRead($Input1) & @CR & StringToBinary(GUICtrlRead($Input1)), 60, $Form1)
        EndSwitch
    WEnd
EndFunc

 

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <File.au3>

Example()
Func Example()
    #Region ### START Koda GUI section ### Form=
    Local $Form1 = GUICreate("Form1", 335, 84)
    Local $Input1 = GUICtrlCreateInput("Input1 ' """, 8, 8, 121, 21)
    Local $Button1 = GUICtrlCreateButton("Button1", 136, 8, 75, 25)
    local $array[2]
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                GUIDelete()
                Exit
            Case $Button1
                MsgBox(0,"",GUICtrlRead($Input1) & @CR & StringToBinary(GUICtrlRead($Input1)), 60, $Form1)
                $array[0] = "Global $UnityName = '" & GUICtrlRead($Input1) & "'"
                $array[1] = StringToBinary(GUICtrlRead($Input1))
                _FileWriteFromArray("C:\Users\Matthew.Mumm\Desktop\Output.txt", $array)
        EndSwitch
    WEnd
EndFunc

gives the same issue.  File output:

Global $CefName = 'Input1 ' "'
0x496E7075743120272022

 

Link to comment
Share on other sites

..what I meant by:

31 minutes ago, argumentum said:

I'm sure that your code concept is impractical

,is that you are creating a script with a script. I'm sure that whatever you want to accomplish ( and have not shared ), can be done in a more "elegant" way :) 

Edited by argumentum

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Example()
Func Example()
    #Region ### START Koda GUI section ### Form=
    Local $Form1 = GUICreate("Form1", 335, 84)
    Local $Input1 = GUICtrlCreateInput("Input1 ' """, 8, 8, 121, 21)
    Local $Button1 = GUICtrlCreateButton("Button1", 136, 8, 75, 25)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                GUIDelete()
                Exit
            Case $Button1
                ConsoleWrite('Global $var = "' & StringReplace(GUICtrlRead($Input1), '"', '""') & '"' & @CRLF)
                MsgBox(0, "", GUICtrlRead($Input1) & @CR & _
                        'Global $var = "' & StringReplace(GUICtrlRead($Input1), '"', '""') & '"', 60, $Form1)
        EndSwitch
    WEnd
EndFunc   ;==>Example

 

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...