Computing.Net > Forums > Programming > Windows Bat: Find line in 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.

Windows Bat: Find line in file

Reply to Message Icon

Name: pednoy
Date: November 21, 2004 at 22:50:03 Pacific
OS: Win 2000
CPU/Ram: Xeon 1g
Comment:

Dear,

I have some problem about batch programming,

I have to write batch that find line
"Server (PID = 1856) Ready" or
"Server Mode 2 (PID = 2453) Ready"
in file and assign 1856 or 2453 to a variable.

Note that a line can be at anywhere in file.
1 file has only 1 line.

How can i do all of this with windows bat script? Or i need some execute program to help.
If anyone can help me it is a pleasure.

Thank you very much.
Pednoy



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: November 21, 2004 at 23:00:12 Pacific
Reply:

Do you mean that the file that batch is looking at has only one line?

Everything is on the same line?

Post the file.

M2


0

Response Number 2
Name: pednoy
Date: November 23, 2004 at 01:24:21 Pacific
Reply:

Sorry for unclear information.

There are many files that generated from my system. Each file will contain many lines and will contain one of two lines I mentioned above.
Unfortunately that I can not know exactly where "Server (PID = 1856) Ready" or
"Server Mode 2 (PID = 2453) Ready" will be at which line number.

Because the files are generate from legacy system and has variety name.
For detecting file name and read it, I can do now.
Next step is to retrieve number after "PID =" to assign to a variable.

Is there anyway to do this?


0

Response Number 3
Name: Mechanix2Go
Date: November 23, 2004 at 02:17:14 Pacific
Reply:

Hi,

This should do it:
::##
@echo off
set MYVar=

find "Server (PID = 1856) Ready" filename
if errorlevel 1 goto next
set MYVar=1856
goto end

:next

find "Server Mode 2 (PID = 2453) Ready" filename
if errorlevel 1 goto end
set MYVar=2453

:end


M2


0

Response Number 4
Name: pednoy
Date: November 24, 2004 at 16:58:03 Pacific
Reply:

Thanks Mechanix2Go.
You gave me many ideas.

How about if it is possible to find "(PID =" and move cursor to after its position. then read number after that until find ")".Because number is process number and it can vary.

Thanks for you kindly help


0

Response Number 5
Name: Mechanix2Go
Date: November 24, 2004 at 21:25:02 Pacific
Reply:

So, what you really want is the PID into a var, regardless of PID characters. Right?

M2


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: November 24, 2004 at 23:56:06 Pacific
Reply:

pednoy,

This should do it:


@echo off
:: This is the world-famous PID setter bat by Computer Mechanix
:: http://www.Golden-Triangle.com
::

set MYvar=
set MYvar2=
set PID=
set Mode2PID=

find "Server Mode 2 (PID" <filename>mode2
find "Server (PID" <filename>NOmode2

set /p MYvar2=<mode2
set /p MYvar=<NOmode2

set PID=%MYvar:~15,4%
set Mode2PID=%MYvar2:~22,4%

echo PID= %PID%
echo Mode2PID= %Mode2PID%


M2


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 Programming Forum Home


Sponsored links

Ads by Google


Results for: Windows Bat: Find line in file

Sed:Add text to end of line in file www.computing.net/answers/programming/sedadd-text-to-end-of-line-in-file/15109.html

echo text to specific line in file www.computing.net/answers/programming/echo-text-to-specific-line-in-file/11965.html

Delete a specific line in a text file www.computing.net/answers/programming/delete-a-specific-line-in-a-text-file/18879.html