# Solved: DOS - problem with the batch file on using for loop



## trusp (Dec 5, 2008)

Hi,
I have a problem with my batch file.
I ran my script to get the next process number from the Num file. 
Here the problem is that instead the value in the text file "ECHO is on." is placed to the temp variable.

Batch Command:
E:\>for /F "eol=; tokens=1 delims=" %%i in (C:\Num.txt) do (set /a no=%%i+1 )

CLI Output: 
E:\>for /F "eol=; tokens=1 delims=" %%i in (C:\Num.txt) do (set /a no=%%i+1 )
E:\>(set /a no=ECHO is on.+1 )
Missing operator.

Num.txt - Is the text file which contains the process number.
I think the problem may be in for loop. 
Please help me out in this issue asap.

Trusp
​


----------



## TheOutcaste (Aug 8, 2007)

What is the content of C:\Num.txt?
If it's *Echo is on.* then that is the result you'll get.

I'd be looking at the script that creates the C:\Num.txt file
For example, if it's created by a statement like this:
echo %var% >C:\Num.txt
if var is not defined, C:\Num.txt will contain *Echo is on.*

HTH

Jerry


----------



## trusp (Dec 5, 2008)

Jerry,

Thanks for your Response

This Example contains the Similar Script
@echo on
set Path=C:\Temp.txt
for /f "eol=; tokens=1 delims=" %%i in (%Path%) do (set /a no=%%i+1)
echo First %no%
set no=1
echo %no%> %Path%
for /f "eol=; tokens=1 delims=" %%i in (%Path%) do (set /a no1=%%i+1)
echo Final %no1%
Here I am trying to read a number from file and after that trying to set it as 1.
But its setting as "ECHO is on".
I cant find the reason for it.
set no=1
echo %no%> %Path%
Please help me out in this issue


Trusp


----------



## TheOutcaste (Aug 8, 2007)

set no=1
echo %no%> %Path%

Here's the problem. Echoing just a number using redirection has a little quirk, so has to be handled differently. If there is no space between the number and the redirection symbol, it sees the number as part of the redirection operator, not as the item to be echoed.
the *>* and *<* redirection symbols are actually *n<* and *n>*, where *n* is a number from 0-9, specifying the handle to be redirected. If *n* is not specified, *>* defaults to *1>* and *<* defaults to *0<*:

```
[B]Handle  Num  Description[/B]
STDIN   0    Keyboard input
STDOUT  1    Output to the Command Prompt window
STDERR  2    Error output to the Command Prompt window
```
3-9 are undefined by Windows, but can be defined by an application.

So in your file since you set *no* to 1, and there is no space between *%no%* and the *<*, *echo %no%> %Path%* means *echo 1> %path%*, i.e., redirect the STDOUT of the Echo command to the file, rather than *echo 11> %path%*.
Echo by itself outputs either *Echo is On.* or *Echo is Off.*

So, three ways around it, add a space between *%no%* and *>*, add a period, same as you would to echo a blank line, or put the redirection at the start of the line:
*echo %no% >%Path%
echo.%no%>%Path%
>%Path% echo %no%*

HTH

Jerry


----------



## trusp (Dec 5, 2008)

Thank you Jerry.


----------



## TheOutcaste (Aug 8, 2007)

You're Welcome!


----------

