1

I have a batch file that counts from 0-100 and echos 1-100 into a text file. but the first 9 numbers are not echoed, it just says echo is off. Can anyone help?

@echo off
:start
set /a count=%count%+1
echo %count%>>file.txt
if %count%==100 goto end
goto start
:end
pause

this is the code i am using, and the text output is below.

ECHO is off.
10
11
12
13

as you see it counts all the way to 100 after the first 9 are skipped.

Mark
  • 3,609
  • 1
  • 22
  • 33
C-Earhart
  • 31
  • 2

2 Answers2

3

Add a space between echo %count% and >>file.txt. 2> is a redirect operator. Batch Redirect Operators. Also, add set count= before :start in-case the var already exists.

You can also use a For loop instead of your goto and labels. - Batch For Loops

@echo off
:start
set /a count=%count%+1
echo %count% >>file.txt
if %count%==100 goto end
goto start
:end
pause
Mat Carlson
  • 543
  • 3
  • 12
  • thanks so much :) also is there any way to remove the blank line at the end of the output text? thanks in advance – C-Earhart Sep 27 '13 at 19:21
  • I think the answer here [Deleting last n lines from file using batch file](http://stackoverflow.com/questions/12345964/deleting-last-n-lines-from-file-using-batch-file) will work for you. – Mat Carlson Oct 04 '13 at 18:57
3

the best solution is a reverse order of the arguments:

>>file.txt echo %count%
Endoro
  • 37,015
  • 8
  • 50
  • 63