1

I tried with batch to extract required code, but this doesn't work especially for big files. I'm wondering if this is possible with VB script. So,

I need to extract text from file between 2 delimiters and copy it to TXT file. This text looks like XML code, instead delimiters <string> text... </string>, I have :::SOURCE text .... ::::SOURCE. As you see in first delimiter are 3x of ':' and in second are 4x of ':'

Most important is that there are multiple lines between these 2 delimiters.

Example of text:

text&compiled unreadable characters
text&compiled unreadable characters
:::SOURCE
just this code
just this code
...
just this code
::::SOURCE text&compiled unreadable characters
text&compiled unreadable characters

Desired output:

just this code
just this code
...
just this code
halfer
  • 19,824
  • 17
  • 99
  • 186
Andy
  • 79
  • 1
  • 7

1 Answers1

1

Maybe you can try somethig like this:

filePath = "D:\Temp\test.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filePath)

startTag = ":::SOURCE"
endTag = "::::SOURCE"
startTagFound = false
endTagFound = false
outputStr = ""

Do Until f.AtEndOfStream
    lineStr = f.ReadLine
    startTagPosition = InStr(lineStr, startTag)
    endTagPosition = InStr(lineStr, endTag)

    If (startTagFound) Then
        If (endTagPosition >= 1) Then
            outputStr = outputStr + Mid(lineStr, 1, endTagPosition - 1)
            Exit Do
        Else
            outputStr = outputStr + lineStr + vbCrlf
        End If
    ElseIf (startTagPosition >= 1) Then
        If (endTagPosition >= 1) Then
            outputStr = Mid(lineStr, startTagPosition + Len(startTag), endTagPosition - startTagPosition - Len(startTag) - 1)
            Exit Do
        Else
            startTagFound = true
            outputStr = Mid(lineStr, startTagPosition + Len(startTag)) + vbCrlf
        End If
    End If
Loop

WScript.Echo outputStr

f.Close

I've made the assumption that start and end tag can be anywhere inside the file, not only at start of lines. Maybe you can simplify the code if you have more information on the "encoding".

Thomas Ledan
  • 118
  • 11
  • Thanks @Thomas, you are super star. Do you ha any idea how I can send file path from batch file to this script and echo to text file? Cheers, Andy – Andy Feb 22 '16 at 15:19
  • Hi @Andy, check those answers : http://stackoverflow.com/a/2806731/1123674 and http://stackoverflow.com/a/34046444/1123674. There are tons of other ressource on the web to pass arguments to script and to write to files. – Thomas Ledan Feb 22 '16 at 18:44