Solved

# How do I VBS code to add text to end of each line in text file

Posted on 2011-10-26
208 Views
I have a txt file that I need to add the department number to the end of each line of text like the following you will see I added the 12 in manually.  There are sometimes hundreds of lines and I have to add the 12 to the end of each line.

NEW      DESTINATION I D      01596 24 WIN12      3.61      4.95      QU      009281015963      10/26/2011      12
NEW      WOODEN BOAT          03402 12 N/D11      4.56      6.25      BM      009281034025      10/26/2011      12

Thanks,
Don
0
Question by:dondudley

LVL 51

Assisted Solution

Here's a basic shell that you can use and adjust as needed.  Save as a VBS file, and run passing either one or two filenames to it on the command line.  If one name passed it overwrites that file with the changes.  If two file names passed, it reads the first, and saves the changed version to the 2nd file name.

' Define needed constants
Const ForWriting = 2
Const TriStateUseDefault = -2

' Get input file name from command line parm, if 2 parms entered
' use second as new output file, else rewrite to input file
If (WScript.Arguments.Count > 0) Then
strInFile = WScript.Arguments(0)
Else
WScript.Echo "No filename specified."
WScript.Quit
End If
If (WScript.Arguments.Count > 1) Then
strOutFile = WScript.Arguments(1)
Else
strOutFile = strInFile
End If

' Read file into an array
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strInFile, ForReading, False, TriStateUseDefault)
objFile.Close

' Loop through all lines of the file, append " 12" to the end
For i = 0 To UBound(arrLines)
If arrLines(i) <> "" Then
arrLines(i) = arrLines(i) & " 12"
End If
Next

' Rewrite file with any changes made
Set objFile = objFSO.OpenTextFile(strOutFile, ForWriting, True)
objFile.Write(Join(arrLines, vbCrLf))
objFile.Close

~bp
0

LVL 8

Accepted Solution

Here's another example of how to do it

Const ForReading = 1
Const ForWriting = 2

On Error Resume Next

sTxtFileToCheck = "C:\Scripts\text.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTxt = objFSO.OpenTextFile(sTxtFileToCheck, ForReading, True)

Do While Not objTxt.AtEndOfStream

sTxt = sTxt & objTxt.ReadLine & "	12" & vbCrLf

Loop

objTxt.Close

Set objTxt = objFSO.OpenTextFile(sTxtFileToCheck, ForWriting, True)
objTxt.WriteLine(sTxt)
objTxt.Close


Jawa29
0

Author Comment

Your are great, worked first time and I have been working on this for at least two hours.

Thanks again.

Don
0

Author Comment

I've requested that this question be closed as follows:

Accepted answer: 0 points for dondudley's comment http:/Q_27416534.html#37032705

for the following reason:

I just had to copy the code into my vbs and it worked first time. &nbsp;I had been working on this for two hours and it only took him minutes.
0

Author Comment

Why is there 0 points?  I think this was a great job and very fast response.....
0

LVL 51

Expert Comment

Question needs attention as I think a solution was intended to be accepted.

~bp
0

Author Closing Comment

Great Work.
0

## Featured Post

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …