David_D
David_D

Reputation: 1402

Output file is empty using vbscript

Starting from an input file (input.txt) i have to find some strings and write them in another file txt (output.txt) This is the input.txt

**********************************************************
* NAME           : CONTROLLER                                                          
* FUNCTION       : NOTHING IMPORTANT                                           
* BEGIN DATE     : 31/07/13                               
* TIME BEGIN     : 23.39.17.75                            
**********************************************************
* DATA INPUT READ  : 000000540                            
**********************************************************

And this is the code:

Const ForReading = 1
Const ForWriting = 2
Dim objFSO 'File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objInputTS 'Text Stream Object
Set objInputTS = objFSO.OpenTextFile("D:\input.txt", ForReading, False)
Dim objOutputTS 'Text Stream Object
Set objOutputTS = objFSO.OpenTextFile("D:\output.txt", ForWriting, True)

Do Until objInputTS.AtEndOfStream
    Dim strLine
    strLine = objInputTS.ReadLine()
    If (Left(strLine, 13) = "BEGIN DATE:") Then objOutputTS.WriteLine(Mid(strLine, 20))
    If (Left(strLine, 13) = "TIME BEGIN:") Then objOutputTS.WriteLine(Mid(strLine, 20))
    If (Left(strLine, 18) = "DATA INPUT READ:") Then objOutputTS.WriteLine(Mid(strLine, 22))    
Loop

objOutputTS.Close()
objInputTS.Close()

But in the output file nothing appears. Any helps? i want this output for esxample

20/05/2013 22/05/2013 21.00.00.00 0000000054

Upvotes: 0

Views: 376

Answers (1)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

The line

* BEGIN DATE     : 31/07/13

does not match the condition

If (Left(strLine, 13) = "BEGIN DATE:")

you have to ac*count* for the "* " prefix too.

To spell it out:

>> s1 = "* BEGIN DATE     : 31/07/13"
>> s2 = Left(s, 13)
>> WScript.Echo """" & s2 & """"
>>
"* BEGIN DATE "
>> c1 = "BEGIN DATE:"
>> c2 = "* BEGIN DATE "
>> WScript.Echo 1, CStr(c1 = s2)
>> WScript.Echo 2, CStr(c2 = s2)
>>
1 False
2 True

Upvotes: 1

Related Questions