0

I have an input file like this:

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

I want take the date, time and data and move in another output file formatted like this way:

31/07/13  23.39.17.75  000000540
28/04/13  22.19.35.22  000008940

As far now i tryied: EDITED

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:\Desktop\input.txt", ForReading, False)
Dim objOutputTS 'Text Stream Object
Set objOutputTS = objFSO.OpenTextFile("D:\Desktop\output.txt", ForWriting, True)



 Do Until objInputTS.AtEndOfStream
    Dim strLine
    strLine = objInputTS.ReadLine()

    c1 = "* BEGIN DATE"
    'WScript.Echo Len(c1)
    c2 = "* BEGIN DATE"
    'WScript.Echo Len(c2)
    c3 = "* DATA INPUT READ"
    'WScript.Echo Len(c3)

    If (Instr(strLine, 13) = "BEGIN DATE") Then objOutputTS.WriteLine(Mid(strLine, 20))

    If (Instr(strLine, 13) = "TIME BEGIN") Then objOutputTS.WriteLine(Mid(strLine, 20))

    If (Instr(strLine, 18) = "DATA INPUT READ") Then objOutputTS.WriteLine(Mid(strLine, 22)) 
Loop

objOutputTS.Close()
objInputTS.Close()

But in my output.txt file nothing appears. I think the problem is * but i don't know. I tryied in all ways. I can't find a way out. What's wrogn in my code?

2 Answers 2

1

Try replacing:

If (Left(strLine, 12) = "* BEGIN DATE ") ...

with

If Instr(strLine, "BEGIN DATE") then
   strLineSplit = Split(strLine, ":")
   objOutputTS.WriteLine(Trim(strLineSplit(1)))
end if

or something to this effect.

EDIT:

Within your loop:

Dim beginDate, endDate, dataInputRead 

strLine = objInputTS.ReadLine()

If Instr(strLine, "BEGIN DATE") then
    strLineSplit = Split(strLine, ":")
    beginDate = Trim(strLineSplit(1))
end if

If Instr(strLine, "END DATE") then
    strLineSplit = Split(strLine, ":")
    endDate = Trim(strLineSplit(1))
end if

If Instr(strLine, "DATA INPUT READ") then
    strLineSplit = Split(strLine, ":")
    dataInputRead = Trim(strLineSplit(1))
    objOutputTS.WriteLine(beginDate & " " & endDate & " " & dataInputRead & Chr(13) & Chr(10))
end if
Sign up to request clarification or add additional context in comments.

7 Comments

Ok now it works.. The only one little problem is the formatt of text. It's 31/07/13 23.39.17.75 000000540 31/07/13 23.39.17.75 000000540 line by line... And i want separate the 2 things with only 2 rows.
Append a line break to the end... something like & Chr(13) & Chr(10) (if memory serves me right). So objOutputTS.WriteLine(Trim(strLineSplit(1)) & Chr (13) & Chr(10))
@Ekkehard.Horner - wasn't trying to elude. For the sake of accuracy (and perhaps efficiency), I prefer Instr and Split functions over Left and Mid functions if/when possible.
@Di Vero Labs - with Chr it's perfect but i have to start a new line with the other datas..
@DiVeroLabs - If the task is to find 'needles' at the start of strings (Left(, looking for them anywhere in the string (InStr) is a mistake.
|
0

Your Left(strLine, 12) can never be equal to a string of length 13:

>> c1 = "* BEGIN DATE "
>> WScript.Echo Len(c1)
>>
13

WRT comment:

First you have to learn to count:

>> WScript.Echo """" & Mid(s1, 21) & """"
>>
"1/07/13"

Then you should put some diagnostic output into your script to check your assumptions, e.g.

WScript.Echo """" & Left|Mid(strLine, <decent number>) & """"

WRT edit:

Your

 If (Instr(strLine, 13) = "BEGIN DATE") Then

compares a string of length 13 to "BEGIN DATE" - length 10, no "* " prefix.

1 Comment

sooo, i've tryied to write: If (Left(strLine, 13) = "* BEGIN DATE ") Then objOutputTS.WriteLine(Mid(strLine, 21)) but nothing change! the second part maybe is wrong. I mean objOutputTS.WriteLine(Mid(strLine, 21))

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.