VBScript-复制html页面的内容并插入到新创建的html页面中



我编写了一个VBScript程序,它创建了一个html页面。使用WriteLine命令,我想知道是否可以复制文件"default.html"(在同一目录中)的代码,并将其插入我的WriteLine命令中。

Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile(Trim(Username) & Trim(Password) & ".html",2,True)
f.WriteLine("<!-- """ & Username & """-->")
f.WriteLine("          ")
Set fs = Nothing
MsgBox "Webpage created!"

感谢

尝试以下代码:

Const FsoTristateUseDefault = -2 ' Opens the file using the system default.
Const FsoTristateTrue = -1 ' Opens the file as Unicode.
Const FsoTristateFalse = 0 ' Opens the file as ASCII.
' some operations
sUsername = "username"
sPassword = "12345"
' set path to default.html
sScriptFolder = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName) & ""
sDefaultPath = sScriptFolder & "default.html"
' read default
sDefaultContent = ReadTextFile(sDefaultPath, FsoTristateUseDefault)
' processing some data to get result
sResult = ""
sResult = sResult & "<!-- """ & Username & """-->" & vbCrLf
sResult = sResult & "          " & vbCrLf
sResult = sResult & sDefaultContent
' write result to new file, as Unicode
WriteTextFile sResult, Trim(sUsername) & Trim(sPassword) & ".html", FsoTristateTrue
Function ReadTextFile(sPath, iFormat)
    With CreateObject("Scripting.FileSystemObject").OpenTextFile(sPath, 1, False, iFormat)
        ReadTextFile = ""
        If Not .AtEndOfStream Then ReadTextFile = .ReadAll
        .Close
    End With
End Function
Sub WriteTextFile(sCont, sPath, iFormat)
    With CreateObject("Scripting.FileSystemObject").OpenTextFile(sPath, 2, True, iFormat)
        .Write(sCont)
        .Close
    End With
End Sub

注意,如果您需要打开文件,例如UTF-8,您必须使用其他工具,如ADODB.Stream

最新更新