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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text output has weird characters 1

Status
Not open for further replies.

fdalmoro

IS-IT--Management
May 8, 2003
90
US
Hi Guys, I have a script that allows me to input a range of IP's to scan and check if they're responding. If the IP responds it puts the IP into a text file. I can then use that list of IP's to do whatever I want to do on the network.

The problem is the list that is being created here has some special characters which does not allow it to be used again by any other vbscript. If you type edit c:\iplist.txt at a command prompt you can see the special characters that are causing the problem. If you open up the text file in notepad it looks fine.

Anyone have any idea on how to get around this?

Thanks, here is the code:

Code:
OPTION EXPLICIT
On Error Resume Next

DIM objFSO, objOutputfile, IPlist, StartIP, StartArray
DIM strPingIP, IPOctet1, IPOctet2, IPOctet3, IPOctet4, FinalOctet, objPingIPResult, objPingIP

Const ForReading = 1
Const ForWriting = 2
IPlist = "c:\iplist.txt"

set objFSO = CreateObject("Scripting.FileSystemObject")
set objOutputfile = objFSO.CreateTextFile(IPlist,ForWriting,TRUE)

StartIP = InputBox("Enter the First IP in the range to ping. eg... 10.1.10.5")
StartArray = split(StartIP,".")
IPOctet1 = StartArray(0)
IPOctet2 = StartArray(1)
IPOctet3 = StartArray(2)
IPOctet4 = StartArray(3)

FinalOctet = InputBox("Enter just the Last Octet of the IP range to ping. eg..." &  IPOctet1 & "." & IPOctet2 & "." & IPOctet3 & "." & IPOctet4 & " - " & IPOctet1 & "." & IPOctet2 & "." & IPOctet3 & "." & "XXX")

While Cint(IPOctet4) <= Cint(FinalOctet)
	strPingIP = IPOctet1 & "." & IPOctet2 & "." & IPOctet3 & "." & IPOctet4
	wscript.echo "Pinging... " & strPingIP
	Set objPingIP = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_PingStatus where address = '" & strPingIP & "'")
	For Each objPingIPResult In objPingIP
		If objPingIPResult.StatusCode = 0 Then
			objOutputfile.WriteLine (strPingIP)
		End if
		IPOctet4 = IPOctet4 + 1
	Next
Wend

Fernando
 
[1]
>set objOutputfile = objFSO.CreateTextFile(IPlist,ForWriting,TRUE)
Wrong understanding.

ForWriting is now standing as a proxy for overwrite being true.

TRUE is now standing as a proxy for system default encoding which for your NT series being UNICODE.

Hence, the file created is of unicode encoding with BOM (FF FE) little endian.

When you read it with fso, you have now therefore to specify explicitly the encoding as system (-1) or unicode (-2). If you don't, ascii is assumed: therefore failed.

[2] Always after writing all:
[tt] objOutputfile.close[/tt]

[3] I am about, if not for the reason not trying to make the forum too non-caring! -- but one day, I may --, to red flag any post with "on error resume next" at the top of the script. I blame ms scripting team for that above all.

[4] You make unwarranted assumption of users' obeying your instruction to enter data in the exact format you ask for. (I know... private use... all that!)
 
[1]I made the change:

set objOutputfile = objFSO.CreateTextFile(IPlist)

and it works fine now.

[2] true, added in ;)

[3] good point.

[4] Yah, just for me. I do have a version which asks for full IP both times but I got lazy and figured I can make do with just the last octet not the full end IP :)

Thanks!

Fernando
 
>on error resume next

There is nothing wrong with this construct when used appropriately. For example, it is the closest you can come to mimicking much of the API's way of dealing with errors, or getting close to the fashionable Try/Catch construct in VB/VBA.

Obviously, using it simply to stop errors being raised because one is too lazy to deal with errors is inappropriate use ...
 
I don't think I have any lesson to take on that. I know what it is and it is eroding newcomer's mind.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top