Loop issue in displaying data in TXT file

June 28, 2010 at 06:45:38
Specs: Windows XP
HI,

I want to display datas in text file. Here i have Listfile.txt and want to display each line using batch file. How to do this with looping. Below is my code

for /f "tokens=* delims= " %%a in (Listfile.txt) do (
set /a N+=1
set v!N!=%%a
)
set hostname=!v1!
echo %hostname%
pause

Data in Listfile.txt

4mLinuxMachine.cpp
ShutdownPanel.cpp
windows.cpp

How to display each using hostname variable

Please help


See More: Loop issue in displaying data in TXT file

Report •

#1
June 28, 2010 at 12:04:41
(Mine's not to reason why...)

Is this what you're looking for:

@echo off

setlocal EnableDelayedExpansion

set N=
for /f "tokens=* delims= " %%a in (Listfile.txt) do (
  set /a N+=1
  set v!N!=%%a
)
for /l %%i in (1, 1, %N%) do echo !v%%i!

?


Report •

#2
June 28, 2010 at 21:42:12
Thanks for your valuable answer
I have some more doubt.
Here i want to pass !v%%i! to other for loop to change carriage return in text file.
Is it possible to pass this variable.

THis is my code

@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (Listfile.txt) do (
set /a N+=1
set v!N!=%%a
)

::set hostname=!v1!
::echo %hostname%

for /l %%i in (1, 1, %N%) do (
echo !v%%i!


for /f "tokens=* delims=" %%i in (!v%%i!) do ( :: this part not working
echo %%i >> Linux11.cpp

)
pause


Report •

#3
June 29, 2010 at 05:44:22
The first thing that I notice in your code is that you haven't
cleared variable N or set it to zero before the first for loop.
Looking further down past the first for loop, it's difficult to know
what's going on because your opening and closing brackets
don't add up and I'm not sure if that's supposed to be two
separate loops or an outer and an inner...

Can I ask what exactly are you trying to accomplish? Could
it be that you want to concatenate all lines into one line or
maybe you want to convert CRLFs to LFs?


Report •
Related Solutions


Ask Question