Jump to content

HowTo Variable Expansion ?


 Share

Recommended Posts

In CMD;

@Echo off &SetLocal EnableExtensions EnableDelayedExpansion

set "VAR=echo.Hello there ^! 1 &1 = 2"
::
%VAR%

OUTPUT;

Hello there ! 1 &1 = 2

In AU3;

AutoItSetOption ( 'ExpandVarStrings' , 1 )
;
$VAR = MsgBox ( 0, '', 'Hello there ! 1 &1 = 2' )
;
$VAR$

OUTPUT;

ERROR: syntax error (illegal character)

$VAR$

In AU3;

AutoItSetOption ( 'ExpandVarStrings' , 1 )
;
$VAR = MsgBox ( 0, '', 'ok' )
;
$VAR

OUTPUT;

ERROR: syntax error

$VAR

How do I set a variable and then execute it as if it were a command ?

Don't mean the obvious solution;

MsgBox ( 0, '', $VAR )

Otherwise the code would grow very looong, when I start doing much more complex things

Also a related problem

in CMD;

set "Debug.Define=if defined Debug.Sub"

:ThisFunction
::(
   set "Debug.Sub=ThisFunction"

   %Debug.Define% echo.Debugging !Debug.Sub!
...

I am stumbled how can I do that simply in AU3 &then I mean the %Debug.Define% part

Am I really going to have to write a complete function for something that simply in CMD ???

• Any number images • Images of any size • Any number of URLs • Any number of lines

Link to comment
Share on other sites

  • Developers

This is in a nutshell how it works:

$var = " I am text"
MsgBox(0,"Debug: doesn't show its content","var contains: $var$ ")
AutoItSetOption ( 'ExpandVarStrings' , 1 )
MsgBox(0,"Debug: Show content","var contains: $var$ ")
; -or -
MsgBox(0,"Debug","var contains:" & $var)
Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

So u say IT IS NOT POSSIBLE to store a command inside a variable &then using the variable like it were a command ?

like:

$VAR = msgbox ( 0, '', 'hello there' )

;

$VAR$

• Any number images • Images of any size • Any number of URLs • Any number of lines

Link to comment
Share on other sites

AHA so there it had been hiding :D

Searched the help over and over &didn't find it

Maybe they should add this to FAQ in HELP for COMMAND.COM, CMD.EXE users

That would really help

Can't thank u enough for such an easy solution ;)

• Any number images • Images of any size • Any number of URLs • Any number of lines

Link to comment
Share on other sites

  • Developers

Why do you need something like this?

I am yet to write my first script using Execute() (only Obfuscator is using it for making statements unreadble)

Can you give me a simple example where you want to use this for?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

But now I have another problem

$VAR = "msgbox ( 0, '', 'hello there' )"
Execute($VAR)

Works

$VAR = "if 1 = 1 Then msgbox ( 0, '', 'hello there' )"
Execute($VAR)

Does not work ?? :D

• Any number images • Images of any size • Any number of URLs • Any number of lines

Link to comment
Share on other sites

  • Developers

Try answering my question first so I understand what you want to do, because I have the feeling you are heading down the wrong path here.

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I finished a sjablon in CMD language that I can load everytime when I want to write an APP

This sjablon holds all the functions I would ever need

So I copy the sjablon rename it to my new APP &start from there

for example when i need networking support I do:

sjablon code init

   ...

sjablon /code init

app code init

   ...set /a Sjablon.NET = 2 &:: 0 = Essential, 1 = Local, 2 = Network...

app /code init

sjablon code main

   ...

sjablon /code main

app code main

   .. my actiual program ...

app /code main

sjablon code exit

   ...

sjablon /code exit

The sjablon will detect i want to connect to my server &it will do so before the APP starts

This is an example of a typical sjablon function in CMD to copy a file

:XCopy.STABLE ( /FullPathFile.SOURCE: "STRING/BYVAL" /FullPathFile.TARGET: "STRING/BYVAL" /Show_BIG /Show_OK /Show_ERR /Halt_ERR )
::
:: SJAB v1.37 beta
::
:: @FullPathFile.SOURCE     STRING/BYVAL    Required;   @FullPath.SOURCE or @FullPathFile.SOURCE
:: @FullPathFile.TARGET     STRING/BYVAL    Required;   @FullPath.TARGET or @FullPathFile.TARGET
::                              Will rename @FullPathFile.TARGET if one file copied &@FullPathFile.TARGET not exist as a dir
::                              Will make dir @FullPathFile.TARGET if multiple files copied
:: @Show_BIG            STRING      Optional;   Switch BigScreen &back
:: @Show_OK             STRING      Optional;   Show output channel 1
:: @Show_ERR            STRING      Optional;   Show output channel 2
:: @Halt_ERR            STRING      Optional;   Halt on output channel 2
::
:: @ERR             BYVAL       RETURN;     1 for error 0 otherwise
::
:: Warning;     If @FullPathFile.SOURCE is a single file And @FullPathFile.TARGET not exist as a dir (
::
::          @FullPathFile.TARGET will be treated as a file
::          add an "\" at the end of @FullPathFile.TARGET &@FullPathFile.TARGET will be treated as a directory
::      )
::
::      does not check for; drive exist error, Cyclic copy error
::
SetLocal
::(
    ::set "Debug.Sub=!Sub.%Sub.Depth%.Name!"
    ::(
        ::%Debug% "" "inside !Sub.%Sub.Depth%.Name! () Sub.%Sub.Depth%.Par.AsIS:" &echo.!Sub.%Sub.Depth%.Par.AsIS!_
        set "Sub.!Sub.Depth!.Use=:XCopy.STABLE ( /FullPathFile.SOURCE: "STRING/BYVAL" /FullPathFile.TARGET: "STRING/BYVAL" /Show_BIG /Show_OK /Show_ERR /Halt_ERR )"
    ::)

    ::%Debug% "PROC PAR"
    ::(
        set "Par.FIX=FullPathFile.SOURCE FullPathFile.TARGET"
        set "Par.OPT=Show_BIG Show_OK Show_ERR Halt_ERR"
        %PROC.PAR.FAST% ()

        for %%! in ( FullPathFile.SOURCE FullPathFile.TARGET ) do   %DeQuote.IfEven% %%! &%Get.ByVal% %%!

        for %%! in ( Show_ERR ) do                  if defined Halt_ERR set "%%!=/%%!"

        for %%! in ( PrefetchCOMMAND
        ) do (
            set "%%!="

            if not defined Show_OK  set "%%!=>nul"
            if not defined Show_ERR set "%%!=!%%!! 2>nul"
        )

        ::%Debug.Equals% for %%! in ( FullPathFile.SOURCE FullPathFile.TARGET Show_BIG Show_OK Show_ERR Halt_ERR ) do echo. %%!=!%%!!_
    ::)

    ::%Debug% /Debug.Pause "VALID"
    ::(
        for %%! in ( FullPathFile.SOURCE ) do if exist "!%%!!" (
            ::
            set /a ERR = 0

            if exist "!%%!!\" (
                ::
                set /a Source.ISFile = 0

                %Slash.Rem.Last% %()%

            ) else  set /a Source.ISFile = 1

        ) else (
            ::
            set /a ERR = 1

            if defined Show_ERR                 echo. &echo. not exist FullPathFile.SOURCE=!FullPathFile.SOURCE!_ ^! &%Debug.Pause%
            if defined Halt_ERR                 %ERR.Set.VALID% /not_exist: %%!
        )

        for %%! in ( FullPathFile.TARGET
        ) do (
            %ERR.CHK.DEFINED% %()%

            set "FetchCOMMAND=" &if !Source.ISFile! equ 0 (
                ::
                set "FetchCOMMAND=/e"

                %Slash.Rem.Last.DOUBLE% %()%
            )
        )

        %FullPath.Get.STABLE% /FullPathFile: FullPathFile.Target /StoreVAR: FullPath.Target /Halt_ERR

        %Wait.Get.WorkC.USE% ()

        ::%Debug.Equals% for %%! in ( FullPathFile.SOURCE FullPathFile.TARGET Source.ISFile FetchCOMMAND Show_BIG Show_OK Show_ERR Halt_ERR ERR ) do echo. %%!=!%%!!_
    ::)

    ::%Debug% "Perform" &%Debug.Define% pause
    ::(
        if defined Show_BIG set /a ModeColState.LAST = !ModeColState! &%MODE.HighCols% %()%
        ::(
            ::%Debug% /Debug.Pause "if !ERR! equ 0"
            if !ERR! equ 0 (
                ::
                if defined Show_OK if defined Show_BIG (
                    ::
                    if !ModeColState.LAST! equ !ModeColState! echo.

                ) else  echo.

                if not defined TMP                  %ERR.Set.VALID% /not_defined: TMP
                set /a TMP.CleanUp = 0 &if not exist "!TMP!\" set /a TMP.CleanUp = 1 &%MD.FAST% "TMP" /Halt_ERR

                set "MSG=XCopy.EXE !FetchCOMMAND! /v /c /i /f /g /h /r /k /y "!FullPathFile.SOURCE!" "!FullPathFile.TARGET!""
                %LOG.SIMPLE% %()%

                ::%Debug% /Debug.Pause " !MSG!" " cd=!cd!_"
                echo.>nul

                :: Double piping here can trigger error 'niet bestaande sluis' at random !, can be avoided by 'redirecting << file b' or 'use & instead of || to get ErrorLevel'
                echo.>nul

                for %%! in ( "!TMP!\WorkC.!WorkC.USE!.CMD"
                ) do (
                    >%%! (echo.b) &%PrefetchCOMMAND% XCopy.EXE !FetchCOMMAND! /v /c /i /f /g /h /r /k /y "!FullPathFile.SOURCE!" "!FullPathFile.TARGET!" < %%! ||set /a ERR = !ErrorLevel!
                    >nul 2>&1 del /f /q %%!
                )

                set /a ERR.MEM = !ERR!
                if !TMP.CleanUp! neq 0 %RDF.AtStage.HKLM% /FullPathFile: "TMP" /Show_OK /Halt_ERR
                set /a ERR = !ERR.MEM!
            )

            ::%Debug% /Debug.Pause "if !ERR! gtr 0"
            if !ERR! gtr 0 if defined Halt_ERR          %ERR.Set.THIS% "XCopy: ""!FullPathFile.SOURCE!"" " "To   : ""!FullPathFile.TARGET!"" "

            %RegWrite.FAST% "!EDReg!" "WorkC.Free.!WorkC.USE!" REG_SZ "!WorkC.USE!" /Halt_ERR
        ::)
    ::)
::
::%Debug% /Debug.Pause " ERR=!ERR!" "outside !Sub.%Sub.Depth%.Name! ()"
(
    EndLocal

    set /a ModeColState = %ModeColState%

    set /a ERR = %ERR%
)
::
goto :eof ()
::)

You then call from your app for example like this

%XCopy.STABLE% /FullPathFile.SOURCE: FullPathFile.SOURCE /FullPathFile.TARGET: FullPathFile.SOURCE /Show_BIG /Show_OK /Halt_ERR

Here comes the cool part

set "Debug.Sub=:XCopy.STABLE"

%XCopy.STABLE% /FullPathFile.SOURCE: FullPathFile.SOURCE /FullPathFile.TARGET: FullPathFile.SOURCE /Show_BIG /Show_OK /Halt_ERR

echo.endoftest
pause
exit

This will make the sjablon print debug information ONLY for the calling function since %Debug.Equals% is

for %%! in ( Debug.Equals ) do          set "%%!=if defined Debug.Sub call set "Ó=%%Sub.^^^!Sub.Depth^^^!.Name%%" ^&if /i ["^^^!Debug.Sub^^^!"] == ["^^^!Ó^^^!"]"

That means that ONLY if Debug.Sub=:XCopy.STABLE AND !Sub.%Sub.Depth%.Name!=:XCopy.STABLE :DEBUG () will run :

Of course u will have to remove the %debug% comments in :XCopy.STABLE () otherwise it won't work.

the comments are there to speed up CMD exec a little while I am not debugging a none compiled sjablon

So the reason I want it is because i am doing a language conversion battle

I WAN'T EVERY CMD FUNCTION TO EXIST AS AU3 YEP THAT's RIGHT :D

&I want to minimize the amount of rewriting I have to do !

Why ?

I have 514961 characters inside my sjablon, it is incredibly huge since it tries to catch any possible error at any time

But it does about everything i want it to in CMD, it is written to handle multiple instances

It can even spawn a new instance of itself to execute a sub Function so doing multiprocessing :huggles:

Now I wan't to update to AU3 &VBS since CMD is SLOW, WEAK, HARD to DEBUG &no fun to program in

I have been looking for an editor that can syntax check a CMD script for ages without succes :D

• Any number images • Images of any size • Any number of URLs • Any number of lines

Link to comment
Share on other sites

So in short I am converting from the CMD language

I wan't to stick close to the CMD code I already have since I am

reading a CMD function

writing an AU3 equivalent etc..

reading a CMD line,

writing an AU3 equivalent

This is why %expandable_var% is such a problem for me

if I can't find a good solution I'll have to go a long way to get to the same poin't.

I know I can avoid %expandable_var% but hey, it's already there... :D

In CMD you typically set a variable to a command or series of commands that can itself also contain variables that need expansion

example:

@echo off &SetLocal EnableExtensions EnableDelayedExpansion

for %%? in ( Slash.Rem.Last ) do set "%%?=set "%%^^!=^^^!%%^^!^^^!\###" &set "%%^^!=^^^!%%^^!:\\###=^^^!" &set "%%^^!=^^^!%%^^!:\###=^^^!" "

set "VAR=C:\Path\"

for %%! in ( VAR ) do %Slash.Rem.Last% %()%

this will give u VAR=C:\Path

Edited by EdDyreen

• Any number images • Images of any size • Any number of URLs • Any number of lines

Link to comment
Share on other sites

  • Developers

Just back... I am not constantly online :huggles:

I need to let this sink in before trying to answer as its a little more complex than expected.

I am used to the good old dos prompt and batch files but not to a level that you are at with these Sjablon/templates.

Jos :D

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

Aah The Netherlands...

Daar ZEN'k nog nor school gwest :D

Ikke domme BELG aangenaam :huggles:

I knew you are from Belgium already.

Saw some Dutch in your dos stuff so checked from which surrounding you were coming.:

Looking at the stuff you posted it seems to me you need to do a total rewrite of all these template functions in AutoIt3 without trying to stick to the DOS type syntax, because what else is the purpose of doing this in AutoIt3?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

because what else is the purpose of doing this in AutoIt3?

I have yet to figure out what the purpose is of doing this in AutoIt3

I have been doubting for a long time what language to convert my sjablon to since my main problem with the

windows terminal always has been execution speed, syntax checking &easy debugging

I considered VBS 6 since it does a syntax check when the file is loaded &has great execution speeds

I also considered C++ since when you master it you won't need anythin else

In C you really can do everything :huggles:

However i chose AU3 since it's a great tutor for API COM etc.., very popular, FREEEEE &easy to learn.

I also chose it since CMD &VBS is soon to be replaced by PowerShell

When I found out about PowerShell my first thought was 'O great, yet ANOTHER script language !'

Like we don't have enough already.

I try to avoid having to learn PowerShell :D

Eventiually i like to have sjablon's for:

BAT, CMD, VBS older Windows OSes

AU3 Windows OSes

C++ Any OS

C++ is my goal but AU3 seems like a good inbetween to master

When I find time I will post my sjablon's for these languages on me future website

For others to learn &learn from others :D

Well see ya &happy programming :

• Any number images • Images of any size • Any number of URLs • Any number of lines

Link to comment
Share on other sites

Hi.

Either use the statement where you need it:

; $VAR = "msgbox ( 0, '', 'hello there' )"
; Execute($VAR)
msgbox ( 0, '', 'hello there' )

... or, if there are several places in your code, where you need this statement, wrap it into a fuction:

MyFunc()

Func MyFunc()
if 1 = 1 Then msgbox ( 0, '', 'hello there' )
endfunc

Regards, Rudi.

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

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