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 620375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:45:25+00:00 2026-05-13T18:45:25+00:00

File test.cmd: name=dummy for /f eol=; tokens=1 delims=, %%i in (list.txt) do ( echo

  • 0

File test.cmd:

name=dummy  

for /f "eol=; tokens=1 delims=," %%i in (list.txt) do (  
echo i: %%i  
set name=%%i  
echo name: %name%)  

the file list.txt contains this lines (one name per line):

John  
Tom  
Erica  
Sara  

Each time I launch this batch I get this output:

i: John  
name: dummy  
i:T om  
name: dummy  
i: Erica  
name: dummy  
i: Sara  
name: dummy  

It seems that the variable name does not get assigned the value of %%i

Any idea?

  • 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-13T18:45:26+00:00Added an answer on May 13, 2026 at 6:45 pm

    You’re falling into the old trap of not using delayed expansion.

    For a quick fix, just put

    setlocal enabledelayedexpansion
    

    before that loop in your batch file and use !name! instead of %name%.

    CMD expands variables while parsing a command. A command in this sense is a single line or a “block”, delimited with parentheses. The complete for loop is only parsed once and in that stage %name% gets replaced with the value it has at that point, namely "dummy". Delayed expansion on the other hand uses ! instead of % to delimit variable names and the variables then get expanded right before execution of a command.

    Whenever you are setting a variable inside a parenthesized block and use its value in the same block again you need to use delayed expansion. help set has also some info about this:

    Delayed environment variable expansion
    is useful for getting around the
    limitations of the current expansion
    which happens when a line of text is
    read, not when it is executed. The
    following example demonstrates the
    problem with immediate variable
    expansion:

    set VAR=before
    if "%VAR%" == "before" (
        set VAR=after
        if "%VAR%" == "after" @echo If you see this, it worked
    )
    

    would never display the message, since
    the %VAR% in both IF statements is
    substituted when the first IF
    statement is read, since it logically
    includes the body of the IF, which is
    a compound statement. So the IF
    inside the compound statement is
    really comparing “before” with “after”
    which will never be equal. Similarly,
    the following example will not work as
    expected:

    set LIST=
    for %i in (*) do set LIST=%LIST% %i
    echo %LIST%
    

    in that it will not build up a list of
    files in the current directory, but
    instead will just set the LIST
    variable to the last file found.
    Again, this is because the %LIST% is
    expanded just once when the FOR
    statement is read, and at that time
    the LIST variable is empty. So the
    actual FOR loop we are executing is:

    for %i in (*) do set LIST= %i
    

    which just keeps setting LIST to the
    last file found.

    Delayed environment variable expansion
    allows you to use a different
    character (the exclamation mark) to
    expand environment variables at
    execution time. If delayed variable
    expansion is enabled, the above
    examples could be written as follows
    to work as intended:

    set VAR=before
    if "%VAR%" == "before" (
        set VAR=after
        if "!VAR!" == "after" @echo If you see this, it worked
    )
    
    set LIST=
    for %i in (*) do set LIST=!LIST! %i
    echo %LIST%
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small file test.txt contains a Long number inside while another piece
I've got a file (test.txt) which contains 1234567. However when I try to read
For example, suppose I have a batch file called 'test.cmd' and it simply contains:
I have a file test.txt which has an array: array = [3,5,6,7,9,6,4,3,2,1,3,4,5,6,7,8,5,3,3,44,5,6,6,7] Now what
In my holder test file: test name must be present do holder = Holder.new(name:
I have a haskell file test.hs. In this file I have written a function
I have a js file test.js that contains a global variable global_var and a
I have a button on a file test.php <?php $url=http://.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?> <input type=button name=btn
-java -classpath<> <classname> in the .bat file to launch java test from cmd windows
I launch a .bat file to launch one java test the file contains :

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.