如何在VBScript中动态更新行?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在VBScript中动态更新行?相关的知识,希望对你有一定的参考价值。
我正在写一个脚本,我想知道如何读取txt文件并找到该行然后替换它。
例如,如果我有一个名为ImportParms.txt的txt文件,其中包含一些参数行,如下所示:
cmdFile=E:JobsUPCSParmsImportParms.upcs_cmd Process=StandardImport LogFile=e:JOBSUPCSLogsImportLog.txt File=E:jobsUPCSTestImportFile.txt ImportRejects=No RenameToOld=No RenameDateStamp=No ResultsReport="Public ReportsImportResult" LinkExisting=Custom1
我想更新每次运行时说File=something.txt
的行。所以,而不是
sCmd = sBotCmd & " file=""" & sUtilOut &""" "
它会说
sCmd = sBotCmd
从运行exe的代码部分看起来像这样:
'Run processbot to update
Set oFile = oFSO.GetFile(sUtilOut)
If oFile.Size <> 0 Then
strFile = sWORK & "parms" & BOTPARMFILE
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
'If first for characters = "file"
'delete this line
strFile.Write "file=" & sUtilOut
'End If
Loop
objFile.Close
sBotCmd = """"& sWORK & "" & BOTPROG & """ " _
& " cmdFile=""" & sWORK & "parms" & BOTPARMFILE & """ "
sCmd = sBotCmd
Call OutMsg(isVerbose, isVerbose, sSysLog, "Command: " & sCmd)
iRetC = oShell.Run(sCmd, 0, True)
Call OutMsg(isVerbose, isVerbose, sSysLog, "RC: " & iRetC)
If iRetC <> 0 Then
Call OutMsg(isVerbose, isVerbose, sSysLog, "Exiting with code " _
& rcPROCBOT)
WScript.Quit(rcPROCBOT)
End If
End If
答案
您无法就地修改文件。由于您的文件很小,只需阅读其全部内容,进行修改,然后将其写回。
替换这个:
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
'If first for characters = "file"
'delete this line
strFile.Write "file=" & sUtilOut
'End If
Loop
objFile.Close
有了这个:
txt = objFS.OpenTextFile(strFile).ReadAll
Set re = New RegExp
re.Pattern = "^(File=)[^
]*"
re.MultiLine = True
fso.OpenTextFile(strFile, 2).Write re.Replace(txt, "$1" & sUtilOut)
以上是关于如何在VBScript中动态更新行?的主要内容,如果未能解决你的问题,请参考以下文章