Jump to content

ExpandVarString Literal Strings


Recommended Posts

Using Opt(ExpandVarStrings, 1)

I wish to replace a variable value within a string with an expanded variable for example:

Change C:\Documents and Settings\<Username>\Desktop to '@DesktopDir@'

(A snippet of my code is below)

In the StringReplace "ReplaceString" field I've tried to use literal "@" signs but @@DesktopDir@@ and various other combinantions always returns the complete expanded variable value rather than @DesktopDir@. I've also tried using @ in compination with Chr(64) but with the same results for instance '@@DesktopDir' & Chr(64).

The only way I've been able to resolve this is by using

$OPT_AUTOVARS = Opt('ExpandVarStrings', 1)

And then toggle it within the _FNC_CHCKFUNC Function, not sure if this is the best way or if maybe I'm missing something.

Cheers

#include <GUIConstantsEx.au3>
#Include <WindowsConstants.au3>

Opt('ExpandVarStrings', 1)
Opt('GUICoordMode', 0)
Opt('MustDeclareVars', 1)

Global $GUI_FILENAME
Global $GUI_FILESELE
Global $MSG_FILESELE
Global $TMP_CHKMACRO

Global Const $WS_EX_COMPOSITED = 0x02000000

Example()

Func Example()
    Local $msg
    GUICreate("Example", 320, 40, -1, -1, BitOR($WS_CAPTION, $WS_SIZEBOX, $WS_SYSMENU), $WS_EX_COMPOSITED)

    GUISetCoord(10, 10)
    $GUI_FILENAME = GUICtrlCreateInput('', -1, -1, 200, 20)
    $GUI_FILESELE = GUICtrlCreateButton('Browse...', 210, -1, 90, 20)
    GUISetState()

    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                Exit
            Case $msg = $GUI_FILESELE
                $MSG_FILESELE = FileOpenDialog('Please Select a File', @ScriptDir, 'exe (*.exe)')
                If @error <> 1 Then GUICtrlSetData($GUI_FILENAME, _FNC_CHKMACRO($MSG_FILESELE))
        EndSelect
    WEnd
EndFunc


Func _FNC_CHKMACRO($TMP_CHKMACRO)

    If StringInStr($TMP_CHKMACRO, @DesktopDir) Then
        $TMP_CHKMACRO = StringReplace($TMP_CHKMACRO, @DesktopDir, '@@DesktopDir@')
    EndIf
    Return $TMP_CHKMACRO
EndFunc
Link to comment
Share on other sites

It's still @DesktopDir@ but you need to temporary change the Opt flag to see the changes:

#include <GUIConstantsEx.au3>
#Include <WindowsConstants.au3>

Opt('ExpandVarStrings', 1)
Opt('GUICoordMode', 0)
Opt('MustDeclareVars', 1)

Global $GUI_FILENAME
Global $GUI_FILESELE
Global $MSG_FILESELE
Global $TMP_CHKMACRO

Global Const $WS_EX_COMPOSITED = 0x02000000

Example()

Func Example()
    Local $msg
    GUICreate("Example", 320, 40, -1, -1, BitOR($WS_CAPTION, $WS_SIZEBOX, $WS_SYSMENU), $WS_EX_COMPOSITED)

    GUISetCoord(10, 10)
    $GUI_FILENAME = GUICtrlCreateInput('', -1, -1, 200, 20)
    $GUI_FILESELE = GUICtrlCreateButton('Browse...', 210, -1, 90, 20)
    GUISetState()

    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                Exit
            Case $msg = $GUI_FILESELE
                $MSG_FILESELE = FileOpenDialog('Please Select a File', @ScriptDir, 'exe (*.exe)')
                If @error <> 1 Then GUICtrlSetData($GUI_FILENAME, _FNC_CHKMACRO($MSG_FILESELE))
        EndSelect
    WEnd
EndFunc


Func _FNC_CHKMACRO($TMP_CHKMACRO)

    If StringInStr($TMP_CHKMACRO, @DesktopDir) Then
        $TMP_CHKMACRO = StringReplace($TMP_CHKMACRO, @DesktopDir, '@@DesktopDir@')
    EndIf
    
    Opt('ExpandVarStrings', 0)
    ConsoleWrite($TMP_CHKMACRO & @LF)
    Opt('ExpandVarStrings', 1)
    Return $TMP_CHKMACRO
EndFunc
Link to comment
Share on other sites

It's still @DesktopDir@ but you need to temporary change the Opt flag to see the changes:

#include <GUIConstantsEx.au3>
#Include <WindowsConstants.au3>

Opt('ExpandVarStrings', 1)
Opt('GUICoordMode', 0)
Opt('MustDeclareVars', 1)

Global $GUI_FILENAME
Global $GUI_FILESELE
Global $MSG_FILESELE
Global $TMP_CHKMACRO

Global Const $WS_EX_COMPOSITED = 0x02000000

Example()

Func Example()
    Local $msg
    GUICreate("Example", 320, 40, -1, -1, BitOR($WS_CAPTION, $WS_SIZEBOX, $WS_SYSMENU), $WS_EX_COMPOSITED)

    GUISetCoord(10, 10)
    $GUI_FILENAME = GUICtrlCreateInput('', -1, -1, 200, 20)
    $GUI_FILESELE = GUICtrlCreateButton('Browse...', 210, -1, 90, 20)
    GUISetState()

    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                Exit
            Case $msg = $GUI_FILESELE
                $MSG_FILESELE = FileOpenDialog('Please Select a File', @ScriptDir, 'exe (*.exe)')
                If @error <> 1 Then GUICtrlSetData($GUI_FILENAME, _FNC_CHKMACRO($MSG_FILESELE))
        EndSelect
    WEnd
EndFunc


Func _FNC_CHKMACRO($TMP_CHKMACRO)

    If StringInStr($TMP_CHKMACRO, @DesktopDir) Then
        $TMP_CHKMACRO = StringReplace($TMP_CHKMACRO, @DesktopDir, '@@DesktopDir@')
    EndIf
    
    Opt('ExpandVarStrings', 0)
    ConsoleWrite($TMP_CHKMACRO & @LF)
    Opt('ExpandVarStrings', 1)
    Return $TMP_CHKMACRO
EndFunc
That doesn't work. Getting rid of the annoying GUI distraction makes it easier to see:
Opt('ExpandVarStrings', 1)

Global $sBefore = @DesktopDir & '\MySubDir\MyFile.exe'
ConsoleWrite("$sBefore = " & $sBefore & @LF)
Global $s_After = _FNC_CHKMACRO($sBefore)
ConsoleWrite("$s_After = " & $s_After & @LF)


Func _FNC_CHKMACRO($TMP_CHKMACRO)
    Local $iSave = Opt('ExpandVarStrings', 0)
    If StringInStr($TMP_CHKMACRO, @DesktopDir) Then
        $TMP_CHKMACRO = StringReplace($TMP_CHKMACRO, @DesktopDir, '@@DesktopDir@')
    EndIf
    ConsoleWrite("$TMP_CHKMACRO = " & $TMP_CHKMACRO & @LF)
    Opt('ExpandVarStrings', $iSave)
    Return $TMP_CHKMACRO
EndFunc

:)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Thanks everyone for your repsonses

That's the only work around I found as well, except I used $OPT_AUTOVARS rather than $iSave, I just wasn't sure if I was overlooking something obvious.

Thanks again for the replies.

Cheers

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...