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

  • SEARCH
  • Home
  • 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 8063167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:55:58+00:00 2026-06-05T10:55:58+00:00

I found a bug in a bat file I was writing and by accident

  • 0

I found a bug in a bat file I was writing and by accident found a fix however I don’t understand why the fix worked.. Which is ultimately my question, or if there are other problems with this logic that I have not discovered.

The script does a line count in 2 or more files then the goal is to find the file with the lowest line count and later use that count in a variable later in the script as a parameter.

Example file 1 (9 lines):

Wrote,0
Wrote,1
Wrote,2
Wrote,3
Wrote,4
Wrote,5
Wrote,6
Wrote,7
Wrote,8

Example file 2 (10 lines):

Wrote,0
Wrote,1
Wrote,2
Wrote,3
Wrote,4
Wrote,5
Wrote,6
Wrote,7
Wrote,8
Wrote,9

The main difference that I see is that lines 9 & 10 in file 2 are two digit line numbers

NOTE: I am not referring to the actual value on each line but only the line number’s count value as my code does not try to parse the lines but only get the line count.

Near the top of the code I create then append the count values for each file into a temp file called ~numbers.txt so to simulate that process here assume my starting code is:

echo 9 > ~numbers.txt
echo 10 >> ~numbers.txt
type  ~numbers.txt

So now I have a file with two lines “9” and “10” and of course 9 is lower than 10 but it also is one digit less than 10.

Now I attempt to find the lowest number contained in the file ~numbers.txt by reading the 1st line into a variable then looping and evaluating if current value is less than prior value.

setlocal enabledelayedexpansion
set /p lowest=<~numbers.txt

FOR /F "delims=" %%G in (~numbers.txt) DO IF %%G LSS !lowest! SET lowest=%%G
echo  lowest num = %lowest%

When I run this code I get this output which is wrong:

lowest num = 10

What I have found is that if I remove the “delims=” from the FOR loop I will get the correct result of 9.

ALSO and what really screws up my understanding of the problem; If I only change the values in ~numbers.txt to 10 and 100 and leave the FOR loop line alone (keeping “delims=” ) then I get the correct value of 10 as my answer???

This is bad because it implies that my logic is flawed and that other number combinations will also yield different results.

Can anyone explain to me why this is the case and/or give an example on how to get the correct lowest number regardless of the values?

Thanks…

  • 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-05T10:56:00+00:00Added an answer on June 5, 2026 at 10:56 am

    If you don’t see something it doesn’t mean it doesn’t exist. (I’m talking of spaces here)
    Try this instead:

    > ~numbers.txt echo 9<press ENTER immediately>
    >> ~numbers.txt echo 10<press ENTER immediately>
    

    This will be truly WYSIWYG, no spaces at the end of line, no text comparisons by if because strings are not all numbers.
    Of course, stripping the spaces with default delims (one of which is a space) also works fine as you have noticed already.

    Consider this way of determining lowest line count:

    set /a minlines=0x7FFFFFFF
    for /f "tokens=2 delims=:" %%a in ('find /c /v "" *.txt') do set /a minlines="%%a-(minlines-%%a)*(minlines-%%a>>255)"
    echo %minlines%
    

    Note: no ifs, no delayed expansion

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

Sidebar

Related Questions

I just found a bug in my Rails app which would lead to certain
We've found a bug in a program that we bought from another company, which
I found a bug in the Perl module IO::Socket::SSL and I could probably fix
I think I found a bug in opencart so in the featured module there
Earlier today I've found a bug in one of our projects - there is
I found a bug in my code which boiled down to comparing Double(0.0) with
Found a bug in IE (all versions), wondering if there is a workaround. This
Fix this bug in Chrome 20.0.1115.1 dev-m I found a bug in chrome20. Do
I'm writing tests for my Grails-Application and I found a bug in my application
Say I just found a bug involving javascript workflow in a web app. Most

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.