From: Aziz on
Hello,

I have this VB script, it goes through a text file and if it finds a line
with x400, x500 or SystemMailbox it deletes that line.
At the moment, the script removes all lines with x400 but won't do the same
for x500 or SystemMailbox. I think it's something to do with the value being
retained but don't know enough VB script to fix it. Can anyone help?




Option Explicit

Call DeleteLine("x400")
Call DeleteLine("x500")
Call DeleteLine("SystemMailbox")




Function DeleteLine(strKey)
'DeleteLine Function by TomRiddle 2008

Const ForReading=1:Const ForWriting=2
Dim objFSO,objFile,strLine,strLineCase,strNewFile,strFile,LineNumber
REM,CheckCase
strFile = "C:\proxyaddresses.txt"
strKey = "x400"

Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objFile=objFSO.OpenTextFile(strFile,ForReading)
Do Until objFile.AtEndOfStream
strLine=objFile.Readline
REM If CheckCase=0 then
strLineCase=ucase(strLine):strKey=ucase(strKey)
If LineNumber=objFile.Line-1 or LineNumber=0 then
If instr(strLine,strKey) or instr(strLineCase,strkey) or strKey=""
then
strNewFile=strNewFile
Else
strNewFile=strNewFile&strLine&vbcrlf
End If
Else
strNewFile=strNewFile&strLine&vbcrlf
End If
Loop
objFile.Close
Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objFile=objFSO.OpenTextFile(strFile,ForWriting)
objFile.Write strNewFile
objFile.Close

Set objFile = Nothing
Set objFso = Nothing
Set strKey = Nothing

End Function



From: Tom Lavedas on
On May 11, 9:37 am, "Aziz" <a...(a)aa.com> wrote:
> Hello,
>
> I have this VB script, it goes through a text file and if it finds a line
> with x400, x500 or SystemMailbox it deletes that line.
> At the moment, the script removes all lines with x400 but won't do the same
> for x500 or SystemMailbox. I think it's something to do with the value being
> retained but don't know enough VB script to fix it. Can anyone help?
>
> Option Explicit
>
> Call DeleteLine("x400")
> Call DeleteLine("x500")
> Call DeleteLine("SystemMailbox")
>
> Function DeleteLine(strKey)
>  'DeleteLine Function by TomRiddle 2008
>
>     Const ForReading=1:Const ForWriting=2
>     Dim objFSO,objFile,strLine,strLineCase,strNewFile,strFile,LineNumber
> REM,CheckCase
>  strFile = "C:\proxyaddresses.txt"

*** remove the next line of code ***
>  strKey = "x400"
****************************************
>
>     Set objFSO=CreateObject("Scripting.FileSystemObject")
>     Set objFile=objFSO.OpenTextFile(strFile,ForReading)
>     Do Until objFile.AtEndOfStream
>        strLine=objFile.Readline
>      REM  If CheckCase=0 then
> strLineCase=ucase(strLine):strKey=ucase(strKey)
>        If LineNumber=objFile.Line-1 or LineNumber=0 then
>           If instr(strLine,strKey) or instr(strLineCase,strkey) or strKey=""
> then
>              strNewFile=strNewFile
>           Else
>              strNewFile=strNewFile&strLine&vbcrlf
>           End If
>        Else
>           strNewFile=strNewFile&strLine&vbcrlf
>        End If
>     Loop
>     objFile.Close
>     Set objFSO=CreateObject("Scripting.FileSystemObject")
>     Set objFile=objFSO.OpenTextFile(strFile,ForWriting)
>     objFile.Write strNewFile
>     objFile.Close
>
> Set objFile = Nothing
> Set objFso = Nothing
> Set strKey = Nothing
>
>  End Function

This function has strKey hard coded into the function (see comment
above).
_____________________
Tom Lavedas
From: Marcello on
Hi Aziz
....
> Function DeleteLine(strKey)
.... Until here strKey is x400 or x500 or SystemMailbox
> strKey = "x400"
.... Now strKey is x400

Marcello


From: Aziz on
"Tom Lavedas" <tglbatch(a)verizon.net> wrote On May 11, 9:37 am
> <SNIP>
>This function has strKey hard coded into the function (see comment
> above).


Thanks for that Tom, silly mistake on my part.
I've removed that line, but it doesn't seem to have any real effect on the
script, it will still not remove x500 or SystemMailbox. Not sure what the
problem is, do I need to 'null' or 'destroy' any of the values or objects
created? (it's been a long time since I did anything beyond basic
scripting!)
Here's the modified script in full:


Option Explicit

Call DeleteLine("x400")
Call DeleteLine("x500")
Call DeleteLine("SystemMailbox")

Function DeleteLine(strKey)

Const ForReading=1:Const ForWriting=2
Dim objFSO,objFile,strLine,strLineCase,strNewFile,strFile,LineNumber
strFile = "C:\proxyaddresses.txt"

Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objFile=objFSO.OpenTextFile(strFile,ForReading)
Do Until objFile.AtEndOfStream
strLine=objFile.Readline
If LineNumber=objFile.Line-1 or LineNumber=0 then
If instr(strLine,strKey) or instr(strLineCase,strkey) or strKey=""
then
strNewFile=strNewFile
Else
strNewFile=strNewFile&strLine&vbcrlf
End If
Else
strNewFile=strNewFile&strLine&vbcrlf
End If
Loop
objFile.Close
Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objFile=objFSO.OpenTextFile(strFile,ForWriting)
objFile.Write strNewFile
objFile.Close

Set objFile = Nothing
Set objFso = Nothing
Set strKey = Nothing

Exit Function
End Function



--


Aziz


From: Tom Lavedas on
On May 11, 10:37 am, "Aziz" <a...(a)aa.com> wrote:
> "Tom Lavedas" <tglba...(a)verizon.net> wrote On May 11, 9:37 am
>
> > <SNIP>
> >This function has strKey hard coded into the function (see comment
> > above).
>
> Thanks for that Tom, silly mistake on my part.
> I've removed that line, but it doesn't seem to have any real effect on the
> script, it will still not remove x500 or SystemMailbox. Not sure what the
> problem is, do I need to 'null' or 'destroy' any of the values or objects
> created? (it's been a long time since I did anything beyond basic
> scripting!)

There are a number of unnecessary lines and variables in the script.
I'm not sure what the problem was, but I found it easier to edited it
to work for me.

Option Explicit

Call DeleteLine("x400")
Call DeleteLine("x500")
Call DeleteLine("SystemMailbox")

Function DeleteLine(strKey)

Const ForReading = 1, ForWriting = 2
Dim objFSO, objFile, strLine, strNewText, strFile, strLCaseKey
strFile = "C:\proxyaddresses.txt"

Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objFile=objFSO.OpenTextFile(strFile, ForReading)

strNewText = ""
strLCaseKey = Lcase(strKey)

Do Until objFile.AtEndOfStream
strLine=Lcase(objFile.Readline)
If instr(strLine, strLCaseKey) = 0 then
strNewText=strNewText & strLine & vbcrlf
End If
Loop

objFile.Close
Set objFile=objFSO.OpenTextFile(strFile, ForWriting)
objFile.Write strNewText
objFile.Close

Set objFile = Nothing ' I know that all of the 'manuals' do it
Set objFso = Nothing ' It's not really needed, but of no harm

End Function
_____________________
Tom Lavedas