Solved

Remove Characters from TXT file using Visual Basic

Posted on 2013-01-31
15
455 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 42

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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

856 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