想要检查文本文件中的年份是否为实际年份,如果不是实际年份则更新,2013年。 例如,文本行是:SET YEAR = 2012 到目前为止,我只能将实际年份写入文件,但现在我需要更新文件中的信息(如果存在)... 在这里感谢任何帮助。
dim filesys
Set filesys = CreateObject("Scripting.FileSystemObject")
path = filesys.GetAbsolutePathName("C:\Backups\Machine.bat")
getname = filesys.GetFileName(path)
If filesys.FileExists(path) Then
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set filesys = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFile("C:\Backups\Machine.bat")
Set objFile = objFSO.OpenTextFile("C:\Backups\Machine.bat", 8)
objFile.WriteLine
objFile.Write("SET YEAR=")
objFile.WriteLine(Year(Date()))
objFile.Close
End if
答案 0 :(得分:0)
您可以替换文本文件中的特定行,如下所示:
path = "C:\Backups\Machine.bat"
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(path) Then
Set inFile = fso.OpenTextFile(path)
Set outFile = fso.OpenTextFile(path & ".tmp", 2, True)
Do Until inFile.AtEndOfStream
line = inFile.ReadLine
If UCase(Left(Trim(line), 9)) = "SET YEAR=" Then
outFile.WriteLine "SET YEAR=" & Year(Date)
Else
outFile.WriteLine line
End If
Loop
inFile.Close
outFile.Close
fso.DeleteFile path, True
fso.MoveFile path & ".tmp", path
End If