Nathan Hawthorne
Nathan Hawthorne

Reputation: 303

How do I insert commas into text?

I have to insert commas at certain points in lines of text, using VBScript. I need it to check the first four charcters of each line with an if statement and if it matches it inserts the commas required for that line this is what I have so far:

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("H:\Letter Display\Letters\LTRPRT__00000008720000000001NI-K-RMND.txt", ForReading)

strNIK = "1000"
strLine = objFile.ReadLine

If Left(strLine,4) = strNIK then

  arrCommas = Array(16,31,46,56,66,79,94,99)

  Do Until objFile.AtEndOfStream

    intLength = Len(strLine)
    For Each strComma in arrCommas
      strLine = Left(strLine, strComma - 1) + "," + Mid(strLine, strComma, intLength)
    Next
    strText = strText & strLine & vbCrLf
  Loop

end if

objFile.Close

Set objFile = objFSO.OpenTextFile("H:\Letter Display\Letters\LTRPRT__00000008720000000001NI-K-RMND.txt", ForWriting)
objFile.Write strText
objFile.Close

If anyone could help with making this an IF statement it would be much appreciated.

Upvotes: 1

Views: 2422

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200213

You need to move the ReadLine and the conditional inside the Do..Loop:

strNIK = "1000"
arrCommas = Array(16,31,46,56,66,79,94,99)

Do Until objFile.AtEndOfStream
  strLine = objFile.ReadLine

  If Left(strLine, 4) = strNIK then
    intLength = Len(strLine)
    For Each strComma in arrCommas
      strLine = Left(strLine, strComma - 1) + "," _
        + Mid(strLine, strComma, intLength)
    Next
  End If

  strText = strText & strLine & vbCrLf
Loop

If you want the output to consist only of the modified lines, move the line strText = strText & strLine & vbCrLf inside the conditional:

If Left(strLine, 4) = strNIK then
  '...
  strText = strText & strLine & vbCrLf
End If

Do the comma indexes in your array already account for the positional shift that is caused by the character insertion?

Also, it might be a good idea to write the output line by line to a temporary file and then replace the input file with that temporary file after you finished processing all input:

Set inFile  = objFSO.OpenTextFile(inputFilename, ForReading)
Set outFile = objFSO.OpenTextFile(outputFilename, ForWriting)

Do Until inFile.AtEndOfStream
  strLine = inFile.ReadLine
  'do stuff with strLine
  outFile.WritLine
Loop

inFile.Close
outFile.Close

objFSO.DeleteFile inputFilename, True
objFSO.MoveFile outputFilename, inputFilename

That way you can avoid memory exhaustion when processing large files.

You can process all files with a specific extension in a given directory like this:

folderName = "C:\some\folder"

For Each objFile In objFSO.GetFolder(folderName).Files
  If LCase(objFSO.GetExtensionName(objFile.Name)) = "ltr" Then
    'processing takes place here
  End If
Next

If you want to use the inputfile/outputfile approach I suggested above, you can either use the same temporary outputfile name for each input file, or you can derive the outputfile name from the inputfile name, e.g. like this:

inputFilename  = objFile.Name
outputFilename = inputFilename & ".tmp"

Upvotes: 1

Related Questions