VBScript Error 52 Bad file name or number - vbscript

I have a VBScript which is used in Avaya CMS to generate a CSV every 3 seconds. This CSV is then read on each change by a node server.
Every now and then it will throw an error (Error 52) and it then requires someone to manually click OK on the prompt and then it continues.
I didn't write the initial script, but I have been trying to fix it so it doesn't throw this error.
As far as I understand, the issue is being caused when the script tries to write to the file when the node server is trying to read the file.
So far I have tried to fix this by making the file read only, then, in the script it makes it writeable, writes to it and makes it read only again.
I thought this had fixed the issue, but I have received Error 52 pop up again and now I am unsure.
Does anyone know if there is a way to get VB/Avaya to ignore the error? I was thinking something like putting On Error Resume Next before the line that writes the file?
I know ignoring errors is not good practice, but in this case, if it does ignore it, it just means the file wont be updated on this loop, but 3 seconds later it will loop again so it should be fine.
Script:
On Error Resume Next
cvsSrv.Reports.ACD = 1
Set Info = cvsSrv.Reports.Reports("Real-Time\Designer\Skill Status")
If Info Is Nothing Then
Else
b = cvsSrv.Reports.CreateReport(Info,Rep)
If b Then
Rep.RefreshInterval = 3
Rep.SetProperty "Split/Skill","5"
Set WshShell = CreateObject("WScript.Shell")
theHTMLLocation = "C:\wamp\www\wallboards\agent_status.csv"
theSleepFile = "C:\wamp\www\wallboards\sleep.vbs"
theTemplateLocation = ""
Set fso = CreateObject("Scripting.FileSystemObject")
Set gfile = fso.GetFile("C:\wamp\www\wallboards\agent_status.csv")
Do While Rep.Window.Caption <> ""
gfile.Attributes = 0
d = Rep.ExportData("C:\wamp\www\wallboards\agent_status.csv", 44, 0, False, True, True)
gfile.Attributes = 1
WshShell.Run "wscript.exe """ & theSleepFile & """", , True
Loop
If Not cvsSrv.Interactive Then cvsSrv.ActiveTasks.Remove Rep.TaskID
Set Rep = Nothing
End If
End If
Set Info = Nothing
P.S - I left out some Parameters at that start of the file which I don't think are relevant, if someone thinks they are I will add them.

Related

why does LogonHostedExchangeMailbox get stuck with no error message?

We are using Redemption Library to access our e-mails. Scripts are automated on server and triggered by Task Scheduler at regular intervals. After server reboots we usually experience the problem with Session.Logon, since the user needs to log on again.
To avoid this problem, we are trying now to use LogonHostedExchangeMailbox Session method. Due to unknown reason the script gets stuck at this command. Could anyone advise what the reason might be? Here is the sample:
set Session = CreateObject("Redemption.RDOSession")
InboxName = "xxx#domain.com"
Session.LogonHostedExchangeMailbox InboxName,InboxName, "pwd" 'the script gets stuck here and does not throw any errors.
set oFolder = Session.GetFolderFromPath("\\"&InboxName&"\Inbox")
set items = oFolder.items
i = items.count
do until i = 0
if items.count > 0 then
set Mail = Items.Item(i)
wscript.echo Mail.Subject
'wscript.echo Mail.Body
end if
i = i - 1
loop
Session.Logoff
Thanks.

dbDenyWrite still in force after recordset closed

I have some code that gets a value from another table, then updates the value in the table. This code is in a loop that performs for each item in recordset based on another table. The code works the first time through but the next time it errors on the first line saying access is denied because the table is being held by another use or the user interface. The code uses DAO.
Anyone have a clue as to why the dbDenyWrite is still in force after closing the recordset and destroying its reference?
Here is a code snippet:
Set rsRR = DataDB.OpenRecordset("Railroads", dbOpenTable, dbDenyWrite)
rsRR.Index = "Railroads_RRIx"
rsRR.Seek "=", RTrs!RR
If rsRR.NoMatch Then
' Write ERROR MESSAGE
rsRR.Close
Set rsRR = Nothing
GoSub CleanUp
ReverseRouteDataCollect = 0
Exit Function
End If
If Not dWork Is Nothing Then Set dWork = Nothing
Set dWork = New Scripting.Dictionary
FieldsSave dWork, rsRR
i = FieldsCopy(drr, dWork, "TemplatesRailroad")
If dWork(rsRR.Name & "$LastWaybillNo") = "999999" Then
rsRR.Edit
rsRR!LastWaybillNo = 2001
rsRR.Update
Else
rsRR.Edit
rsRR!LastWaybillNo = dWork(rsRR.Name & "$LastWaybillNo") + 1
rsRR.Update
End If
rsRR.Close
Set rsRR = Nothing
.. why the dbDenyWrite is still in force after closing the recordset
and destroying its reference?
Because you only do this in case of a NoMatch.
So either change the dbDenyWrite to allow for edits, or (slower) reset the recordset before starting editing it:
Set rsRR = DataDB.OpenRecordset("Railroads", dbOpenTable)

0x800a0005 Error when using OpenAsTextStream

My intention is to write every output line to a log file for archive purposes. Instead of writing to a file using OpenTextFile() and Write(), and then closing at the end of the script, I would like to write to the file as the script executes. If for whatever reason, the script is terminated before it has a chance to close the file, I worry I will lose those logs.
To achieve this, here is what I have attempted:
'Master Logs
Dim MasterLogFileName, MasterLogFile, MasterLogFileStream
Const ForAppending = 8
Const TristateUseDefault = 2
Function Write(text)
Wscript.StdOut.Write text
Set MasterLogFileStream = MasterLogFile.OpenAsTextStream(ForAppending, TristateUseDefault)
MasterLogFileStream.Write text
MasterLogFileStream.Close
End Function
'Create a Master Log file to write to
MasterLogFileName = "LogFile.log"
fso.CreateTextFile(MasterLogFileName)
Set MasterLogFile = fso.GetFile(MasterLogFileName)
Write("Test writing!")
Write("Test writing 2!")
However, I receive the following error:
0x800a005 - Microsoft VBScript runtime error: Invalid procedure call or argument
On this line:
Set MasterLogFileStream = MasterLogFile.OpenAsTextStream(ForAppending, TristateUseDefault)
When executing the Write() Function for the second time, using this line:
Write("Test writing 2!")
As specified by Lankymart and JosefZ, the reason this wasn't working was because I specified 2 for TristateUseDefault, instead of -2, as per the OpenAsTextStream method specifies.

VBScript/Classic ASP permission denied with fso.OpenTextFile after several uses

I have a process that loops through a moderate amount of data (1MB) storing it in an array and then periodically writes it to the disk. After several iterations, the script fails at fso.OpenTextFile() in my else section as though the file has not been closed or finished closing from the previous time the function was called. The iteration # doesn't seem to be specific as it's happened anywhere between the 2nd and 10th iteration that I can tell. The file is actually created and being appended to so it doesn't appear to be a permissions issue. I considering adding a time delay to the process but don't want to necessarily add overhead to an already long process.
OS: Windows 2012 R2
Any thoughts or suggestions appreciated.
'Write array to disk
sub writeFile()
'on error resumenext
set fso = Server.CreateObject("Scripting.FileSystemObject")
if needToCreateFile then
set objTextFile = fso.CreateTextFile(server.mappath("google/linklist.html"),true)
objTextFile.writeLine("<!DOCTYPE html>")
objTextFile.writeLine("<html>")
objTextFile.writeLine("<title>")
objTextFile.writeLine("Content Listing")
objTextFile.writeLine("</title>")
needToCreateFile = false
else
' OpenTextFile Method needs a Const value
' ForAppending = 8 ForReading = 1, ForWriting = 2
Set objTextFile = fso.OpenTextFile (filename, ForAppending, True)
end if
'Write contents of array to file
for each link in linkList
if link <>"" and not isNull(link) then
objTextFile.writeLine(link & "<br>")
end if
next
objTextFile.writeLine("</html>")
objTextFile.Close
set fso = nothing
set objTextFile = nothing
'on error goto 0
end sub
Follow Up - Solved
Adding a 3 second delay solved the problem, but significantly delayed the processing time. So, rather than opening and closing the file each time I wanted to write to it, I simply left it open until the entire script was done and thus I didn't need the delay.
sub writeFile()
if needToCreateFile then
set objTextFile = fs.CreateTextFile(server.mappath("google/linklist.html"),true)
objTextFile.writeLine("<!DOCTYPE html>")
objTextFile.writeLine("<html>")
objTextFile.writeLine("<title>")
objTextFile.writeLine("Content Listing")
objTextFile.writeLine("</title>")
needToCreateFile = false
end if
'Write contents of array to file
for each link in linkList
if link <>"" and not isNull(link) then
objTextFile.writeLine(link & "<br>")
end if
next
objTextFile.writeLine("</html>")
' objTextFile.Close
' set fso = nothing
' set objTextFile = nothing
end sub
Adding a 3 second delay solved the problem, but significantly delayed the processing time. So, rather than opening and closing the file each time I wanted to write to it, I simply left it open until the entire script was done and thus I didn't need the delay. See modified script above.

Input past end of file

I'm having some trouble figuring out what's wrong with my VBScript code as it's giving me
Script Engine Error:62:Input past end of file
when i'm trying to run
Function FSORead(ini,Section,Key,defval)
Set FSO = CreateObject("Scripting.FileSystemObject")
Set File = FSO.OpenTextFile("C:\Documents and Settings\ndtm\Application Data\IceChat\Scripts\"+ini)
keysearch = False
Do Until File.AtEndOfStream
if keysearch = False then
if File.ReadLine = "["&Section&"]" then
keysearch = True
end if
else
if Key = Mid(File.ReadLine,1,instr(File.ReadLine,"=")-1) then
FSORead = Mid(File.ReadLine,len(Key)+1)
end if
if Mid(File.ReadLine,1,1) = "[" then
FSORead = defval
end if
end if
Loop
FSORead = defval
End Function
And the File i'm calling does exist and does contain stuff like
[section1]
var1=randomvalue
[section2]
var=someothervalue
I have been trying different things but it's not working so i came here to see if someone could help me find what's wrong and how to fix it
Oh and this is the Sub calling it, it's made for a bot in a IRC network and the "SendCommand" basically allows you to execute non-script commands within the script itself
Sub FSOTest()
SendCommand "/msg #ndtm "&FSORead("http.ini","qtserver","survival","")
End Sub
Your
if Key = Mid(File.ReadLine,1,instr(File.ReadLine,"=")-1) then
tries to read two lines from the file. You need a variable that can be send to Mid and Instr.

Resources