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!

Microsoft VBScript runtime error: Bad file mode

Status
Not open for further replies.
Oct 23, 2009
15
US
Trying to create the following script and keep getting a Bad File Mode error. Can someone help me. I'm not much of a programmer and I'm banging my head against the wall. The error is on the line that reads:
Do Until ipFileObj.AtEndOfStream = True

In order to run the script you have to create a file called IP_Addresses.csv. Thank you for any help!

' Defining constants
const READ = 1
const WRITE = 2
const APPEND = 8
const ASCII = 0

' Defining variables and initializing
dim fileName, ipAddrStr, newRoom, comp1_IP, comp2_IP, comp3_IP, comp4_IP
fileName = "IP_Addresses.csv"
ipAddrStr = ""
newRoom = "106"
comp1_IP = "192.168.10.59"
comp2_IP = "192.168.10.60"
comp3_IP = "192.168.10.61"
comp4_IP = "192.168.10.62"

' Creating ASCII text file and overwriting
Set fso = CreateObject("Scripting.FileSystemObject")
ipAddrStr = _
newRoom & ",1," & comp1_IP & vbCrLf & _
newRoom & ",2," & comp2_IP & vbCrLf & _
newRoom & ",3," & comp3_IP & vbCrLf & _
newRoom & ",4," & comp4_IP & vbCrLf

'Append New Room Information to File
Set fso = CreateObject("Scripting.FileSystemObject")
If NOT fso.FileExists(fileName) Then
WScript.StdOut.WriteLine(Chr(7) & Chr(7)),0, False
WScript.StdOut.WriteLine("File Does Not Exit!!!" & vbCrlf & _
"You Must Create the File Before You can Read the File!!")
WScript.Quit
End If
Set ipFileObj = fso_OpenTextFile(fileName,APPEND,ASCII)
ipFileObj.Write(ipAddrStr)
ipFileObj.Close()

' Read and write IPs file
Set ipFileObj = fso_OpenTextFile(fileName,APPEND,ASCII)

' Read file one line at a time displaying room, computer, and IP
Do Until ipFileObj.AtEndOfStream = True

room = ipFileObj.Read(3)
ipFileObj.Skip(1)
computer = ipFileObj.Read(1)
ipFileObj.Skip(1)
ipAddress = ipFileObj.Read(13)
ipFileObj.SkipLine()
WScript.Echo "The IP Address in Room "& room &" for Computer " & computer& " is " &ipAddress
Loop

ipFileObj.Close()
 
As you are in APPEND mode (=fso mode ForAppending) you are ALWAYS atEndoFStream, appending from there on.
[wink]

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top