Csv 如何在文本中插入逗号?

Csv 如何在文本中插入逗号?,csv,vbscript,Csv,Vbscript,我必须使用VBScript在文本行中的某些点插入逗号。我需要它用if语句检查每一行的前四个字符,如果它匹配,则插入该行所需的逗号。这是我到目前为止所拥有的: Const ForReading = 1 Const ForWriting = 2 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile("H:\Letter Display\Letters\LTRPRT__000

我必须使用VBScript在文本行中的某些点插入逗号。我需要它用if语句检查每一行的前四个字符,如果它匹配,则插入该行所需的逗号。这是我到目前为止所拥有的:

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语句,我们将不胜感激。

你需要在
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
如果希望输出仅由修改的行组成,请将行
strText=strText&strLine&vbCrLf
移动到条件行中:

If Left(strLine, 4) = strNIK then
  '...
  strText = strText & strLine & vbCrLf
End If
数组中的逗号索引是否已经考虑了由字符插入引起的位置偏移

此外,最好将输出逐行写入临时文件,然后在处理完所有输入后用该临时文件替换输入文件:

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
这样可以避免在处理大型文件时内存耗尽

您可以处理给定目录中具有特定扩展名的所有文件,如下所示:

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
inputFilename  = objFile.Name
outputFilename = inputFilename & ".tmp"
如果要使用我上面建议的inputfile/outputfile方法,可以为每个输入文件使用相同的临时outputfile名称,也可以从inputfile名称派生outputfile名称,例如:

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
inputFilename  = objFile.Name
outputFilename = inputFilename & ".tmp"

您需要在
Do..Loop
中移动
ReadLine
和条件:

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
如果希望输出仅由修改的行组成,请将行
strText=strText&strLine&vbCrLf
移动到条件行中:

If Left(strLine, 4) = strNIK then
  '...
  strText = strText & strLine & vbCrLf
End If
数组中的逗号索引是否已经考虑了由字符插入引起的位置偏移

此外,最好将输出逐行写入临时文件,然后在处理完所有输入后用该临时文件替换输入文件:

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
这样可以避免在处理大型文件时内存耗尽

您可以处理给定目录中具有特定扩展名的所有文件,如下所示:

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
inputFilename  = objFile.Name
outputFilename = inputFilename & ".tmp"
如果要使用我上面建议的inputfile/outputfile方法,可以为每个输入文件使用相同的临时outputfile名称,也可以从inputfile名称派生outputfile名称,例如:

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
inputFilename  = objFile.Name
outputFilename = inputFilename & ".tmp"

非常感谢,你能告诉我如何对一个目录中所有扩展名都相同的文件执行此操作吗?非常感谢,你能告诉我如何对一个目录中所有扩展名都相同的文件执行此操作吗?