Solved

Remove Characters from TXT file using Visual Basic

Posted on 2013-01-31
15
456 Views
Last Modified: 2013-01-31
I have a text file that I would like to remove the first 9 characters (Zip+4). (See attached Sample) Then save the file without the characters. I would like to do this using Visual Basic. Any help with code would be appreciated.

Thanks!
Sample.txt
0
Comment
Question by:woodsboro_kid
  • 7
  • 7
15 Comments
 
LVL 18

Expert Comment

by:Cluskitt
ID: 38839568
Dim oReader As New IO.StreamReader("C:\Sample.txt")
Dim oWriter As New IO.StreamWriter("C:\Fixed.txt")
While oReader.Peek <> -1
  oWriter.WriteLine(Mid(oReader.ReadLine, 10, 500)
End While
oReader.Close()
oWriter.Close()

Something like this, anyway.
0
 
LVL 43

Expert Comment

by:Amit
ID: 38839569
How about doing it without any script, does that work for you. If so, copy paste this text into a Winword. Now press the Alt Key + Mouse key. Drag and select area you want to remove. Once selected hit delete button.
0
 

Author Comment

by:woodsboro_kid
ID: 38839581
I'm sorry. I need to do this programmatically as there is a file for each state.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:Cluskitt
ID: 38839596
If VB doesn't like Mid, use Microsoft.VisualBasic.Mid
0
 

Author Comment

by:woodsboro_kid
ID: 38839610
How do I put it in a VB script. I am total novice.
0
 

Author Comment

by:woodsboro_kid
ID: 38839636
I get an "Expected end of statement" Error, Line 1 Char 13
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 38839646
This is for a VB app. Your subject said VB, and so did the tags. I'll try to get something to work for VB script.
0
 

Author Comment

by:woodsboro_kid
ID: 38839655
My apologies. I suck at this stuff.
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 38839675
Try This:

<%
Dim fso, inFile, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set InFile = fso.OpenTextFile("c:\Sample.txt", ForReading)
Set OutFile = fso.OpenTextFile("c:\Fixed.txt", ForWriting, True)
Do While InFile.AtEndOfStream <> True
    OutFile.WriteLine = Mid(InFile.ReadLine, 10, 500)
Loop
InFile.Close
OutFile.Close
%>
0
 

Author Comment

by:woodsboro_kid
ID: 38839700
Getting Invalid procedure call or argument Line 3 Char 1. Note: I removed <% (and %> at the end.) I am just trying to run a simple vbs file.
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 38839729
Sorry, my bad... You have to add this line (place it on top):
Const ForReading = 1, ForWriting = 2, ForAppending = 8
0
 

Author Comment

by:woodsboro_kid
ID: 38839782
Getting closer. I get Object doesn't support this property or method: 'WriteLine'

This is on Line 7, Char 5.
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 38839810
Again, my bad... sorry, but I was just trying to adapt it to the original code and there were some typos...

OutFile.WriteLine Mid(InFile.ReadLine, 10, 500)

(there was an = too many). It should work fine now. I tested it on this end and it's working right.
0
 

Author Comment

by:woodsboro_kid
ID: 38839824
WOOOHOOOO! That did it! Awesome. Do you want to repost the whole vbs code or should I assign points (solution) to the last post?
0
 
LVL 18

Accepted Solution

by:
Cluskitt earned 500 total points
ID: 38839851
I suppose it would be better to have the whole thing together.

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, inFile, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set InFile = fso.OpenTextFile("c:\Sample.txt", ForReading)
Set OutFile = fso.OpenTextFile("c:\Fixed.txt", ForWriting, True)
Do While InFile.AtEndOfStream <> True
    OutFile.WriteLine Mid(InFile.ReadLine, 10, 500)
Loop
InFile.Close
OutFile.Close
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question