0

I have a VBScript program coded and it creates a html page. Using the WriteLine command, I was wondering if I could copy the code of a file "default.html" (in the same directory) and insert it into my WriteLine command.

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!"

Thanks

1 Answers1

0

Try the code below:

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

Note if you need to open file e. g. in UTF-8, you have to use another tools like ADODB.Stream

Community
  • 1
  • 1
omegastripes
  • 12,351
  • 4
  • 45
  • 96