Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 317557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:29:17+00:00 2026-05-12T08:29:17+00:00

Using the Windows XP CMD command-line I can expand a variable twice as follows:

  • 0

Using the Windows XP CMD command-line I can expand a variable twice as follows:

set AAA=BBB
set BBB=CCC
for /F "usebackq tokens=*" %i in (`echo %%AAA%%`) do echo %i

will echo CCC. I.e. AAA has been expanded to the string BBB, and then the variable BBB has been expanded to CCC.

This doesn’t work from inside a batch script (i.e. a .cmd file). Changing the %%AAA%% to either %%%AAA%%% or %%%%AAA%%%% doesn’t work either.

Any idea how i can achieve this from within a script, namely to take expand the variable AAA to the string CCC?

Late Edit

The answers posted work for my reduced example however the non-tortuous answer doesn’t work for the real case. Here’s an extended example (which doesn’t work), which illustrates what I was actually trying to do:

setlocal enabledelayedexpansion

set LIST=BBB CCC DDD
set BBB=111
set CCC=222
set DDD=333

for %%i in (%LIST%) do (

    for /F  %%a in ('echo %%%i%') do  echo !%%a!

)

I would like to see

111
222
333

output.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T08:29:18+00:00Added an answer on May 12, 2026 at 8:29 am

    Thinking in terms of a less tortuous solution, this, too, produces the CCC you desire.

    setlocal enabledelayedexpansion
    set AAA=BBB
    set BBB=CCC
    for /F  %%a in ('echo %AAA%') do  echo !%%a!
    

    edit:

    to dissect this answer:

    setlocal enabledelayedexpansion – this will allow for any environment variable setting during your bat to be used as modified during the process of your for loop.

    set AAA=BBB, set BBB=CCC – your data population set statements

    for /F %%a in ('echo %AAA%') do echo !%%a! – This tells the processor to loop, albeit only once, and take out the first token that is returned (default delimiter of space and tab apply) from the running of the command in the parens and put it in the var %%a (outside of a batch, a single % will do). If you specify that var as %%a, you need to use %%a in your do block. Likewise, if you specify %%i, use %%i in your do block. Note that to get your environment variable to be resolved within the do block of the for loop, you need surround it in !‘s. (you don’t need to in the in block, as I originally posted – I have made that change in my edit).

    edit:

    You were very close with your updated example. Try it like this:

    @echo off
    setlocal enabledelayedexpansion
    set LIST=BBB CCC DDD
    set BBB=111
    set CCC=222
    set DDD=333
    
    for %%i in (%LIST%) do (
        for /F %%a in ('echo %%i') do echo !%%a!
    )
    

    The difference between your update and this is that you were trying to echo the environment variable in the in set with in ('echo %%%i%'), but without the !‘s for the delayed expansion of set variables. Were you to use in ('echo !%%i!'), you would see your BBB, CCC, and DDD variables resolved, but then the do block of your inner loop wouldnt have anything to resolve – you dont have any 111 environment variables. With that in mind, you could simplify your loop with the following:

    @echo off
    setlocal enabledelayedexpansion
    set LIST=BBB CCC DDD
    set BBB=111
    set CCC=222
    set DDD=333
    
    for %%i in (%LIST%) do (echo !%%i!)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 181k
  • Answers 181k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You may use google api to load jquery: <script src="http://www.google.com/jsapi"></script>… May 12, 2026 at 4:18 pm
  • Editorial Team
    Editorial Team added an answer As far as I know the only way you can… May 12, 2026 at 4:18 pm
  • Editorial Team
    Editorial Team added an answer A way of doing this with a feature would be… May 12, 2026 at 4:18 pm

Related Questions

I just set up a new MySQL 5.1 Database Server on a Windows XP
I need to run a task every 10 mins from morning 9 AM to
Im searching a tool which allows me to specify some folders as bookmarks and
We are trying to convert a bash shell script into a Windows batch script.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.