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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:53:17+00:00 2026-05-25T17:53:17+00:00

Can anyone explain this? I am able to count in a loop using the

  • 0

Can anyone explain this? I am able to count in a loop using the Windows command prompt, using this method:

SET /A XCOUNT=0
:loop
SET /A XCOUNT+=1
echo %XCOUNT%
IF "%XCOUNT%" == "4" (
  GOTO end
) ELSE (
  GOTO loop
)
:end

But this method does not work (it prints out “1” for each line in the file). It acts like the variable is out of scope:

SET /A COUNT=1
FOR /F "tokens=*" %%A IN (config.properties) DO (
  SET /A COUNT+=1
  ECHO %COUNT%
)
  • 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-25T17:53:18+00:00Added an answer on May 25, 2026 at 5:53 pm

    It’s not working because the entire for loop (from the for to the final closing parenthesis, including the commands between those) is being evaluated when it’s encountered, before it begins executing.

    In other words, %count% is replaced with its value 1 before running the loop.

    What you need is something like:

    setlocal enableextensions enabledelayedexpansion
    set /a count = 1
    for /f "tokens=*" %%a in (config.properties) do (
      set /a count += 1
      echo !count!
    )
    endlocal
    

    Delayed expansion using ! instead of % will give you the expected behaviour. See also here.


    Also keep in mind that setlocal/endlocal actually limit scope of things changed inside so that they don’t leak out. If you want to use count after the endlocal, you have to use a “trick” made possible by the very problem you’re having:

    endlocal && set count=%count%
    

    Let’s say count has become 7 within the inner scope. Because the entire command is interpreted before execution, it effectively becomes:

    endlocal && set count=7
    

    Then, when it’s executed, the inner scope is closed off, returning count to it’s original value. But, since the setting of count to seven happens in the outer scope, it’s effectively leaking the information you need.

    You can string together multiple sub-commands to leak as much information as you need:

    endlocal && set count=%count% && set something_else=%something_else%
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone explain why using comments like this in css cause really weird effects
Can anyone explain what this mod_rewrite rule is doing? I'm trying to comment the
Can anyone explain why this code gives the error: error C2039: 'RT' : is
Can anyone explain why this code with the given routes returns the first route?
Can anyone explain why this query returns an empty result. SELECT * FROM (`bookmarks`)
Can anyone explain to me what this means? Run-Time Check Failure #0 - The
Can anyone explain to me why this isn't working? #include <stdio.h> #include <stdlib.h> char
Can anyone explain what might be causing this error. Im thinking its the quotes.
Can anyone explain the output of this program and how I can fix it?
Can anyone explain why I'm getting this compile error? Duplicate 'Rad.Core.Aop.MethodArgumentValidation' attribute E:\Scripting\Rad.Core\Properties\AssemblyInfo.cs This

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.