Solved

Remove Characters from TXT file using Visual Basic

Posted on 2013-01-31
15
452 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 41

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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now