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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:43:57+00:00 2026-06-06T12:43:57+00:00

searched this site and others – no joy. :: first for loop for /L

  • 0

searched this site and others – no joy.

:: first for loop
for /L %%x in (1,1,2) do (
generic-executable.output > grab1-%%x.txt

:: second, nested for loop
for /f "delims=" %%i in (grab1-%%x.txt) do (set grab1=%%i)
echo variable string is %grab1%%x%

generic-executable.output > grab2-%%x.txt
for /f "delims=" %%i in (grab2-%%x.txt) do (set grab2=%%i)
echo variable string is %grab2%%x%
)

Trying to run a nested for loop that will

1) write data to the file

2) take data from the file and save it to another variable.

The names of the end variables should be a concatenation of each of the for loops (i.e. grab1-1, 2-1, 1-2, 2-2).

Saving the data to the variables is no problem, formatting the variables to recall the data IS.

I’m most likely missing something in the formatting of the concatenated variable. I’ve tried single ', double ", ^, !, one %, two %, backslash, ACK!! … the closest I’ve gotten is

echo %grab1-%%x

gave:

%grab1-1

I’d appreciate any tips you can provide.

Thanks,
Dave

  • 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-06-06T12:43:59+00:00Added an answer on June 6, 2026 at 12:43 pm

    You have run into a classic stumbling block for batch newbies: You cannot set a variable within a loop (within parentheses) and then access the value using %var% within the same loop. The Entire loop (parenthesized block of code) is parsed in one pass, and %var% is expanded at parse time. So you see the value of var as it was prior to the loop executing.

    The solution is to enable delayed expansion using setlocal enableDelayedExpansion near the top of your script, and then expand the variable using delayed expansion as !var!. Delayed expansion means the value is expanded at run time – exactly what you want.

    I believe this is what you were trying to achieve

    setlocal enableDelayedExpansion
    for /L %%x in (1,1,2) do (
    
      genericOutput1.exe > grab1-%%x.txt
      for /f "delims=" %%i in (grab1-%%x.txt) do set "grab1-%%x=%%i"
      echo grab1-%%x variable string is !grab1-%%x!
    
      genericOutput2.exe > grab2-%%x.txt
      for /f "delims=" %%i in (grab2-%%x.txt) do set "grab2-%%x=%%i"
      echo grab2-%%x variable string is !grab2-%%x!
    )
    
    ::List all of the grab variable defined
    set grab
    

    You don’t need to save the output of your executables to a file. (Unless of course that is your requirement). You can use FOR /F to process the output directly. The FOR command has many variants that look nearly identical, yet behave very differently. This variant uses single quotes to cause FOR /F to process a command.

    setlocal enableDelayedExpansion
    for /L %%x in (1,1,2) do (
    
      for /f "delims=" %%i in ('genericOutput1.exe') do set "grab1-%%x=%%i"
      echo grab1-%%x variable string is !grab1-%%x!
    
      for /f "delims=" %%i in ('genericOutput1.exe') do set "grab2-%%x=%%i"
      echo grab2-%%x variable string is !grab2-%%x!
    )
    
    ::List all of the grab variable defined
    set grab
    

    Note that FOR /F will iterate each line (whether it be from a text file or from command output). Your algorithm will only save and print the content of the last line – each successive line will overwrite the value from the prior line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched this site, and numerous others, looking for a solution to this
I have searched high and low on this site and many others and have
This is my first post at this site. I searched alot but couldn't find
I've searched this site and the web and just looking for a simple example
I have searched this site and Google and even though the idea is pretty
Preface: I am sure this is incredibly simple, but I have searched this site
I searched all over the internet and this site and I can't figure this
I searched the site but I could not find any solution to this problem.
Okay, so I have searched this site and found many tutorials on how to
I searched through this site to find out how to rotate a image back

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.