1.

Solve : HOW TO READ INPUTS FROM FILE.?

Answer»

Hi all,

I need to read input from file as variable. Is there is any dos command available for this. i want to perform this in sequence. (Line by Line)

Regards,
Mohamed Asif KPthe usual command is FOR. Type FOR /? at the command prompt for more information. PLEASE give more information.

Code: [Select]@echo off
set /a LNE=0
:find
set /a LNE=%LNE%+1
for /f "skip=%LNE% delims=" %%m in ('FINDSTR /n . "C:\test.txt"') do goto find


set /a SKP=0

:begin
if %SKP% GTR %LNE% goto end
if %SKP% GTR 0 goto SECOND

:first

for /f "delims=" %%i in ('findstr . "C:\test.txt"') do (

echo %%i
goto second
)

:second
set /a SKP=%SKP%+1
for /f "skip=%SKP%" %%i in ('findstr . "C:\test.txt"') do (

echo %%i
goto begin
)

:end
echo.
echo Done!
echo.
@pause
That's kinda general, it just diplays, line by line, the lines of test.txt. Reply if you want something different.Darkblade, that's all very fine and all, and I salute the code wizardry, but why all those labels and gotos and fancy stuff? What's wrong, as a demo to a learner, with this...

for /f "delims=" %%F in (c:\test.txt) do (
echo %%F
)

Nothing. But if he WANTED to do something with the lines, findstr WOULD have to be used.Using the brackets like that makes the code shorter but as long as the work gets done, I don't see a problem. Dark Blade, I find in very long codes it's less complicating to use gotos but that's just me.
Everyone has their way.Quote from: Dark Blade on June 21, 2007, 02:54:56 AM

Nothing. But if he wanted to do something with the lines, findstr would have to be used.

Not necessarily.

Code: [Select]@echo off
REM create demo csv file
echo eggs,flour,milk > demo.txt
echo cream,butter,sugar >> demo.txt
echo vanilla,lemon,chocolate >> demo.txt
REM mmm...!

REM show first item in each line
for /F "delims=," %%L in (demo.txt) do (
echo %%L
)


result...

Code: [Select]eggs
cream
vanilla

i am actually curious what OP is going to do with those lines being read


Discussion

No Comment Found