• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 461
  • Last Modified:

Remove Characters from TXT file using Visual Basic

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
woodsboro_kid
Asked:
woodsboro_kid
  • 7
  • 7
1 Solution
 
CluskittCommented:
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
 
AmitIT ArchitectCommented:
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
 
woodsboro_kidAuthor Commented:
I'm sorry. I need to do this programmatically as there is a file for each state.
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
CluskittCommented:
If VB doesn't like Mid, use Microsoft.VisualBasic.Mid
0
 
woodsboro_kidAuthor Commented:
How do I put it in a VB script. I am total novice.
0
 
woodsboro_kidAuthor Commented:
I get an "Expected end of statement" Error, Line 1 Char 13
0
 
CluskittCommented:
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
 
woodsboro_kidAuthor Commented:
My apologies. I suck at this stuff.
0
 
CluskittCommented:
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
 
woodsboro_kidAuthor Commented:
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
 
CluskittCommented:
Sorry, my bad... You have to add this line (place it on top):
Const ForReading = 1, ForWriting = 2, ForAppending = 8
0
 
woodsboro_kidAuthor Commented:
Getting closer. I get Object doesn't support this property or method: 'WriteLine'

This is on Line 7, Char 5.
0
 
CluskittCommented:
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
 
woodsboro_kidAuthor Commented:
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
 
CluskittCommented:
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now