Jump to content

Batch: dynamic variable assignation & evaluation


Recommended Posts

Attached is a demonstration of how to use SET to dynamically assign variables when their names are made from other variables' values.

It makes use of the CALL command and of the righ number of %s.

The demonstration is a simple menu application, displaying labels, asking for the user's choice and returning a custom data associated with the label.

For any use it could have.

Choice.cmd

Link to comment
Share on other sites


Djé, nice idea, how about pick_one.cmd. It is based on a similar idea, but its syntax will not be obvious to understand.

To use it, which is probably a good idea in the first instance, drop a reasonably high level folder onto it or run it from the command line with the folder path as a parameter.

[note] It would of course only work in this case if you had txt files in your chosen folder structure[/note]

Link to comment
Share on other sites

Attached is a demonstration of how to use SET to dynamically assign variables when their names are made from other variables' values.

It makes use of the CALL command and of the righ number of %s.

The demonstration is a simple menu application, displaying labels, asking for the user's choice and returning a custom data associated with the label.

For any use it could have.

That a nice little demo, keep up the good work.

Yzöwl nice work, pick all the text file out and place them on a menu.

Edited by gunsmokingman
Link to comment
Share on other sites

Thanks dudes.

Yzöwl, I'm too tired for tonight and really could not grab the entire mechanism yet. :wacko:

But I'm suspecting you acheived something great... :w00t:

I'll have to investigate tomorrow morning with a good coffee...

Link to comment
Share on other sites

In Choice.cmd, in order to get rid of the quotes in the displayed strings we can use:

:SETVALUES
SET %TYPE%%I%=%~1
GOTO :EOF

[only added a tilde (~) inside %1]

No solution yet for the question marks...

@Yzöwl: I feel better today and got it.

Yesterday, I though you had somehow managed to make a multi-lines variable :w00t:, because of:

ECHO/.%PICK% |FINDSTR/C:".%ONE%."

but now I understand it's use. Good thing for validating choices.

Link to comment
Share on other sites

@Yzöwl, I'm pretty sure you've got it (although my brain is too narrow right now to figure out how it works exactly) but the FOR loop is not a very good demonstration:

you could acheive the same displayed result with a single line %VAR%:

::make VAR a slightly different way
FOR /F "DELIMS=" %%? IN (SIXLINE.TXT) DO SET VAR=!VAR!"%%~?"<SPACE here>
::confirm VAR is monoline
ECHO %VAR%
::still display it multiline
FOR %%? IN (%VAR%) DO ECHO %%~?

-please note the <SPACE> at the end of 'DO SET VAR=!VAR!"%%~?" '

-to be integrated in the previous example

Also 'DO SET VAR=!VAR!"%%~?"^' followed by 2 empty lines does the trick without %LF%'s. And as in your example, it may be multiline as it can't be displayed properly by ECHO.

Do you know of any other way to dump those variables so we can confirm their 'multitude' :unsure: ?

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
  • Recently Browsing   0 members

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