# Solved: Use bat file to search for numbers in first line of text file



## amallen (Jul 28, 2004)

Using a bat file, I need to read the first line of a text file and see if it has any numbers in it. I have figured out how to read the first line of a text file: set /p var1=<myfile.txt. I have also figured out how to search a file to see if it has any numbers: findstr [0-9] myfile.txt. But I haven't figured out how to read the first line only and then search the variable for any numbers. Any suggestions would be appreciated.

Thanks,

amallen


----------



## TheOutcaste (Aug 8, 2007)

Echo the variable and pipe it to findstr:
*echo %var1%|findstr [0-9] >Nul*

You can then check the errorlevel: 0 if f*0*und, 1 if m*1*ssing
You can also use this trick:

```
echo %var1%|findstr [0-9]
Goto result%errorlevel%
:: an errorlevel other than 0 or 1 will give an error and fail, so this will end the batch
Goto :EOF
:result0
:: If numbers found we get here
:: code to run if numbers found
:: Goto done
:result1
:: If no numbers found we get here
:: code to run if numbers not found
:done
```
HTH

Jerry


----------



## amallen (Jul 28, 2004)

Thanks, Jerry. I did find another solution but yours is cleaner (i.e. - less kludgey).

amallen


----------

