Thread: vbscript help
View Single Post
Old 2008-11-10, 02:33   #1
IronBits
I ♥ BOINC!
 
IronBits's Avatar
 
Oct 2002
Glendale, AZ. (USA)

3·7·53 Posts
Default vbscript help

I'm hoping someone here knows enough about vbs to help me.
I have this script that works ok, but I'm sure it's not optimal and would appreciate any assistance to make it better.
Yes, I'm a newbie at it, and just trying to learn it. Try not to laugh too hard

First, the .txt file I'm parsing is Unix based ... cr/lf I think chars at the end of each line...

user=MyUserName is my Name
[2008/08/11 06:04:17]
305*2^587594-1 is not prime. Res64: D7E974A6678F50AD Time : 956.0 sec.
user=User Name 1
[2008/08/11 06:04:29]
301*2^587635-1 is prime! Time : 515.0 sec.
user=UserName2
[2008/08/11 06:04:34]
395*2^587644-1 is not prime. Res64: 64EDC1DAF0150DCD Time : 367.0 sec.

And now the in efficient code that does the work.

Code:
Option Explicit

Const ForReading = 1, ForWriting = 2, ForAppending = 8 

Dim oFSO, oInFile, oOutFile
Dim str_Input, str_Output, str_CurrentLine, str_CSV, MyArray, MyLine, UserName
Dim MyDate, MyMO, MyDD, MyYY, MyDay, MyAMPM
Dim MyTIME, MyHH, MyMM, MySS
Dim MyK, MyP, MyN, MyDATA, MyRES, MyT, MyD

' the path to the input and output file
str_Input  = "c:\Results.txt"
str_Output = "c:\Results.csv"

str_CSV=""

Set oFSO = CreateObject("Scripting.FileSystemObject")

' If the file exists
If oFSO.FileExists(str_Input) Then

    ' open the text file for input
    Set oInFile = oFSO.OpenTextFile(str_Input)

    ' create the csv file for output
    Set oOutFile = oFSO.CreateTextFile(str_Output,True)

    ' for each line in the input file
    Do While Not oInFile.AtEndOfStream

    ' read the line 

    str_CurrentLine = oInFile.ReadLine()
    str_CurrentLine = Replace(str_CurrentLine, Chr(13), "")
    str_CurrentLine = Replace(str_CurrentLine, Chr(10), "") 

    if Left(str_CurrentLine,5) = "user=" Then
        MyArray = Split(str_CurrentLine,"=",-1,1)
        UserName = MyArray(1)
    End If

    if Left(str_CurrentLine,1) = "[" Then
        '[08/11/2008 06:55:34 AM]
        MyLine = Split(str_CurrentLine," ", -1, 1 )
        MyDay  = Split(MyLine(0),"/", -1 ,1 )
        MyMO   = Replace( MyDay(0), "[", "" )
        MyDD   = MyDay(1)
        MyYY   = MyDay(2)
        MyTime = Split(MyLine(1),":")
        MyHH   = MyTime(0)
        MyMM   = MyTime(1)
        MySS   = MyTime(2)
        MyAMPM = Replace( MyLine(2), "]", "" )
        if MyAMPM = "AM" and MyHH = "12" Then MyHH = "00" End If
        If MyAMPM = "PM" and MYHH = "00" Then MyHH = "12" End If
            If MyAMPM = "PM" and MYHH = "01" Then MyHH = "13" End If
            If MyAMPM = "PM" and MYHH = "02" Then MyHH = "14" End If
            If MyAMPM = "PM" and MYHH = "03" Then MyHH = "15" End If
            If MyAMPM = "PM" and MYHH = "04" Then MyHH = "16" End If
            If MyAMPM = "PM" and MYHH = "05" Then MyHH = "17" End If
            If MyAMPM = "PM" and MYHH = "06" Then MyHH = "18" End If
            If MyAMPM = "PM" and MYHH = "07" Then MyHH = "19" End If
            If MyAMPM = "PM" and MYHH = "08" Then MyHH = "20" End If
            If MyAMPM = "PM" and MYHH = "09" Then MyHH = "21" End If
            If MyAMPM = "PM" and MYHH = "10" Then MyHH = "22" End If
            If MyAMPM = "PM" and MYHH = "11" Then MyHH = "23" End If
    End If
' 0              1  2   3       4      5                 6    7 8     9
' 305*2^587594-1 is not prime.  Res64: D7E974A6678F50AD  Time : 956.0 sec.

    If inStr(1,str_CurrentLine,"not prime.") Then
        MyDATA = Split(str_CurrentLine," ",-1,1)
        MyK = Split(MyDATA(0),"*",-1,1) ' 305 * 2 ^ 587594 - 1
        MyP = Split(MyK(1),"^",-1,1)    ' 2 
        MyN = Split(MyP(1),"-",-1,1)    ' 587594
        MyD = MyN(1) ' 1
        MyRES = MyDATA(6) ' residue
        MyT = MyDATA(10) ' Time
        Wscript.echo "user=" & UserName
        Wscript.echo "[" & MyYY  & "/" & MyMO  & "/" & MyDD & " " & MyHH & ":" & MyMM & ":" & MySS & "]"
        Wscript.echo MyK(0) & " " & "*" & " " & MyP(0) & " " & "^"  & " " & MyN(0) & " " & "-" & " " & MyD & " " & MyRES & " " & MyT
'        str_CSV = "user=" & UserName & VBCrLf &  "[" & MyYY  & "/" & MyMO  & "/" & MyDD & " " & MyHH & ":" & MyMM & ":" & MySS & "]" & VBCrLf & Str_CurrentLine
'        Wscript.echo str_CSV
        ' output the data to the csv file
        oOutFile.WriteLine(str_CSV)
    End If

' 301*2^587635-1 is prime!  Time : 515.0 sec.
    If inStr(1,str_CurrentLine,"prime!") Then
        MyDATA = Split(str_CurrentLine," ",-1,1)
        MyK = Split(MyDATA(0),"*",-1,1) ' 305 * 2 ^ 587594 - 1
        MyP = Split(MyK(1),"^",-1,1)    ' 2 
        MyN = Split(MyP(1),"-",-1,1)    ' 587594
        MyD = MyN(1) ' 1
        MyT = MyDATA(6) ' Time
        Wscript.echo "user=" & UserName
        Wscript.echo "[" & MyYY  & "/" & MyMO  & "/" & MyDD & " " & MyHH & ":" & MyMM & ":" & MySS & "]"
        Wscript.echo MyK(0) & " " & "*" & " " & MyP(0) & " " & "^"  & " " & MyN(0) & " " & "-" & " " & MyD & " " & "                 " & MyT
'        str_CSV = "user=" & UserName & VBCrLf & "[" & MyYY  & "/" & MyMO  & "/" & MyDD & " " & MyHH & ":" & MyMM & ":" & MySS & "]" & VBCrLf & Str_CurrentLine
'        Wscript.echo str_CSV
        oOutFile.WriteLine(str_CSV)
'        MsgBox "Prime Found!" & VBCr & "by: " & UserName & VbCr & Str_CurrentLine
    End If

str_CSV=""

LOOP

' Close the input and output file
    oInFile.Close
    oOutFile.Close

' Finished and exit
End IF

' Tidy up
    Set oFSO = Nothing
IronBits is offline   Reply With Quote