替换 .text 文件中的特定字符
Replacing a specific charcater in a .txt file
我的 .txt 文件显示为“203,13,58,2018,0”,我需要做的就是将末尾的 0 替换为 1。
我发现了几个变体:
我在这里有代码,但该网站不让我 post 说格式不对,我很沮丧,这里有我一直在尝试使用的代码的网站需要一些帮助. (https://ss64.com/vb/syntax-replace.html)
但他们都删除了其他所有内容,只留下“,1”。我错过了什么?
使用相同的代码,但进行了改编:
Option Explicit
Dim fso,strFilename,strSearch,strReplace,objFile,oldContent,newContent
strFilename="D:\demo.txt"
strSearch=",0"
strReplace=",1"
'Does file exist?
Set fso=CreateObject("Scripting.FileSystemObject")
if fso.FileExists(strFilename)=false then
wscript.echo "file not found!"
wscript.Quit
end if
'Read file
set objFile=fso.OpenTextFile(strFilename,1)
oldContent=objFile.ReadAll
'Write file
newContent=replace(oldContent,strSearch,strReplace,1,-1,0)
set objFile=fso.OpenTextFile(strFilename,2)
objFile.Write newContent
objFile.Close
我的 .txt 文件显示为“203,13,58,2018,0”,我需要做的就是将末尾的 0 替换为 1。
我发现了几个变体:
我在这里有代码,但该网站不让我 post 说格式不对,我很沮丧,这里有我一直在尝试使用的代码的网站需要一些帮助. (https://ss64.com/vb/syntax-replace.html)
但他们都删除了其他所有内容,只留下“,1”。我错过了什么?
使用相同的代码,但进行了改编:
Option Explicit
Dim fso,strFilename,strSearch,strReplace,objFile,oldContent,newContent
strFilename="D:\demo.txt"
strSearch=",0"
strReplace=",1"
'Does file exist?
Set fso=CreateObject("Scripting.FileSystemObject")
if fso.FileExists(strFilename)=false then
wscript.echo "file not found!"
wscript.Quit
end if
'Read file
set objFile=fso.OpenTextFile(strFilename,1)
oldContent=objFile.ReadAll
'Write file
newContent=replace(oldContent,strSearch,strReplace,1,-1,0)
set objFile=fso.OpenTextFile(strFilename,2)
objFile.Write newContent
objFile.Close