Regex 如何在VBScript中将双引号替换为空格?
我需要打开Regex 如何在VBScript中将双引号替换为空格?,regex,replace,vbscript,Regex,Replace,Vbscript,我需要打开CSV文件,将双引号替换为空格,并将新文件保存为txt扩展名 我尝试过这个VBScriptcode,但是第一个文件Output\u D1.txt是空的,Output\u D2.txt文件包含Output\u D1.csv的行,Output\u D3.txt文件包含Output\u D2.csv的行。。。等等 如何解决这个问题 nArr = Array("D1", "D2", "D3", "D4", "D5", "D6") Set reP = new RegExp
CSV文件
,将双引号替换为空格,并将新文件保存为txt扩展名
我尝试过这个VBScript
code,但是第一个文件Output\u D1.txt
是空的,Output\u D2.txt
文件包含Output\u D1.csv
的行,Output\u D3.txt
文件包含Output\u 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替换之前使用!)
WSH.Echo(Replace(strg),“,”)
只需使用普通的Replace()
而不是RegExp.Replace()
。@scientist\u 7我也有同样的问题,谢谢---@LankymartI也有同样的问题,谢谢---
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