如何在文本中插入逗号? [英] How do I insert commas into text?
问题描述
我必须在文本行的某些点使用VBScript插入逗号。我需要它检查每行的前四个字符与一个if语句,如果它匹配插入该行所需的逗号这是我到目前为止:
Const ForReading = 1
Const ForWriting = 2
设置objFSO = CreateObject(Scripting.FileSystemObject)
设置objFile = objFSO。 OpenTextFile(H:\Letter Display\Letters\LTRPRT__00000008720000000001NI-K-RMND.txt,ForReading)
strNIK =1000
strLine = objFile.ReadLine
如果Left(strLine,4)= strNIK,则
arrCommas = Array(16,31,46,56,66,79,94,99)
Do Until objFile.AtEndOfStream
intLength = Len(strLine)
对于arrCommas中的每个strComma
strLine = Left(strLine,strComma - 1)+,+ Mid strLine,strComma,intLength)
Next
strText = strText& strLine& vbCrLf
Loop
end if
objFile.Close
设置objFile = objFSO.OpenTextFile(H:\Letter Display\\ \\Letters\LTRPRT__00000008720000000001NI-K-RMND.txt,ForWriting)
objFile.Write strText
objFile.Close
$ b
如果任何人可以帮助制作这个IF语句,您需要移动 ReadLine
和 Do..Loop
中的条件:
strNIK =1000
arrCommas = Array(16,31,46,56,66,79,94,99)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
如果Left(strLine,4)= strNIK,则
intLength = Len(strLine)
每个strComma在arrCommas
strLine = Left(strLine,strComma - 1)+,_
+ Mid(strLine,strComma,intLength)
Next
End If
strText = strText& strLine& vbCrLf
循环
如果希望输出仅包含修改的线,行 strText = strText& strLine& vbCrLf
如果Left(strLine,4)= strNIK,则
strText = strText& strLine& vbCrLf
结束如果
在数组中的逗号索引已经占据位置移位是由字符插入引起的?
此外,最好将输出行写入临时文件,然后用临时文件替换输入文件文件:
设置inFile = objFSO.OpenTextFile(inputFilename,ForReading)
Set outFile = objFSO.OpenTextFile(outputFilename,ForWriting)
Do Until inFile.AtEndOfStream
strLine = inFile.ReadLine
'使用strLine做事情
outFile.WritLine
循环
inFile.Close
outFile.Close
objFSO.DeleteFile inputFilename,True
objFSO.MoveFile outputFilename,inputFilename
这样可以避免处理大文件时的内存耗尽。
可以像这样处理给定目录中具有特定扩展名的所有文件:
folderName =C:\some\folder
For objFile In objFSO.GetFolder(folderName).Files
如果LCase(objFSO.GetExtensionName(objFile.Name))=ltrThen
' here
End If
Next
如果要使用inputfile / outputfile方法我上面建议,您可以为每个输入文件使用相同的临时输出文件名称,或者您可以从输入文件名称导出输出文件名称,例如像这样:
inputFilename = objFile.Name
outputFilename = inputFilename& .tmp
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.
解决方案You need to move the
ReadLine
and the conditional inside theDo..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"
这篇关于如何在文本中插入逗号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!