Solved vbscript to locate specific strings and start reading a file

November 14, 2013 at 00:03:01
Specs: Windows XP
Hi Vbgurus,

I'm trying to put together a script about which I'm not getting things right. Hope you may help me in getting it done.

I'm looking for a vbscript that will search for a specific string, if found, check for first occurence of another string and read the line that follow it. For e.g. my file would be like,
....
.....
......
STAGE
.....
.....
.....
....
FROM
<string to be extracted>
...
...
....
As in the above example, the script should search for the word "STAGE" first, if found check for the first occurence of word - "FROM" and read the line that follows - FROM.

NOTE - My file would have many such occurences and hence, I should search like above, and keep writing the STAGE, FROM & the immediate line to an xl file.

Thanks in advance for any help that you may offer.


See More: vbscript to locate specific strings and start reading a file

Report •

✔ Best Answer
November 14, 2013 at 19:55:40
There are questions, applying to both triggers (STAGE, FROM): are these always in caps? Do they always begin a line (ie: always preceded by either null or crlf)? Is there a terminating character (space, colon, whatever) to the string, or alternatively, is there any chance that "stage" or "from" could occur in the text stream other than your designated triggers? That said, here's my initial stab at it:
'========== begin vbscript
set fso=createobject("scripting.filesystemobject")
z=vbcrlf&fso.opentextfile("vbst",1).readall
x=split(z,vbcrlf&"STAGE",-1,vbtextcompare)
for i=0 to ubound(x)
p=instr(1,x(i),"from",vbtextcompare)
if p>0 then
y=split(mid(x(i),p+4),vbcrlf)
targ=y(0)
if targ="" then targ=y(1)
wscript.echo targ
end if
next
'======= end vbscript
edited to reflect updated requirements 11/16 pm brief test

message edited by nbrane



#1
November 14, 2013 at 00:56:53
Thanks for your reply Mechanix2Go. But unfortunately I need this as vbscript as this is going to be plugged in to an existing script which is already in VB.

Report •

#2
November 14, 2013 at 02:04:49
I deleted the earlier msg because I had it wrong. So you didn't miss much.

still not vbs...

::====== script starts here ===============
::
:: vbstart.bat 2013-11-14 15:37:26.48
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

for /f "tokens=1* delims=[]" %%t in ('find /n "STAGE" ^< myfile') do (
set G=%%t
)

:main
set S=
for /f "tokens=1* delims=[]" %%a in ('find /n "FROM" ^< myfile') do (
if not defined S if %%a gtr !G! set/a S=%%a+1
)

:sub1
set L=
for /f "tokens=* delims= " %%i in (myfile) do (
set /a L+=1
if !L! equ !S! echo.%%i
) >> NEWFILE
goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

#3
November 14, 2013 at 02:11:42
Mechanix2Go - Would it be possible to give me the equivalent VBScript for this?

Report •

Related Solutions

#4
November 14, 2013 at 02:25:45
So sorry,I would love to do but can not.

=====================
M2 Golden-Triangle


Report •

#5
November 14, 2013 at 02:37:32
Can anybody else help on this?

Report •

#6
November 14, 2013 at 06:47:53
So scan a file for "STAGE"..."FROM", and extract the following line, and repeat until end of file?

(I'm so tempted to just have the script write out and call Mechanix2Go's solution)

How To Ask Questions The Smart Way

message edited by Razor2.3


Report •

#7
November 14, 2013 at 08:32:19
Razor2.3 - Yes, first to check for STAGE, once found check for first FROM(there may be many FROM, but need to check only the first FROM) and extract the following line. Then check for next STAGE & FROM and so on....

Report •

#8
November 14, 2013 at 19:55:40
✔ Best Answer
There are questions, applying to both triggers (STAGE, FROM): are these always in caps? Do they always begin a line (ie: always preceded by either null or crlf)? Is there a terminating character (space, colon, whatever) to the string, or alternatively, is there any chance that "stage" or "from" could occur in the text stream other than your designated triggers? That said, here's my initial stab at it:
'========== begin vbscript
set fso=createobject("scripting.filesystemobject")
z=vbcrlf&fso.opentextfile("vbst",1).readall
x=split(z,vbcrlf&"STAGE",-1,vbtextcompare)
for i=0 to ubound(x)
p=instr(1,x(i),"from",vbtextcompare)
if p>0 then
y=split(mid(x(i),p+4),vbcrlf)
targ=y(0)
if targ="" then targ=y(1)
wscript.echo targ
end if
next
'======= end vbscript
edited to reflect updated requirements 11/16 pm brief test

message edited by nbrane


Report •

#9
November 14, 2013 at 22:34:29
nbrane - Thanks a lot for your help and script. It does exactly what I asked for.

To answer your questions -
are these always in caps? - 'STAGE' will always be in caps, but 'FROM' may or may not be.
Do they always begin a line (ie: always preceded by either null or crlf)? - STAGE always begins a line, but 'FROM' may or may not be starting a line. But in anycase, we need to search for the first FROM after every 'STAGE'(even if it is a part of the text stream)

Additionaly, I just looked at one of the user file and understand that there may either be some text in the same line as 'FROM' or in the next line. So if there is a text in the same line as FROM, we must be extracting that and not the following line. If there is no text after 'FROM' then we must be extracting the following line.


Report •

#10
November 15, 2013 at 13:32:33
Untested, because I don't have any test data, and I don't have time to make some:
Set fso = CreateObject("Scripting.FileSystemObject")
Set inFile = fso.OpenTextFile("someFile.txt")
Set outFile = fso.OpenTextFile("outFile.txt", 2, True)
line = ""

Do Until inFile.AtEndOfStream
  Do Until inFile.AtEndOfStream Or InStr(line, "STAGE")
    line = inFile.ReadLine
  Loop
  Do Until inFile.AtEndOfStream
    line = inFile.ReadLine
    txtPos = InStr(line, "FROM")
    If txtPos Then
      If (txtPos + 4) >= Len(RTrim(line)) Then
        line = inFile.ReadLine
        outFile.WriteLine line
      Else
        outFile.WriteLine Mid(line, txtPos + 5)
      End If
      Exit Do
    End If
  Loop
Loop

How To Ask Questions The Smart Way


Report •

#11
November 19, 2013 at 01:49:46
Thanks a lot for your help Razor2.3

Report •

Ask Question