VBScript Basics, Part 22 | Append Files (OpenTextFile)

Опубликовано: 29 Сентябрь 2024
на канале: SimplyCoded
15,537
147

VBScripting (.vbs) Basic tutorial on how to use the OpenTextFile command to edit text files (.txt .doc .rtf .bat .vbs etc...)

Code Example:
______________________________________________________

Option Explicit
Dim fso, objWRITE, objREAD, CD, current, create
Const vbRead=1, vbAppend=8
Set fso = CreateObject("Scripting.FileSystemObject")

CD = Replace(Wscript.ScriptFullName, Wscript.ScriptName, "new.doc")

'-----------------------------
'SETUP PROCESS
'-----------------------------

Sub Refresh
Set objREAD = fso.OpenTextFile(CD,vbRead)
current=objREAD.ReadAll
objREAD.Close
End Sub

Sub Main
Do
create=InputBox(current ,"Text Editor: [*] to quit")
If create = "*" then
wscript.quit
ElseIf create = "" then
Set objWRITE = fso.OpenTextFile(CD,vbAppend,1)
objWRITE.writeblanklines(1)
objWRITE.Close
Call Refresh
Else
Set objWRITE = fso.OpenTextFile(CD,vbAppend,1)
objWRITE.write create
objWRITE.Close
Call Refresh
End If
Loop
End Sub

'---------------------------------
'WRITTING PROCESS
'---------------------------------

If Not fso.FileExists(CD) then
Call Main
Else
If fso.GetFile(CD).Size = 0 Then
Call Main
Else
Call Refresh
Call Main
End If
End If
Wscript.Quit
______________________________________________________

Know the Basics:
------------------------------------------------------------------------------------------
Set objFso = CreateObject("Scripting.FileSystemObject")
Set x = objFso.OpenTextFile("FILE-LOCATION", 8)

x.Write "" = add directly onto the end of your file.
x.WriteLine "" = add directly onto the end of your file, followed by a line.
x.WriteBlankLines(#) = add blank lines to the end of your file.
------------------------------------------------------------------------------------------