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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get text string from file

Status
Not open for further replies.

tdrclan

Programmer
Sep 18, 2007
52
US
I'm have a script that will list the XML's and did not produce a PDf.
getting the list of Pdf's works,
but now I need to get policy number from the xml file.
InStr is returning the pos number but I would like the text.


here is the code.


Set FileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='D:\to_print'} Where " _
& "ResultClass = CIM_DataFile")
For Each objFile In FileList

' Wscript.echo objFile.Drive & objFile.Path & objFile.FileName

If objFile.Extension = "xml" Then
strNewName = objFile.Drive & objFile.Path & objFile.FileName & "." & "pdf"

if objFSO.FileExists(strNewName) then
writeflag = "N"
else
strSearchName = objFile.Drive & objFile.Path & objFile.FileName & "." & "xml"

strMatch="<PolicyRef>" 'This is the string to search for.
if InStr(1,objFSO.OpenTextFile (strSearchName).ReadAll,strMatch,vbTextCompare) > 0 Then
' what code do I need here???? to get the line with the policyref number on it
strPolicy = InStr???
End If

strContents = strSearchName & " - " & strPolicy & vbCrLf
objFile2.Write strContents
strContents = ""
writeflag = "Y"
End If

End If
Next
 
You could try using the mid, left, or right functions since you know the position. See examples below:


strFound=Mid(objFile.Name, 7, 16)

string2=Mid(objFile.Name, 1, 16)

DefPrintWhere = instr(1,defprinter,",")
DefPrinter = left(DefPrinter,DefPrintWhere - 1)
 
Or you could consider the MS XML libraries, which know how to read an XML file directly, and mean that you can get at your tags and their contents with something as simple as

myXMLDocument.getElementsByTagName("PolicyRef")

(I'm not giving a full code solution here so as to give you a chance to figure out the details yourself, but if you have further questions just ask away)
 
I got it to work with the following

strPolicy = Mid(objFSO.OpenTextFile(strSearchName).ReadAll, strStart , 28)

I'm going to try the
getElementsByTagName option later, when I have time.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top