Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Batch file (*.bat) syntax help - find string in file & grab next line

Status
Not open for further replies.

qjade

Programmer
Jun 7, 2004
42
US
Hello friends,

I apologize if posting this in the wrong forum. Saw a similar post here so thought I would start here. Please point to correct forum is I am wrong.

Problem: I have an *.xml file that I want to look through one line at a time via a *.bat file. I need to find a specific node in the "Original.xml" file (usually at the end of file) and copy its value to a variable or an out put file.

--/////////////////
i.e.
"Original.xml"

<Parent>
<something>
blahbalbhablha
</something>

<last_thing>
ThisIsWhatIwant
</last_thing>
</Parent>
--\\\\\\\\\\\\\\\\

All I have so far is that I was able to find the <last_thing> node but can not traverse to the next line. My code:

--///////My Attempt////////
@echo off

::FIND THE STRING <last_thing>
FOR /F "tokens=1" %%Q IN ('FINDSTR /B "<last_string>" Original.xml') DO SET varString = %%Q

ECHO %varString%
--\\\\\\\\\\\\\\\\\\\\\\\\

Any suggestion or direction to the appropriate forum would be greatly appreciated.

Thanks,
-NerdWannaBe

 
I don't think there is a right forum.
If I remember correctly, I think you need to use Edlin and it's subcommands inside your batch script.


_________________
Bob Rashkin
 
try:

Code:
 Dim t As String
 Dim objFSO As FileSystemObject
 Dim objFile As TextStream
Dim objFile1 As TextStream
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile1 = objFSO.OpenTextFile("C:\readme.txt", ForReading)
Set objFile = objFSO.OpenTextFile("C:\Test.txt", ForWriting, True)
Do Until objFile1.AtEndOfStream
t = objFile1.ReadLine
If InStr(t, ""<last_string>" ") > 0 Then

 objFile.WriteLine t
 objFile.Close
 objFile1.Close
Exit Do

End If
Loop
 
sorry code is for vba
change for .vbs
Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile1 = objFSO.OpenTextFile("C:\readme.txt", 1)
Set objFile = objFSO.OpenTextFile("C:\Test.txt", 2, True)
Do Until objFile1.AtEndOfStream
t = objFile1.ReadLine
If InStr(t, ""<last_string>" ") > 0 Then

 objFile.WriteLine t
 objFile.Close
 objFile1.Close
Exit Do

End If
Loop
 
pwise

Your empty line should have this

t = objFile1.ReadLine

shouldn't it?
 
Thanks for all the reply. First, unfortunately part of the requirements delivered to me was to have this solution in a *.bat file. Secondly, I am unfamiliar with *.vbs so would not know how to "compile" or run the provided code above from pwise. Is there anything special that must be done to run your vb script?

If anyone can chime in on the batch file approach I would greatly appreciate it.

Thanks again!
 

Copy/paste everything in notepad and save the file with vbs extension. No compilation needed.

 
Jerry:

I did not test my code just copied it from a program of mine,but i don't think i am missin a line
Code:
Do Until objFile1.AtEndOfStream
t = objFile1.ReadLine 'readline
If InStr(t, ""<last_string>" ") > 0 Then 'check for string
' if found
 objFile.WriteLine t 'write to file
 objFile.Close 'close files
 objFile1.Close
Exit Do 'exit loop

End If
'if not start loop 
Loop
 

pwise said:
If InStr(t, ""<last_string>" ") > 0 Then 'check for string
' if found
objFile.WriteLine t 'write to file
you write t which has <last_string> in it. OP needs the next line.

empty line = the next blank line after If

I was saying
Code:
Do Until objFile1.AtEndOfStream
t = objFile1.ReadLine 'readline
If InStr(t, ""<last_string>" ") > 0 Then 'check for string
' if found
[red]t = objFile1.ReadLine 'read next line of interest[/red]
 objFile.WriteLine t 'write to file
 objFile.Close 'close files
 objFile1.Close
Exit Do 'exit loop

End If
'if not start loop
Loop
 
jerry:
you are right i did not realize that qjade wants the line after "<last_string>
 

Sorry for steeling the joy of leaving that to the reader as an excercise [wink]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top