Batch file- read string in txt file

July 4, 2007 at 18:44:20
Specs: XP, 2GB
I'm looking to search through a text file for a string. Once I find this string, I want to read the string on the same line, after it.

Let me explain more...

I have a file called myfile.bat
Within this file is the following line:

I want to return "TheValueIWant"

If there were no space between "SET" and "WAS_NODE" I could easily read it as an ini file. I have the ability to search for and find this line and add a new line before or after it - or even concatenate information on to the end of it. However with all the tricks I do have, I cannot read this value where SET WAS_NODE=TheValueIWant.

Actually, I'll go one step further now. I do not need this done because I can do this part myself once I can return "TheValueIWant", but I actually only NEED "TheValue" I will need to trim n(IWant) characters off the end of the value returned to produce the final result I am looking for.

Any thoughts?

Many thanks in advance. Please - I do not want to bother anyone so if you do not fully understand the question, please don't waste your time by creating and posting code - just because....


See More: Batch file- read string in txt file

Report •

July 4, 2007 at 22:53:10
::== final2.bat
@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (myfile.bat) do (
echo %%a | find "SET WAS_NODE" > nul
if not errorlevel 1 set str=%%a
for /f "tokens=2 delims==" %%a in ('echo !str!') do (
set myvar=%%a
echo myvar=!myvar!
set finalvar=!myvar:~0,8!
echo finalvar=!finalvar!

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


Report •

July 5, 2007 at 16:32:18
Absolutely, positively, right on the money!

Works PERFECT and very fast!

Thank you so much~

Report •

Related Solutions

Ask Question