Option Explicit
Dim objFSO, objFolder, objShell, objTextFile, objFile
Dim strDirectory, strFile, strText, oNet, sUserName, sRoom, sComputer
Set oNet = CreateObject("WScript.Network")
'variables
sUserName = oNet.UserName
sComputer = oNet.ComputerName
'sRoom = Left(sComputer,3)
strDirectory = "C:\TEMP\"
strFile = sUserName & ".log"
strText = "login: " & Now & Chr(13) & Chr(10) & _
"User: " & sUserName & Chr(13) & Chr(10) & _
"Computer: " & sComputer & Chr(13) & Chr(10) & _
"Lokaal: " & sRoom & Chr(13) & Chr(10) & _
""
' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Check that the strDirectory folder exists
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
'WScript.Echo "Just created " & strDirectory
End If
If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Set objFile = nothing
set objFolder = nothing
' OpenTextFile Method needs a Const value
' ForAppending = 8 ForReading = 1, ForWriting = 2
Const ForAppending = 8
Set objTextFile = objFSO.OpenTextFile _
(strDirectory & strFile, ForAppending, true)
' Writes strText every time you run this VBScript
objTextFile.WriteLine(strText)
objTextFile.Close
Else
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
Set objFile = nothing
set objFolder = nothing
' OpenTextFile Method needs a Const value
' ForAppending = 8 ForReading = 1, ForWriting = 2
Const ForWriting = 2
Set objTextFile = objFSO.OpenTextFile _
(strDirectory & strFile, ForWriting, true)
' Writes strText every time you run this VBScript
objTextFile.WriteLine(strText)
objTextFile.Close
End If
WScript.Quit
' End of VBScript to write to a file with error-correcting Code