How do I output the Text between 2 strings to a text file?HELP!  
Author Message
Idiot_idiot





PostPosted: Sat Feb 04 15:52:07 CST 2006 Top

VB Scripts >> How do I output the Text between 2 strings to a text file?HELP! I am trying to output the text between two strings , i giot to wite a
routine somthing as shown,but the output text file is BLANK!What am I
doing wron?I am new to VB script...Please Help!



Ajay
startstring = "ASSOCIATION TOTALS BY ACCOUNT TYPE"
endstring = "TOTAL""

sData = ReadFile("C:\Documents and Settings\Owner.AJAYGARG\My_
Documents\ASSOCIATION TOTALS BY ACCOUNT TYPE.doc")


startposition = InStr(sData, startstring)
sData = Left(sData, startposition)
endposition = InStr(sData, endstring) + Len(endstring) - 1
sData = Left(sData, endposition)


WScript.echo sData


Function ReadFile(FilePath)
'Given the path to a file, will return entire contents
' works with either ANSI or Unicode
Dim FSO, CurrentFile
Const ForReading = 1, TristateUseDefault = -2, _
DoNotCreateFile = False
Set FSO = createobject("Scripting.FileSystemObject")
If FSO.FileExists(FilePath) Then
If FSO.GetFile(FilePath).Size>0 Then
Set CurrentFile = FSO.OpenTextFile(FilePath, ForReading, _
False, TristateUseDefault)
If CurrentFile.AtEndOfStream <> True Then
ReadFile = CurrentFile.ReadAll
WriteFile "C:\Documents and Settings\Owner.AJAYGARG\My
Documents\ASSOCIATION TOTALS BY ACCOUNT TYPEf.doc", outData
: CurrentFile.Close
End If
End If
End If
End Function

Sub WriteFile(FILEPATH, sData)
'writes sData to FilePath
With CreateObject("Scripting.FileSystemObject")._
OpenTextFile(FilePath, 2, True)
.Write sData: .Close
End With
End Sub

Visual Studio149  
 
 
\





PostPosted: Sat Feb 04 15:52:07 CST 2006 Top

VB Scripts >> How do I output the Text between 2 strings to a text file?HELP! > I am trying to output the text between two strings , i giot to wite a
> routine somthing as shown,but the output text file is BLANK!What am I
> doing wron?I am new to VB script...Please Help!

The output file is blank because you are writing undefined data to it. Even if
that were not true, you are writing data to the file before it has been
modified, and even if it were modified, it would be wrong. :-)

The way to write successful script is one step at a time, especially if you are
new at it. Keep it simple and don't try to combine everything into one big
kluge. This is not the place for a complete tutorial or step by step analysis of
your script. I have rewritten it so that it should work, but I have no way to
test it under actual working conditions.
--
Crash
'---------------------------------------------------------------------------
startstring = "ASSOCIATION TOTALS BY ACCOUNT TYPE"
endstring = "TOTAL"

Set fso=CreateObject("Scripting.FileSystemObject")

sData = ReadFile("C:\Documents and Settings\Owner.AJAYGARG\My "_
& "Documents\ASSOCIATION TOTALS BY ACCOUNT TYPE.doc")

if len(sData) then

startposition = InStr(sData, startstring)
endposition=inStrRev(sData,endstring)

if startposition>0 AND endposition>0 then
sData=mid(sData,startposition+len(startstring),_
endposition-(startposition+len(startstring)))
end if

WriteFile "C:\Documents and Settings\Owner.AJAYGARG\My "_
& "Documents\ASSOCIATION TOTALS BY ACCOUNT TYPEf.doc", sData

end if

Set fso=nothing

Function ReadFile(FilePath)
ReadFile=""
If fso.FileExists(FilePath) Then
If fso.GetFile(FilePath).Size>0 Then
Set CurrentFile = fso.OpenTextFile(FilePath)
ReadFile = CurrentFile.ReadAll
CurrentFile.Close
End If
End If
End Function

Sub WriteFile(FilePath,data)
Set outFile=fso.CreateTextFile(FilePath)
outFile.write data
outFile.close
end sub


 
 
RodeCa





PostPosted: Wed Feb 08 07:06:11 CST 2006 Top

VB Scripts >> How do I output the Text between 2 strings to a text file?HELP! But true problem is (at least ;-):
> startposition = InStr(sData, startstring)
> sData = Left(sData, startposition) ' *** LEFT. Sure?
your new sData contains *from beginning to startstring*: you have discarded
from that point to end

--
HIH



"AG" <EMail@HideDomain.com> escribió en el mensaje
news:EMail@HideDomain.com...
>I am trying to output the text between two strings , i giot to wite a
> routine somthing as shown,but the output text file is BLANK!What am I
> doing wron?I am new to VB script...Please Help!
>
>
>
> Ajay
> startstring = "ASSOCIATION TOTALS BY ACCOUNT TYPE"
> endstring = "TOTAL""
>
> sData = ReadFile("C:\Documents and Settings\Owner.AJAYGARG\My_
> Documents\ASSOCIATION TOTALS BY ACCOUNT TYPE.doc")
>
>
> startposition = InStr(sData, startstring)
> sData = Left(sData, startposition)
> endposition = InStr(sData, endstring) + Len(endstring) - 1
> sData = Left(sData, endposition)
>
>
> WScript.echo sData
>
>
> Function ReadFile(FilePath)
> 'Given the path to a file, will return entire contents
> ' works with either ANSI or Unicode
> Dim FSO, CurrentFile
> Const ForReading = 1, TristateUseDefault = -2, _
> DoNotCreateFile = False
> Set FSO = createobject("Scripting.FileSystemObject")
> If FSO.FileExists(FilePath) Then
> If FSO.GetFile(FilePath).Size>0 Then
> Set CurrentFile = FSO.OpenTextFile(FilePath, ForReading, _
> False, TristateUseDefault)
> If CurrentFile.AtEndOfStream <> True Then
> ReadFile = CurrentFile.ReadAll
> WriteFile "C:\Documents and Settings\Owner.AJAYGARG\My
> Documents\ASSOCIATION TOTALS BY ACCOUNT TYPEf.doc", outData
> : CurrentFile.Close
> End If
> End If
> End If
> End Function
>
> Sub WriteFile(FILEPATH, sData)
> 'writes sData to FilePath
> With CreateObject("Scripting.FileSystemObject")._
> OpenTextFile(FilePath, 2, True)
> .Write sData: .Close
> End With
> End Sub
>