Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 ?

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

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

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

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

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 .. ?

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.

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.

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

Hi Divesh,
No problem on my end. Glad to help.
M2
If at first you don't succeed, you're about average.

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

Hi Divesh,
Last question first.
HOSTNAME puts puts out upper/mixed case.
Get these utils:
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.

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

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.

Hi Divesh,
Here's the code to get IP onto var.
You need change.com, available here:
::======= 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.

![]() |
![]() |
![]() |

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