Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outputting ComputerName into text file?

Status
Not open for further replies.

2marshall8

Technical User
Jul 29, 2006
18
0
0
US
I need a way to output the computer name of a machine to a text file w/o the carriage return at the end. I know how to do this using batch files and echo but it includes this carriage return which messes other parts of my script up. If you have any advice on this I would greatly appreciate it. Here is what I have, but I just need a way to direct this echo command into a text file

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
For Each objItem In colItems
computerName = objItem.name
wscript.echo computerName
Next


marshall
 
Hope this helps. I assumed you had more script so I didn't try to add this to yours. Thanks.

Code:
const fsoForWriting = 2
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Open the text file
Dim objTextStream
Set objTextStream = objFSO.OpenTextFile("C:\YourDirectory\nameoffile.txt", fsoForWriting, True)

'Write computer name to file nameoffile.txt
objTextStream.WriteLine computername

'Close the file and clean up
objTextStream.Close
Set objTextStream = Nothing
Set objFSO = Nothing
 
this helped a lot, thanks. I got the name into the txt file but for some reason I still get the space after this is entered into the file. Is there a way to remove the carriage return after this is entered so that there isn't any blank line created?

thanks
 
I saw this code at the link provided if you still need any help. This code here only trims the blanks lines. It doesn't keep it from happening but could be incorporated with the other code to to make sure that you have no blank lines in your text files.

Code Source:
Code:
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", ForReading)

Do Until objFile.AtEndOfStream
    strLine = objFile.Readline
    strLine = Trim(strLine)
    If Len(strLine) > 0 Then
        strNewContents = strNewContents & strLine & vbCrLf
    End If
Loop

objFile.Close

Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", ForWriting)
objFile.Write strNewContents
objFile.Close
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top