1

New to VBScript and having a problem grasping this concept.

This is the code:

Set WshNetwork = WScript.CreateObject("WScript.Network")
strCompName = WshNetwork.Computername
Wscript.Echo WshNetwork.Username >j:\strCompName.txt
WScript.Quit()

Basically I want to the username dumped to a text file and the text file should be named with the name of the computer. I've tried putting the strCompName in quotes, single quotes, parenthesis with no success.

2
  • You're mixing batch-file syntax with vbscript > doesn't mean the same thing. If you want to save to a file will need to use the Scripting.FileSystemObject object. Commented Sep 17, 2015 at 14:08
  • Great! Just one follow up..How can I use the computername as part of the filename? Commented Sep 17, 2015 at 14:17

2 Answers 2

1

Here is the code that you can use. You need to use FileSystemObject. The FileSystemObject is used to gain access to a computer's file system. It can create new files and access existing ones.

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strCompName = WshNetwork.Computername

'writing to file
outFile="c:\TEMP\" & strCompName & ".txt"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write WshNetwork.Username & vbCrLf
objFile.Close

Set objFile = Nothing
Set objFSO = Nothing
Set WshNetwork = Nothing

WScript.Quit()

Save this in .vbs file and run and you will get a text file with computer name in TEMP folder (Change the path if you like).

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much! Can you tell me what the purpose is of setting the objfile, objFSO, wshnetwork to nothing?
You should release reference to the object as soon as you no longer need it, it's just good a practice. However VBScript engine does this automatically when the variable that keeps the reference goes out of scope.
1

This code should work. This code opens the file and appends it if the file exists or creates a file and writed to it if it does not exist.

'constants
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

'Load domain, username, & computer variables
Set oShell = CreateObject( "WScript.Shell" )
sDomain = oShell.ExpandEnvironmentStrings( "%USERDOMAIN%" )
sUserName = oShell.ExpandEnvironmentStrings( "%USERNAME%" )
sComputer = oShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )

'Setup filesystemobject
Set oFSO=CreateObject("Scripting.FileSystemObject")

'Check to see if file exists. If exists open it forAppending
'else create file and write to it.

outFile="c:\export\" & sComputer & ".txt"
If oFSO.FileExists(outFile) Then
    Set objFile = oFSO.OpenTextFile(outFile, ForAppending, True, TristateTrue)
Else
    Set objFile = oFSO.CreateTextFile(outFile,True)
End If

'write to file
objFile.WriteLine sDomain & "\" & sUsername & " - " & Now

'clean up objects
objFile.Close
Set objFile = Nothing
Set oFSO = Nothing
Set oShell = Nothing

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.