Error on line 63, that I cannot seem to debug. - vbscript

This is line 63 for me: Set ipAddrFile = fso.OpenTextFile(fileName,Read,ASCII)
I was also getting an error for line 51, but adding the quotes solved my problem.. Well, It got rid of the error. I tried to do the same thing with line 63, but I get the error no matter what. I am also running this program on windows 10 and on a Windows Vista virtual computer.
And this is the Script I'm trying to debug:
' VBScript: IP_FileWrite.vbs
' Written by: Kathleen Williams
' Date: 2/7/18
' Class: COMP230
' Professor: Professor James Lewis
' ===================================
' This initializes a 2-dimension array
' of IP Address. The first index +100
' is the room# and the second index+1
' is the computer# in the room.
dim ipAddress(5,3)
ipAddress(0,0)="192.168.10.11"
ipAddress(0,1)="192.168.10.12"
ipAddress(0,2)="192.168.10.13"
ipAddress(0,3)="192.168.10.14"
ipAddress(1,0)="192.168.10.19"
ipAddress(1,1)="192.168.10.20"
ipAddress(1,2)="192.168.10.21"
ipAddress(1,3)="192.168.10.22"
ipAddress(2,0)="192.168.10.27"
ipAddress(2,1)="192.168.10.28"
ipAddress(2,2)="192.168.10.29"
ipAddress(2,3)="192.168.10.30"
ipAddress(3,0)="192.168.10.35"
ipAddress(3,1)="192.168.10.36"
ipAddress(3,2)="192.168.10.37"
ipAddress(3,3)="192.168.10.38"
ipAddress(4,0)="192.168.10.43"
ipAddress(4,1)="192.168.10.44"
ipAddress(4,2)="192.168.10.45"
ipAddress(4,3)="192.168.10.46"
ipAddress(5,0)="192.168.10.51"
ipAddress(5,1)="192.168.10.52"
ipAddress(5,2)="192.168.10.53"
ipAddress(5,3)="192.168.10.54"
' Define constants, variables and set object properties
CONST ForReading = 1
CONST ForWriting = 2
CONST ForAppending = 8
Const ASCII = 0
'Defining the Variables
fileName = "C:\VBScripts\IP_Addresses.csv"
ipAddrStr = ""
' Create New Folder
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(fileName) Then
fso.DeleteFile(fileName)
End If
Set ipAddrFile = fso.CreateTextFile("fileName,ForWriting,ASCII")
' Read from array and write a line of text.
For room = 0 to 5
For computer = 0 to 3
ipAddrStr = CStr(room+100) & "," & CStr(computer+1) & "," & _
ipAddress(room,computer) & vbCrLf
ipAddrFile.Write(ipAddrStr)
Next
Next
ipAddrFile.close
' Set object properties and close file object.
Set ipAddrFile = fso.OpenTextFile(fileName,Read,ASCII)
WScript.Echo iPAddrFile.ReadAll
ipAddrFile.close

VBScript was designed by at least two people. A genius, who defined the properly named functions CreateTextFile() and OpenTextFile() and
their default arguments to make standard tasks - create an ASCII file, read from an ASCII file - easy:
Option Explicit
Const csFSpec = "48798232.txt"
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
Dim sD : sD = "could be an array, but is just a string for show: " & Now()
' using *Create*TextFile + defaults to always create an ASCII File
Dim tsW : Set tsW = oFS.CreateTextFile(csFSPEC)
tsW.WriteLine sD
tsW.Close
' using OpenTextile + defaults to read from an ASCII File
' no need for a variable or .Close
WScript.Echo oFS.OpenTextFile(csFSPEC).ReadAll()
output:
cscript 48798232.vbs
could be an array, but is just a string for show: 15.02.2018 04:45:06
Then there came the idiot in residence and messed up OpenTextFile() with lots of optional parameters in arbitrary order to make it useable for file creation. From then on people mixed up those functions and their parameter lists.
Code 1: Set ipAddrFile = fso.CreateTextFile("fileName,ForWriting,ASCII")
Create: object.CreateTextFile(filename[, overwrite[, unicode]])
Open: object.OpenTextFile(filename[, iomode[, create[, format]]])
Code 2: Set ipAddrFile = fso.OpenTextFile(fileName,Read,ASCII)
Never being sure of the arguments, their data types, or their order, they use desperate means like quoting the (wrong) argument list.
So: Check the docs carefully (e.g. be aware of the difference between a boolean ForWriting vs. a numerical iomode of the same name (<-- the idiot at work, obviously) and use the defaults for simple/standard tasks.

Related

The OpenTextFile is overriding opened text file by clearing it

I am currently having a problem with OpenTextFile. I created a script over a year ago. Recently, the script started giving me problems. It's clearing the first text file and giving me an error.
Set objArgs = WScript.Arguments
myFile = objArgs(0)
numberofTXT = objArgs(1)
line = objArgs(2)
Set f = CreateObject("Scripting.FileSystemObject").OpenTextFile(myFile, line)
d = f.ReadLine
Set objFSO=CreateObject("Scripting.FileSystemObject")
outFile=numberofTXT
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write d & vbCrLf
objFile.Close
This is a super basic script I wrote to explain my issue. Takes in 3 files as arguments. For example LOL.txt, Hi.txt, and a specific line of LOL.txt (Why did 13-year-old me give examples using the word Lol, oh my gosh). This is meant to read the first file and write the data from the first file to the second file.
This issue was due to my current understanding (when I posted this) and not knowing what documentation was. I was self-taught. Please make sure to read the documentation if you have any issues with OpenTextFile and make sure your arguments are correct for the function.
Read and apply the OpenTextFile Method reference:
Opens a specified file and returns a TextStream object that can be
used to read from, write to, or append to the file.
Syntax
object.OpenTextFile(filename[, iomode[, create[, format]]])
Arguments
object   Required. Object is always the name of a FileSystemObject.
filename Required. String expression that identifies the file to open.
iomode   Optional. Can be one of three constants: ForReading, ForWriting, or ForAppending.
create   Optional. Boolean value that indicates whether a new file can be created if the specified filename doesn't exist. The value
is True if a new file is created, False if it isn't created. If
omitted, a new file isn't created.
format   Optional. One of three Tristate values used to indicate the format of the opened file (TristateTrue = -1 to open the file as
Unicode, TristateFalse = 0 to open the file as ASCII,
TristateUseDefault = -2 to open the file as the system default). If
omitted, the file is opened as ASCII.
Settings
The iomode argument can have any of the following settings:
Constant Value Description
ForReading 1 Open a file for reading only. You can't write to this file.
ForWriting 2 Open a file for writing.
ForAppending 8 Open a file and write to the end of the file.
Read CreateTextFile Method reference as well. Then, the following commented code snippet could help:
Const ForReading = 1
Set objArgs = WScript.Arguments
myFile = objArgs(0) ' file to read
numberofTXT = objArgs(1) ' file to write
line = objArgs(2) ' line serial number to write into output file
' (or number of lines?)
Set objFSO = CreateObject("Scripting.FileSystemObject")
outFile=numberofTXT
Set objFile = objFSO.CreateTextFile(outFile,True)
Set f = objFSO.OpenTextFile(myFile, ForReading)
lineindex = 1
Do until f.AtEndOfStream
d = f.ReadLine
if lineindex = line Then ' only take the line-th line
objFile.Write d & vbCrLf ' or objFile.WriteLine d
Exit Do ' transfers control to the statement immediately following Loop statement
End If
lineindex = lineindex + 1
Loop
objFile.Close
f.Close

VB script run time error input past end of file

I'm taking a scripting class and im having some issues with my script. According to the lab assignment all my syntax is correct. However i keep getting a input past end of file error on line 60,1. I've starred at the program forever and checked all lines letter for letter for quite some time with no luck. Any help would be greatly appreciated. Here is the script.
dim ipAddress(5,3)
ipAddress(0,0)="192.168.10.11"
ipAddress(0,1)="192.168.10.12"
ipAddress(0,2)="192.168.10.13"
ipAddress(0,3)="192.168.10.14"
ipAddress(1,0)="192.168.10.19"
ipAddress(1,1)="192.168.10.20"
ipAddress(1,2)="192.168.10.21"
ipAddress(1,3)="192.168.10.22"
ipAddress(2,0)="192.168.10.27"
ipAddress(2,1)="192.168.10.28"
ipAddress(2,2)="192.168.10.29"
ipAddress(2,3)="192.168.10.30"
ipAddress(3,0)="192.168.10.35"
ipAddress(3,1)="192.168.10.36"
ipAddress(3,2)="192.168.10.37"
ipAddress(3,3)="192.168.10.38"
ipAddress(4,0)="192.168.10.43"
ipAddress(4,1)="192.168.10.44"
ipAddress(4,2)="192.168.10.45"
ipAddress(4,3)="192.168.10.46"
ipAddress(5,0)="192.168.10.51"
ipAddress(5,1)="192.168.10.52"
ipAddress(5,2)="192.168.10.53"
ipAddress(5,3)="192.168.10.54"
const READ = 1
const WRITE = 2
const APPEND = 8
const ASCII = 0
dim fileName
fileName = "IP_Addresses.csv"
dim ipAddrStr
ipAddrStr = ""
dim fso
Set fso = Wscript.CreateObject("Scripting.FileSystemObject")
Set ipFileObj = fso.CreateTextFile(fileName,True,ASCII)
For room = 0 to 5
For computer = 0 to 3
ipAddrSr = CStr(room+100) & "," & CStr(computer+1) & "," ipAddress(room,computer)
& vbCrlf
ipFileObj.write(ipAddrStr)
Next
Next
ipFileObj.close
Set ipFileObj = fso.OpenTextFile(fileName,READ,ASCII)
WScript.Echo ipFileObj.ReadAll **' this is line 60**
ipFileObj.Close
As you don't use "Option Explicit", you get what you deserve: You (try to) concatenate the lines into ipAddrSr but write ipAddrStr to the file. So nothing gets written to the file.
Fix the syntax error and the bad name to:
ipAddrStr = CStr(room+100) & "," & CStr(computer+1) & "," & ipAddress(room,computer) & vbCrlf
Assuming that the file isn't empty, perhaps you need to specify the directory the file is in? I think this can be done either in your script:
fileName = "c:\your_directory\IP_Addresses.csv"
Or if you run it in the command line via cscript...
cscript.exe your.vbs "c:\your_directory\IP_Addresses.csv"
You can check the file size before executing your Echo if you like...
if fileName.size > 0 then
Set ipFileObj = fso.OpenTextFile(fileName,READ,ASCII)
WScript.Echo ipFileObj.ReadAll **' this is line 60**
ipFileObj.Close
else
WScript.Echo = "File was empty"
end if
See details of passing an argument to your script here.

Windows script host error 800A0046

I'm receiving the following error when I run my program:
Script: C: My Folder\Tracking Macro.vbs
Line: 70
Char: 1
Error: Permission denied
Code: 800A0046
Source: Microsoft VBScript runtime error
Here is the code.
' Set constants for reading, writing, and appending files
Const ForReading = 1, ForWriting = 2, ForAppending = 8
' Sets up the object variables.
Dim objExcel, objFSO, objTextFile, objCSVFile
' Sets up the string variables.
Dim strTextFile, strHeadLine, strTextLine, strCSVFile
' Sets up the all the string variables for the program.
Dim Desktop, todaysDate, usageDate, myDay, myMonth, myYear
'This creates the required Objects
Set objExcel = CreateObject("Excel.application")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
Desktop = WshShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\" & "Desktop"
' Set date for date stamp in file name and sheet name
todaysDate = Date()
myMonth = Month(todaysDate)
If Len(myMonth)=1 Then myMonth="0" & myMonth
myDay = Day(todaysDate)
If Len(myDay)=1 Then myDay="0" & myDay
myYear = Right(Year(todaysDate), 2)
usageDate = myMonth & myDay & myYear
' Set up the origin and destination files
strTextFile = (Desktop & "\MacroTracker.txt")
strCSVFile = "C: My Folder\TrackingTesting" & usageDate & ".csv"
strHeadLine = "Macro Name,User ID,Ran At,Contracted Rate,BHVN,Set Number,Provider TIN,Billed Charge,Service Code"
Set objTextFile = objFSO.OpenTextFile(strTextFile)
' Read the entire origin file
Do Until objTextFile.AtEndOfStream
strTextLine = objTextFile.ReadLine
Loop
If (objFSO.FileExists(strCSVFile)) Then
' Create object for appending current TXT file to CSV file
Set objCSVFile = objFSO.OpenTextFile(strCSVFile, ForAppending, True)
' Write an append line of data to the CSV file
objCSVFile.WriteLine strTextLine
Else
' Create CSV file to write to with today's date
Set objCSVFile = objFSO.CreateTextFile(strCSVFile, True)
' Create object for appending current TXT file to CSV file
Set objCSVFile = objFSO.OpenTextFile(strCSVFile, ForAppending, True)
' Write initial header for the CSV file
objCSVFile.WriteLine strHeadLine
' Write an append line of data to the CSV file
objCSVFile.WriteLine strTextLine
End If
' Wait for file to be written to
Wscript.Sleep 600
' Delete origin file to prevent user tampering
objFSO.DeleteFile(strTextFile)
Line 70 is the very last line where I'm deleting the text file. According to every help site I've seen, this is EXACTLY how it should be typed. I checked the permissions of the file...I have full control, so I should be able to delete it. It's only meant to be a temp file, not something that stores info for long periods of time.
I've checked Microsoft and all other help sites for the error code and have not found any solutions that can help me. I'm hoping someone may have ran into a similar instance and found a resolution.
Your file is still open. You need to add this:
objTextFile.Close
somewhere before you try to delete it. I would put it right after you're done using the file.

Reg - Error Input Past End of File - VbScript

I wrote a simple program to write and read data from text file using FileSystemObject, but it doesn't work and gives me a runtime error:
Input Past End of File.
Kindly let me know the mistake I made here.
'Here is the Program -
Dim Fso 'Reference obect to File system
Dim TxtObj 'Reference to Text stream object
Dim Txt 'Reference to Text stream object to open file
Dim i 'To Read Text in file
Set Fso = CreateObject("Scripting.FileSystemObject")
Set Txtobj = Fso.CreateTextFile("C:\Users\ACER\Desktop\Project Folder\NewText_3.txt")
Txtobj.Write("Hello")
Txtobj.Close
Set Txt = Fso.OpenTextFile("C:\Users\ACER\Desktop\Project Folder\NewText_3.txt",1)
Do while Txt.AtEndOfStream<>1
i = Txt.Read(1)
Loop
Msgbox i
Txt.close
Option Explicit ' safety belt
Const FSpec = "14481096.txt" ' dry the file name
Dim Fso 'Reference object to File system
Set Fso = CreateObject("Scripting.FileSystemObject")
Dim TxtObj 'Reference to Text stream object (used for rwrite and read)
Set Txtobj = Fso.CreateTextFile(FSpec)
Txtobj.WriteLine "Hello"
Txtobj.Close
Dim Letter 'To Read Text in file (each letter)
Set Txtobj = Fso.OpenTextFile(FSpec) ' ForReading is the default, no need for magic number 1)
Do Until Txtobj.AtEndOfStream ' never compare boolean values against boolean literals
' or - worse - values of other types you *hope* VBScript
' will convert correctly
Letter = Txtobj.Read(1) ' letter
WScript.Echo "got", Letter
Loop
Txtobj.Close
output:
cscript 14481096.vbs
got H
got e
got l
got l
got o
got <-- aka cr
got <-- aka lf (in case you are wondering)
Extra hint:
>> WScript.Echo CInt(True), CInt(False)
>>
-1 0
>>

.Batch or .VBS (Win_Server_2003)

I have two .csv files: inputfile.csv and mainfile.csv
I need to write a script that:
1- will read one by one all the records in inputfile.csv
2- then find if there is a match in the mainfile.csv
3- if there is a match then do nothing and read the next record from inputfile.csv
4- else if there is not a match in the mainfile.csv write that record from the inputfile.csv to the mainfile.csv
This solution uses Scripting.Dictionary to record each line in mainfile.csv. Then, to see if a line in inputfile.csv is new, all it takes is to see if that line exists in the dictionary. For example:
mainfile.csv
exists,one
exists,two
exists,three
exists,four
exists,five
inputfile.csv
exists,two
new,one
exists,four
new,two
new,three
mainfile.csv (after running the program)
exists,one
exists,two
exists,three
exists,four
exists,five
new,one
new,two
new,three
Here's the code:
Option Explicit
Const ForReading = 1, ForWriting = 4, ForAppending = 8
Dim oFso : Set oFso = CreateObject("Scripting.FileSystemObject")
Dim oDict : Set oDict = CreateObject("Scripting.Dictionary")
'
' Read the contents of 'mainfile.csv'. Add each line to a dictionary
' to allow for a quick lookup.
'
Dim oFileMain : Set oFileMain = oFso.OpenTextFile("mainfile.csv", ForReading)
Dim sLine
While Not oFileMain.AtEndOfStream
sLine = oFileMain.ReadLine()
oDict.Add sLine, True
Wend
oFileMain.Close
Set oFileMain = Nothing
'
' Re-open 'mainfile.csv' in append mode.
'
Set oFileMain = oFso.OpenTextFile("mainfile.csv", ForAppending)
'
' Read the contents of 'inputfile.csv'. Write a line to 'mainfile.csv'
' only if that line does not exist in the dictionary.
'
Dim oFileInput : Set oFileInput = oFso.OpenTextFile("inputfile.csv", ForReading)
While Not oFileInput.AtEndOfStream
sLine = oFileInput.ReadLine()
If Not oDict.Exists(sLine) Then ' not a duplicate!
WScript.Echo "Found new line: [" & sLine & "]"
oFileMain.WriteLine sLine
End If
Wend
oFileInput.Close
Set oFileInput = Nothing
'
' Wrap it up.
'
oFileMain.Close
Set oFileMain = Nothing
Set oDict = Nothing
Set oFso = Nothing
' End
Here is my best attempt at doing it in python, not knowing the structure of the files:
with open("mainfile.csv", "r") as main:
records = [x.strip() for x in main.readlines()]
with open("inputfile.csv", "r") as input:
inputs = [x.strip() for x in input.readlines()]
for input in inputs:
if input not in records:
records.append(input)
with open("mainfile.csv", "w") as main:
for record in records:
main.write(record + "\n")
So for the following files you start with this:
inputfile.csv:
A quick brown fix
Two turtle doves
Feather boa
mainfile.csv:
Some other stuff
Two turtle doves
Friends in low places
Feather boa
Another Fairly boring thing
After you run the script mainfile.csv looks like this:
Some other stuff
Two turtle doves
Friends in low places
Feather boa
Another Fairly boring thing
A quick brown fix

Resources