Computing.Net > Forums > Windows XP > DOS:Read first line frm a txt file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

DOS:Read first line frm a txt file

Reply to Message Icon

Name: Divesh
Date: June 30, 2005 at 06:58:44 Pacific
OS: WInXP
CPU/Ram: Intel
Comment:

I have one text file say ( sample.txt) and the contents of that file are ( say) :
a.pcm
b.pcm
c.pcm
now I want to read first line from the sample.txt from a batch file ,now how can i do that. I tried the option For /f "tokens=1,2 delims=.pcm" %i in (sample.txt) do set First=%i
but the output i m getting is First=a First=b first =c which is not desired .I just output as First=a , i dont know how to break.exit from the for loop in DOS. Please help ?



Sponsored Link
Ads by Google

Response Number 1
Name: hiho
Date: June 30, 2005 at 07:43:59 Pacific
Reply:

You may be better off looking at an NT COMMAND PROMPT SOLUTION as there is no MS-DOS in the NT Family except in emulation NTVDM:

http://www.ss64.com/nt/

http://www.felgall.com/ntcl1.htm/

http://www.felgall.com/ntcl2.htm


0

Response Number 2
Name: dtech10
Date: June 30, 2005 at 08:29:28 Pacific
Reply:

Hi Divish

This help

@echo off
for /f %%a in (sample.txt) do (
echo %%a
exit /b
)


0

Response Number 3
Name: Divesh
Date: June 30, 2005 at 21:39:15 Pacific
Reply:

HI Guys ..thanks a lot ..the tip was really helpful and yeah Dtech the cmd u typed in worked just fine..
Thanks again.. Cheers :-)


0

Response Number 4
Name: Divesh
Date: July 1, 2005 at 02:25:21 Pacific
Reply:

Hi dtech , Now i ve another problem .. i have some more commands to execute after this code
@echo off
for /f %%a in (sample.txt) do (
set date=%%a
echo %%a
exit /b
)
how do i proceed with this as it just exits after this code , also as per my req I cant call my acript again so is there any way i will be able to conitnue executing my code which is next to the snippet given by you.
Your help wud be higly appreciated.

Thanks
Divesh


0

Response Number 5
Name: Divesh
Date: July 1, 2005 at 03:30:50 Pacific
Reply:

Hey guys i got the soln ..break cmd works fine .. cheers


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: July 1, 2005 at 03:39:23 Pacific
Reply:

Hi Divesh,

Let's see how you did it.

M2


If at first you don't succeed, you're about average.


0

Response Number 7
Name: Divesh
Date: July 1, 2005 at 05:27:45 Pacific
Reply:

Hi Everyone

I want to append some info in a file ..say
echo mem|*|* > mem.bat
echo cpu|*|*>> mem.bat
but the problem is that pipe is a redirector and it doesn't let me append that character in a file , anyway i can do this ?? Thanks



0

Response Number 8
Name: dtech10
Date: July 1, 2005 at 14:47:22 Pacific
Reply:

Hi
Is this what you want
echo mem ^|* ^|* > mem.bat
echo cpu ^|* ^|* >> mem.bat


0

Response Number 9
Name: Divesh
Date: July 2, 2005 at 03:50:48 Pacific
Reply:

wow..exactly !! thanks a lot dtech ! one more probs dtech .. there is a code which u told me
@echo off
for /f %%a in (sample.txt) do (
set date=%%a
echo %%a
exit /b
)
which runs for the first time and exits .. but my code which is below that isnt working .. becos the script exits after this ..any sol to this .. someone told me use BREAK cmd but it inst working ..any ideas .. ?


0

Response Number 10
Name: Mechanix2Go
Date: July 2, 2005 at 04:52:32 Pacific
Reply:

Hi Divesh,

I'm still learning. Thanks to dtech10, IVO and others.

Studying dtech's code and making some mods, I found out that:

exit /b

and

goto eof [or :eof]

seem to behave about the same.

So I experimented and figured out how to, in this case, put out just the first line, but not exit. Useful if we want the BAT to do something else. Take a look at this. It puts out the first line of the file and goes to a label.

::=======================
@echo off
for /f %%a in (sample.txt) do (
echo %%a
goto :1
)

:1
echo hello
::========================

By the way, NT has a built-in variable called DATE. So if your BAT reassigns the value of DATE, it could trip up something else. Not a great big deal, but something to be aware of. Maybe use a var like MYDATE or DATE2.

HTH


M2


If at first you don't succeed, you're about average.


0

Response Number 11
Name: dtech10
Date: July 2, 2005 at 09:04:23 Pacific
Reply:

Hi
This is what a book I have says about the Break command. Never tryed it.

Break
If CMD is being run under a debugger, this command causes a breakpoint. Under MS-DOS, this command was used to enable and disable Ctrl+C checking, but it has no such effect in Windows XP.



0

Response Number 12
Name: Divesh
Date: July 3, 2005 at 22:09:12 Pacific
Reply:

Hi Mechanix,dtech

I tried goto cmd and it worked exactly as i wanted it to.. thanks a lot.. u guys r gr8 :-) i m very new to this so i m asking lot of question .. ni m very sorry abt buggin u so much all the time /hope u guys dont mind it ..thanks again for u r help.
cheers
Divesh



0

Response Number 13
Name: Mechanix2Go
Date: July 3, 2005 at 22:51:23 Pacific
Reply:

Hi Divesh,

No problem on my end. Glad to help.

M2


If at first you don't succeed, you're about average.


0

Response Number 14
Name: dtech10
Date: July 4, 2005 at 07:32:18 Pacific
Reply:

Hi
I enjoy helping.
Very happy to appreciated.

DTech10


0

Response Number 15
Name: Divesh
Date: July 6, 2005 at 02:47:41 Pacific
Reply:

HI U expert guys ( D tech / M2 ) i m stuck with my script again .. i want to take the ipaddress of the machine in a variable.. in linux we have cmd hostname -i but in XP we have ipconfig which gives extraneous test too.. do anyone knows any other cmd for that..
Also i want to have a machinename in a variable but in lower case , the cmd %COMPUTERNAME% returns the machine name in caps .. any idea abt this ? i know that there is a cmd hostname which returns mahcine name in lower case but then i m not able to put the cmd's output redirected to a file ?? Kindly suggest


0

Response Number 16
Name: Mechanix2Go
Date: July 6, 2005 at 06:49:09 Pacific
Reply:

Hi Divesh,

Last question first.

HOSTNAME puts puts out upper/mixed case.

Get these utils:

More Power, Mr. Scott

which include "lower" and:

hostname |lower>hnlower

which will put the lowercase version in hnlower
============================

I'll get back to you with the code to set IP into a ver. What I have is messy, so I'll clean it up.


M2


If at first you don't succeed, you're about average.


0

Response Number 17
Name: Divesh
Date: July 7, 2005 at 02:50:06 Pacific
Reply:

Thanks dude :-))


0

Response Number 18
Name: Divesh
Date: July 7, 2005 at 05:30:52 Pacific
Reply:

Hi M2 , i m not able to using the cmd u told me
hostname |lower > IP.txt
do i have to place the COM files send by you at some specific location.
Kindly suggest


0

Response Number 19
Name: Mechanix2Go
Date: July 7, 2005 at 05:52:30 Pacific
Reply:

Hi Divesh,

For this, you need LOWER.COM in the same directory or, better yet, somewhere in the PATH.

I keep all that stuff in c:\util

M2


If at first you don't succeed, you're about average.


0

Response Number 20
Name: Mechanix2Go
Date: July 7, 2005 at 12:06:25 Pacific
Reply:

Hi Divesh,

Here's the code to get IP onto var.

You need change.com, available here:

Change

::======= gets IP into var %IP%
@echo off
ipconfig | find "IP Address" >ip.tmp
change.com ip.tmp " " "" > nul
FOR /f "tokens=2 delims=:" %%a in ('type ip.tmp') do SET IP=%%a
echo IP=%IP%
::==============t98


M2


If at first you don't succeed, you're about average.


0

Response Number 21
Name: Divesh
Date: July 7, 2005 at 22:55:00 Pacific
Reply:

Hi M2.. Thanks a ton man .. Both the cmds worked just as fine :-)) my probs solved now


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Windows XP Forum Home


Sponsored links

Ads by Google


Results for: DOS:Read first line frm a txt file

Read first n lines with MS-DOS bat file www.computing.net/answers/windows-xp/read-first-n-lines-with-msdos-bat-file-/179221.html

Reading and writing to a file in DOS www.computing.net/answers/windows-xp/reading-and-writing-to-a-file-in-dos/178103.html

Folder Moving from a .txt database. www.computing.net/answers/windows-xp/folder-moving-from-a-txt-database/168545.html