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

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
dondudleyAsked:
Who is Participating?
 
jawa29Connect With a Mentor Commented:
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
 
Bill PrewConnect With a Mentor Commented:
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
 
dondudleyAuthor Commented:
Your are great, worked first time and I have been working on this for at least two hours.

Thanks again.

Don
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
dondudleyAuthor Commented:
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
 
dondudleyAuthor Commented:
Why is there 0 points?  I think this was a great job and very fast response.....
0
 
Bill PrewCommented:
Question needs attention as I think a solution was intended to be accepted.

~bp
0
 
dondudleyAuthor Commented:
Great Work.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.