Windows Vista Forums

Calling a Function multiple times

  1. #1


    Aziz Guest

    Calling a Function multiple times

    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




      My System SpecsSystem Spec

  2.   


  3. #2


    Tom Lavedas Guest

    Re: Calling a Function multiple times

    On May 11, 9:37*am, "Aziz" <a...@newsgroup> 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

      My System SpecsSystem Spec

  4. #3


    Aziz Guest

    Re: Calling a Function multiple times

    "Tom Lavedas" <tglbatch@newsgroup> 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



      My System SpecsSystem Spec

  5. #4


    Marcello Guest

    RE: Calling a Function multiple times

    Hi Aziz
    ....

    > Function DeleteLine(strKey)
    .... Until here strKey is x400 or x500 or SystemMailbox

    > strKey = "x400"
    .... Now strKey is x400

    Marcello



      My System SpecsSystem Spec

  6. #5


    Tom Lavedas Guest

    Re: Calling a Function multiple times

    On May 11, 10:37*am, "Aziz" <a...@newsgroup> wrote:

    > "Tom Lavedas" <tglba...@newsgroup> 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

      My System SpecsSystem Spec

  7. #6


    MikeB Guest

    Re: Calling a Function multiple times

    On May 11, 8:37*am, "Aziz" <a...@newsgroup> 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"
    > *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
    WOuld it not be far more efficient to pass an array with all the
    values that you want to delete to the function and then in the
    function read each line once, compare it to all values in the array,
    delete if necessary and return?

    Surely reading the file once, comparing each line against a number of
    values and then returning is more efficient?

      My System SpecsSystem Spec


Calling a Function multiple times
Similar Threads
Thread Forum
Re: Calling excel function VB Script
Re: Calling function from script PowerShell
Call depth reached when calling function PowerShell
Problems calling a function PowerShell
calling imapi2 function put_MultisessionInterfaces return error Vista General