Home > VBScript > How to write to a .txt file with VBScript

How to write to a .txt file with VBScript


‘**********************************************
‘Set Variables
‘**********************************************
Dim Wshshell, objFSO, objNetwork, objShell, strDirectory, strTemp, strFileName, objTextFile, objFolder
‘Constants used in opening files.
Const ForReading = 1, ForWriting = 2, ForAppending = 8
StrTemp = "C:Temp"
Set WshShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("Wscript.Network")
Set objShell = CreateObject("WScript.Shell")
‘**********************************************
‘Create logs & Temp folders if they do not exist
‘**********************************************
If objFSO.FolderExists (strTemp) Then
 ‘WScript.Echo "No need to create " & strTemp
Else
 Set objFolder = objFSO.CreateFolder(strTemp)
 ‘WScript.Echo "Created " & strTemp
End If
strDirectory = "c:Logs"
strFileName = "c:Logstest.txt"
If objFSO.FolderExists (strDirectory) Then
 ‘WScript.Echo "No need to create " & strDirectory
Else
 Set objFolder = objFSO.CreateFolder(strDirectory)
 ‘WScript.Echo "Created " & strDirectory
End If
‘**********************************************
‘Write Script Execution Time
‘**********************************************
Set objTextFile = objFSO.OpenTextFile (strFileName, ForAppending, True)
 objTextFile.WriteLine
 objTextFile.Write ("Admin script ran at ")
 objTextFile.WriteLine Now
objTextFile.Close
wscript.quit
Advertisements
Categories: VBScript
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: