如何在 VBScript 中将双引号替换为 space?

How to replace double quotes to a space in VBScript?

我需要打开 CSV file,用 space 替换双引号并用 txt extension.

保存新文件

我试过这个 VBScript 代码,但是第一个文件 Output_D1.txt 是空的,Output_D2.txt 文件包含 Output_D1.csvOutput_D3.txt 的行文件包含 Output_D2.csv...等行

如何解决这个问题?

   nArr = Array("D1", "D2", "D3", "D4", "D5", "D6")   

   Set reP = new RegExp  
   reP.Pattern = "\"""     

   For I = 0 To UBound(nArr) 
      InFilename = "Output_" & nArr(I) & ".csv"
      Set FILE1 = CreateObject("scripting.FileSystemObject")
      Set infile = FILE1.OpenTextFile(InFileName, 1, False)   
      strg = reP.Replace(strg, " ")
      InFilenameNew = "Output_" & nArr(I) & ".txt"
      Set Outfile = File1.CreateTextFile(inFileNameNew, 1, False)
      Outfile.Write(strg) 
      strg = infile.ReadAll
      infile.Close  
   Next 

试试这个:

  • 您需要设置 RegExp 全局选项来替换所有出现的引号
  • 您需要 CSV 文件的完整路径(有关硬编码示例,请参见 rootFolder)
  • ReadAll用错了地方(需要在RegExp替换之前!)

    Option Explicit 
    
    dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
    dim rootFolder : rootFolder = "C:\Temp\"
    dim nArr : nArr = Array("D1", "D2", "D3", "D4", "D5", "D6")   
    dim i, inFilename, inFilenameNew, infile, outfile, filecontent
    Const ForReading = 1, ForWriting = 2
    
    dim reP : Set reP = new RegExp 
    reP.Global = true
    reP.Pattern = "\"""     
    
    For i = 0 To UBound(nArr) 
        inFilename = rootFolder & "Output_" & nArr(i) & ".csv"
        inFilenameNew = rootFolder & "Output_" & nArr(i) & ".txt"   
    
        if (fso.FileExists(inFilename)) Then
            Set infile = fso.OpenTextFile(InFileName, ForReading) 
            filecontent = infile.ReadAll
            filecontent = reP.Replace(filecontent, " ")
            infile.Close
            Set infile = Nothing
    
            Set outfile = fso.CreateTextFile(inFileNameNew, True)
            outfile.Write(filecontent)
            outfile.Close  
            Set outfile = Nothing
        End If          
    Next 
    
    Set reP = Nothing
    Set fso = Nothing