Solved

Remove Characters from TXT file using Visual Basic

Posted on 2013-01-31
15
453 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Determine what computer(s) is/are running a script file 3 33
delete a folder ever 5 minutes 6 60
VBA code shorten run time 4 55
Running Visio Macro from VBS File 3 59
Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

19 Experts available now in Live!

Get 1:1 Help Now