?
Solved

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

Posted on 2011-10-26
7
Medium Priority
?
216 Views
Last Modified: 2012-05-12
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
Comment
Question by:dondudley
  • 4
  • 2
7 Comments
 
LVL 59

Assisted Solution

by:Bill Prew
Bill Prew earned 1000 total points
ID: 37032012
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 ForReading = 1
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)
arrLines = Split(objFile.ReadAll, VbCrLf)
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

Open in new window

~bp
0
 
LVL 8

Accepted Solution

by:
jawa29 earned 1000 total points
ID: 37032033
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

Open in new window

     
Jawa29
0
 

Author Comment

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

Thanks again.

Don
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:dondudley
ID: 37033698
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

by:dondudley
ID: 37032901
Why is there 0 points?  I think this was a great job and very fast response.....
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37033699
Question needs attention as I think a solution was intended to be accepted.

~bp
0
 

Author Closing Comment

by:dondudley
ID: 37033887
Great Work.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

807 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