VB Script copy specific lines in text file to

October 7, 2010 at 08:23:24
Specs: Windows XP Pro, E7500 2.92 GHz 4 GB
I am trying to create a VB script that will copy specific lines from a text file to another file.
it should search each line and if the 33rd char is either a 1 or 0 copy that line to a new text file.

This is what I have so far, this checks the 33rd character and prints it to console.

I tried putting an If then Else statement in this vbscript to only show 0 or 1and it doesn't work.

I have snippets of almost each function, but seem to get them to work together.


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\scripts\f622sta.wri", 1)
Do Until objFile.AtEndOfStream
objFile.Skip(33)
strCharacters = objFile.Read(1)
Wscript.Echo strCharacters
objFile.SkipLine
Loop

Any help would be very much appreciated.

Thank you in advance

-Don


See More: VB Script copy specific lines in text file to

Report •


#1
October 7, 2010 at 10:24:24
objFile.Skip(33)
strCharacters = objFile.Read(1)

This would show you character 34, not 33.

Also, define "line"

How To Ask Questions The Smart Way


Report •

#2
October 7, 2010 at 12:29:42
Hi, thanks for the correction, Yes, I am looking at char 34.

Here are a couple of lines so you can see what I have to move.

24-000107-1 MEADOW-BROOK ACRES | | | | | | | | | |

24-000113-1 RONNIE &OR CAROL SWE 09/12/10 | 4.33 |3.42 | |5.59 | | 63 U| 63| 610 U|1600 U| 18 U|-.546 |

The first line has no relevant data

The second line does, the constant is the date so only need to verify that char 34 is a 0 or 1 (which is the first number of the date), and if so, copy that whole line of data to a new file.

there are about 500 lines of either non-relevant data, blanks and relevant.

Let me know if you need anything else, and thanks for your help.


Report •

#3
October 7, 2010 at 12:58:25
Seems error prone, but you have better access to your data than I do.
Set fso = CreateObject("Scripting.FileSystemObject")
Set inFile  = fso.OpenTextFile("c:\scripts\f622sta.wri", 1)
Set outFile = fso.OpenTextFile("output.txt", 2, True)
Do Until inFile.AtEndOfStream
  line = inFile.ReadLine
  If Mid(line, 34, 1) = "0" Or Mid(line, 34, 1) = "1" Then _
    outFile.WriteLine line
Loop

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
October 8, 2010 at 03:52:19
Wow, thank you very much, it works great, and the script is 1/3 the size of the one I have.
(that didn't work right)
Have a great weekend

-Don


Report •

#5
October 13, 2010 at 11:45:24
Ok, this is the next issue.

After running that vbscript and the other I created to further format that file, this is what the output is:

50 1 9/12/2010 3.93 3.23 5.59 6 340 3 10 546 NF 39
111 1 9/2/2010 3.59 2.99 13.7 5.67 4.74 320 U 542
9/4/2010 3.75 2.88 13.3 5.71 4.78 290 544
9/6/2010 3 340 U 3 E < 1 NF 37
9/8/2010 3.51 3.07 11.7 5.62 4.74 250 545
9/12/2010 3.72 3.08 11.6 5.61 4.72 3 300 3 E < 1 538
9/16/2010 3.67 3.13 9.9 5.59 4.66 310 U 541

What I now need it to do is add the missing numbers in front of the dates.

If you look at the second line that starts with 111 (tab) 1 (Tab) 9/2/2010 ETC
the next starts with the date and goes until it gets to
111 (tab) 2 (tab) Date
I need to fill the blanks before the date with the # (Tab) # (Tab)

so that it looks like this
111 1 9/2/2010 3.59 2.99 13.7 5.67 4.74 320 U 542
111 1 9/4/2010 3.75 2.88 13.3 5.71 4.78 290 544
111 1 9/6/2010 3 340 U 3 E < 1 NF 37
111 1 9/8/2010 3.51 3.07 11.7 5.62 4.74 250 545
111 1 9/12/2010 3.72 3.08 11.6 5.61 4.72 3 300 3 E < 1 538
111 1 9/16/2010 3.67 3.13 9.9 5.59 4.66 310 U 541
111 2 9/2/2010 3.52 2.92 17 5.56 4.62 260 526 U
111 2 9/8/2010 2 150 4 E < 1 NF 38
111 2 9/12/2010 3.35 3.07 10.1 5.57 4.73 160 543
113 1 9/12/2010 4.33 3.42 5.59 63 U 610 U 1600 U 18 U 546 NF 39

Thank you in advance.

The numbers can range from 1 to 9999
-Don


Report •

#6
October 14, 2010 at 11:29:25
Question: Does this intermediate step get rid of the | characters? Because it might be easier to fix the lines if they remain.

How To Ask Questions The Smart Way


Report •

#7
October 14, 2010 at 11:50:05
Yes, it does but we can leave them in, and I can remove them after.

Report •

#8
October 14, 2010 at 12:36:39
If the first two fields you want to add are blank fields, it'd be easier, yeah. What would that look like?

How To Ask Questions The Smart Way


Report •

#9
October 18, 2010 at 11:17:30
What do you mean?

Thanks


Report •

#10
October 19, 2010 at 12:50:10
yes, the first 2 fields are blanks, that is why it is showing the date first. The Tabs are there, it just didn't show it on the post.

This is what it looks like. After I run the script above, and before I run my script.

24-000050-1 LEE/WALTER/DIANE BER 09/12/10 | 3.93 |3.23 | |5.59 | | 6 |E< 25| 340 | 3 | 10 |-.546 | | NF |39
24-000111-1 JOSEPH &OR CHRISTI(O)09/02/10 | 3.59 |2.99 |13.7 |5.67 |4.74 | | | 320 U| | |-.542 | | |
09/04/10 | 3.75 |2.88 |13.3 |5.71 |4.78 | | | 290 | | |-.544 | | |
09/06/10 | | | | | | 3 |E< 25| 340 U| 3 |E < 1 | | | NF |37
09/08/10 | 3.51 |3.07 |11.7 |5.62 |4.74 | | | 250 | | |-.545 | | |
09/12/10 | 3.72 |3.08 |11.6 |5.61 |4.72 | 3 |E< 25| 300 | 3 |E < 1 |-.538 | | |
09/16/10 | 3.67 |3.13 | 9.9 |5.59 |4.66 | | | 310 U| | |-.541 | | |
24-000111-2 JOSEPH &OR CHRISTI(O)09/02/10 | 3.52 |2.92 |17.0 |5.56 |4.62 | | | 260 | | |-.526 U| | |
09/08/10 | | | | | | 2 |E< 25| 150 | 4 |E < 1 | | | NF |38
09/12/10 | 3.35 |3.07 |10.1 |5.57 |4.73 | | | 160 | | |-.543 | | |
24-000113-1 RONNIE &OR CAROL SWE 09/12/10 | 4.33 |3.42 | |5.59 | | 63 U| 63| 610 U|1600 U| 18 U|-.546 | | NF |39
24-000567-1 WIMBLE BROTHERS 09/12/10 | 4.80 |3.26 | |5.50 | | 1 |E< 25| 180 | 3 | 2 |-.553 | | NF |39
24-000585-1 WARREN RANKIN 09/12/10 | 4.42 |3.10 | |5.67 | | 4 |E< 25| 130 | 1 | 1 |-.548 | | NF |39


Report •

#11
October 20, 2010 at 08:01:07
I can send you the text file, if that would make it easier for you.

Thanks

Don


Report •

#12
October 21, 2010 at 16:22:36
So those lines are no good; there's no clear path from what you posted earlier and those source lines.

But you said the lines you posted earlier have tabs, but no data, and that's usable. I'll see if I can't whip something up, but it'll probably have to wait until tomorrow; I just don't have time today.

How To Ask Questions The Smart Way


Report •

#13
October 21, 2010 at 16:45:53
No problem, unfortunately I haven't had any luck getting the original output file changed at the source. So this is what I have to work with.

Thank you for all of your help.

Don


Report •

#14
October 28, 2010 at 07:40:43
So yeah, I saw something shiny and wondered off.
Here's a VBScript:
Const inName = "in.tsv"
Const outName = "out.tsv"
WScript.Quit Main

Function FindSecondTab(sLine) 'As Integer
  FindSecondTab = InStr(sLine, vbTab)
  FindSecondTab = InStr(FindSecondTab + 1, sLine, vbTab)
End Function

Function Main
  With CreateObject("Scripting.FileSystemObject")
    Set inFile = .OpenTextFile(inName)
    Set outFile = .OpenTextFile(outName, 2, True)
  End With
  Do Until inFile.AtEndOfStream
    inLine = inFile.ReadLine
    tab = FindSecondTab(inLine)
    If (Len(inLine) > 2) Then
      If (tab > 2) Then _
        lineStart = Left(inLine, tab)
      outFile.WriteLine lineStart & Mid(inLine, tab + 1)
    Else 'Line is probably empty
      outFile.WriteLine inLine
    End If
  Loop
End Function

How To Ask Questions The Smart Way


Report •

#15
October 28, 2010 at 07:44:47
Hey thank you, I'll let you know how it goes.

Have a great weekend.

Don


Report •

#16
October 29, 2010 at 11:37:48
Hi I tried it and it doesn't do what I need it to do.

Here is the original file, it is tab delimited with a single tab between.

50 1 10/12/2010 4.18
10/14/2010 4.18
10/18/2010 4.41
111 1 10/2/2010 3.91
10/4/2010 3.15
10/6/2010 3.18
10/8/2010 3.13
10/10/2010 3.88
this is what it should look like after

50 1 10/12/2010 4.18
50 1 10/14/2010 4.18
50 1 10/18/2010 4.41
111 1 10/2/2010 3.91
111 1 10/4/2010 3.86
111 1 10/6/2010 3.79
111 1 10/8/2010 3.61
111 1 10/10/2010 3.88

Thanks

Don


Report •

#17
October 29, 2010 at 11:50:56
Given the input you just gave, I ran the script and got the output you say you need.

How To Ask Questions The Smart Way


Report •

#18
October 29, 2010 at 12:12:12
oops!!! Very sorry, I used the wrong file. When I run it against the correct file it does work.

Thank you

Don


Report •


Ask Question