Is this possible VBScript to VB? ?

Is this possible? I have a VBScript that searches a log file for the word "Successful" or "Failed". If either one of the words exist in the file it then generates a log file named, success.log or fail.log. The VBScript is called from a batch file and has no user interaction at all. Can this be done using Visual Basic? And if so can anyone point me in the right direction to do this? I've done some VB programming but not like this. Could I just use the same VBScript code and just compile it to a VB exe?

Any help is apperciated.

thanks

Comments

  • : Is this possible? I have a VBScript that searches a log file for the word "Successful" or "Failed". If either one of the words exist in the file it then generates a log file named, success.log or fail.log. The VBScript is called from a batch file and has no user interaction at all. Can this be done using Visual Basic? And if so can anyone point me in the right direction to do this? I've done some VB programming but not like this. Could I just use the same VBScript code and just compile it to a VB exe?
    :
    : Any help is apperciated.
    :
    : thanks
    :
    This can be done in VB fairly easily. I've not used VBScript, so I can't say if it can be converted. Is the code long? if not, maybe you could post it here, and we could take a look?

    In any case, look at the Open command in VB. You would open the file, input it one line at a time, and search of the desired word.

    You don't need a form in VB, and don't need user interaction.

    Let us know if you need specific help.

    Melissa

  • : : Is this possible? I have a VBScript that searches a log file for the word "Successful" or "Failed". If either one of the words exist in the file it then generates a log file named, success.log or fail.log. The VBScript is called from a batch file and has no user interaction at all. Can this be done using Visual Basic? And if so can anyone point me in the right direction to do this? I've done some VB programming but not like this. Could I just use the same VBScript code and just compile it to a VB exe?
    : :
    : : Any help is apperciated.
    : :
    : : thanks
    : :
    : This can be done in VB fairly easily. I've not used VBScript, so I can't say if it can be converted. Is the code long? if not, maybe you could post it here, and we could take a look?
    :
    : In any case, look at the Open command in VB. You would open the file, input it one line at a time, and search of the desired word.
    :
    : You don't need a form in VB, and don't need user interaction.
    :
    : Let us know if you need specific help.
    :
    : Melissa
    :
    :

    VBScript is VERY similar to VB. Unfortunately, the file handling routines are not part of the similar sections...

    Start VB and load the default project (standard exe) if prompted. Remove Form1 from the project (right-click in the project window and select remove) and add a .bas module (right-click in project window and select "Add"). Open the bas module and enter this into the code window:
    [code]
    Private Sub Main()

    Dim FT As String
    Dim FFile As Long
    FFile = FreeFile
    Open "NameOfFileHere" For Binary As FFile
    FT = String$(255, 0)
    Get FFile, , FT
    Close
    If InStr(1, FT, "Successful") Then
    Open "success.log" For Output As FFile
    Close
    ElseIf InStr(1, FT, "Failed") Then
    Open "fail.log" For Output As FFile
    Close
    Else
    'Neither occured
    End If

    End Sub
    [/code]
    Compile it. If it complains about needing a startup module, select Sub Main from the dialog that appears.

    This code is best used on smaller files (I'd say up to 100K or so?). How long is the file that needs to be searched? If it's too big, I'll write a routine that will handle larger files more smoothly.

    Hope this helps!
  • : : : Is this possible? I have a VBScript that searches a log file for the word "Successful" or "Failed". If either one of the words exist in the file it then generates a log file named, success.log or fail.log. The VBScript is called from a batch file and has no user interaction at all. Can this be done using Visual Basic? And if so can anyone point me in the right direction to do this? I've done some VB programming but not like this. Could I just use the same VBScript code and just compile it to a VB exe?
    : : :
    : : : Any help is apperciated.
    : : :
    : : : thanks
    : : :
    : : This can be done in VB fairly easily. I've not used VBScript, so I can't say if it can be converted. Is the code long? if not, maybe you could post it here, and we could take a look?
    : :
    : : In any case, look at the Open command in VB. You would open the file, input it one line at a time, and search of the desired word.
    : :
    : : You don't need a form in VB, and don't need user interaction.
    : :
    : : Let us know if you need specific help.
    : :
    : : Melissa
    : :
    : :
    :
    : VBScript is VERY similar to VB. Unfortunately, the file handling routines are not part of the similar sections...
    :
    : Start VB and load the default project (standard exe) if prompted. Remove Form1 from the project (right-click in the project window and select remove) and add a .bas module (right-click in project window and select "Add"). Open the bas module and enter this into the code window:
    : [code]
    : Private Sub Main()
    :
    : Dim FT As String
    : Dim FFile As Long
    : FFile = FreeFile
    : Open "NameOfFileHere" For Binary As FFile
    : FT = String$(255, 0)
    : Get FFile, , FT
    : Close
    : If InStr(1, FT, "Successful") Then
    : Open "success.log" For Output As FFile
    : Close
    : ElseIf InStr(1, FT, "Failed") Then
    : Open "fail.log" For Output As FFile
    : Close
    : Else
    : 'Neither occured
    : End If
    :
    : End Sub
    : [/code]
    : Compile it. If it complains about needing a startup module, select Sub Main from the dialog that appears.
    :
    : This code is best used on smaller files (I'd say up to 100K or so?). How long is the file that needs to be searched? If it's too big, I'll write a routine that will handle larger files more smoothly.
    :
    : Hope this helps!
    :

    I see in the code it has "Open" then filename. Is this the same as "CreateTextFile" in VBScript? I just want the file to be created and not opened.

    You guys are a great help thanks.

    Heres the VBScript if you like to look at and maybe use it in the future.

    Const ForReading = 1
    Const Create = False

    dim objFSO, dbload, strLine, strFileName, code, LoadSuccess, LoadFailed, rc

    'file to search
    strFileName = ("name of file")

    set objFSO = CreateObject("Scripting.FileSystemObject")

    'open the file to be read
    set dbload = objFSO.OpenTextFile(strFileName, ForReading, Create)

    'check to see if file has data
    if NOT dbload.AtEndOFStream then
    Do while NOT dbload.AtEndOFStream

    'read the lines of file
    strLine = dblaod.ReadLine

    'look for the following word in file
    if InStr(strLine, "Successful DB Load") then
    rc = "ErrorLevel = 0"
    set LoadSuccess = objFSO.CreateTextFile("success.log")
    LoadSuccess.WriteLine code
    LoadSuccess.WriteLine rc

    end if

    if InStr(strLine, "Failed DB Load") then
    rc = "ErrorLevel = 1"
    set LoadSuccess = objFSO.CreateTextFile("failed.log")
    LoadSuccess.WriteLine code
    LoadSuccess.WriteLine rc

    end if

    'loop through file
    loop

    end if

    dbload.close
    set dbload = nothing
    set objFSO = nothing




  • : I see in the code it has "Open" then filename. Is this the same as "CreateTextFile" in VBScript? I just want the file to be created and not opened.
    :
    If you open it for Binary, Append, Output or Random and then just close it, it will have only created the file. If you open for Input and it doesn't exist, you get an error. If you open for Output, it will create the file if it doesn't exist and it will set the file's length to 0 if it's not.


    : Heres the VBScript if you like to look at and maybe use it in the future.
    :
    That code could be used with very few changes, but my code will be a lot faster than it would be. Use of FSO in VB is supported, but not recommended for such a simple task. Also, the variables dimmed in that code are all Variants (I think VBScript doesn't support anything else?) which are extremely slow compared to other data types (VB has to determine what the underlying data type is before using it). Another reason is the code reads in one line at a time which is VERY slow access. Basically, my code has the following advantages over a straight conversion:

    VB instead of FSO
    Strings/Longs instead of Variants
    Binary instead of Line Input

    Hope this helps!
    KDL
  • [code]
    Private Sub Main()

    Dim FT As String
    Dim FFile As Long
    FFile = FreeFile
    Open "NameOfFileHere" For Binary As FFile
    FT = String$(255, 0)
    Get FFile, , FT
    Close
    If InStr(1, FT, "Successful") Then
    Open "success.log" For Output As FFile
    Close
    ElseIf InStr(1, FT, "Failed") Then
    Open "fail.log" For Output As FFile
    Close
    Else
    'Neither occured
    End If

    End Sub
    [/code]


    : This code is best used on smaller files (I'd say up to 100K or so?).


    Hi KDL!

    Doesn't your routine just look at the first 256 characters? I see that you close after only one get. Am I missing something?

    I usually use "sequential" file access for things like this, but I guess your way is quicker...

    Thanks,

    Melissa

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories