0x800a0005 Error when using OpenAsTextStream - vbscript

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.

Related

Check if a file exists faster than OPEN DATASET

I'm searching for a way to check if a file exists before using the OPEN DATASET command to open it. The OPEN DATASET command takes up to 30 seconds to trigger an exception, which is too slow for my liking.
This is the code:
TRY.
OPEN DATASET lv_file FOR OUTPUT IN TEXT MODE
ENCODING DEFAULT
WITH SMART LINEFEED.
CONCATENATE ` ` lv_resultdata INTO lv_resultdata.
TRANSFER lv_resultdata TO lv_file.
CLOSE DATASET lv_file.
CATCH cx_sy_file_access_error.
MESSAGE 'Placeholder-message. File cannot be reached'.
EXIT.
ENDTRY.
Try this:
DATA: filepath TYPE epsf-epsdirnam VALUE '/tmp'.
CALL FUNCTION 'EPS_GET_DIRECTORY_LISTING'
EXPORTING
dir_name = filepath
file_mask = 'somefile.txt'
EXCEPTIONS
invalid_eps_subdir = 1
sapgparam_failed = 2
build_directory_failed = 3
no_authorization = 4
read_directory_failed = 5
too_many_read_errors = 6
empty_directory_list = 7
OTHERS = 8.
CHECK sy-subrc = 0.
" writing dataset
It can also be used for remote servers.

chgport Command not Recognized when Running from a Process to Capture Output

I am trying to determine what serial ports are available using VB.NET. When I run a batch file from the command line, it works fine. When I try to run it and capture the output, the chgport command is not recognized as shown below
I tried changing UseShellExecute to False, but it says it needs to the true to capture the output.
I tried being very specific with the paths, even using a command shell to run the chgport command. None of that made any difference.
Any thoughts on what is causing the difference between running from the command line and running in a process to capture the output?
The code snippet I'm using to capture the output:
' Fill in the com ports and file versions
Dim oProcess As New Process()
Dim oStartInfo As New ProcessStartInfo("c:\xpp_prog\ports_and_files.bat", "")
oStartInfo.UseShellExecute = False
oStartInfo.RedirectStandardOutput = True
oStartInfo.RedirectStandardError = True
oProcess.StartInfo = oStartInfo
oProcess.Start()
Dim sOutputS As String
Using oStreamReader As System.IO.StreamReader = oProcess.StandardOutput
sOutputS = oStreamReader.ReadToEnd()
End Using
Dim sOutputE As String
Using oStreamReader As System.IO.StreamReader = oProcess.StandardError
sOutputE = oStreamReader.ReadToEnd()
End Using
MsgBox(sOutputS + sOutputE)
Here is the batch file:
#echo off
cmd /c C:\Windows\System32\chgport.exe
dir c:\xpp_prog\build_*

Having trouble with a vbscript creating a new vbscript containing a variable from the original

I am trying to make a vbs that creates a new vbs file, but im having some trouble with the creation of the second file.
Earlier in the first script it asks for your desired username, which is then assigned to a variable named user.
When it creates the second script, i want the user name to display in a message saying Welcome back [insert username here]. Please input a program value.
problem is, i cant get it to place your username inside of the new scripts code.
attempt 1
If VTCreate1 = 6 And VTCreate2 = 7 And VTCreate3 = 7 And VTCreate4 = 7 And VTCreate5 = 7 Then
Set objFile = objFS.CreateTextFile("C:\Users\Public\Desktop\Aeshnidae_1627374757.vbs")
objFile.WriteLine("vtcmd=inputbox(""Welcome back "")" & user & "("". Please input a program value."")")
attempt 1 results in the newly created script containing the following
vtcmd=inputbox("Welcome back ")Human(". Please input a program value.")
attempt 2
If VTCreate1 = 6 And VTCreate2 = 7 And VTCreate3 = 7 And VTCreate4 = 7 And VTCreate5 = 7 Then
Set objFile = objFS.CreateTextFile("C:\Users\Public\Desktop\Aeshnidae_1627374757.vbs")
objFile.WriteLine("vtcmd=inputbox(""Welcome back & user & . Please input a program value."")")
attempt 2 results in the following
vtcmd=inputbox("Welcome back & user & . Please input a program value.")
any idea how to get the desired username properly inside of the message?
Let's start with the final desired output line
vtcmd=inputbox("Welcome back xxxxxx. Please input a program value")
To place it inside a string variable (what we will later write) we need to quote the line and escape inner double quotes
"vtcmd=inputbox(""Welcome back xxxxxx. Please input a program value"")"
To replace the placeholder we need to cut our string
"vtcmd=inputbox(""Welcome back " ". Please input a program value"")"
close quote ^ ^ open quote
And concatenate the variable with the two strings
"vtcmd=inputbox(""Welcome back " & user & ". Please input a program value"")"
So, the final code line will be
objFile.WriteLine "vtcmd=inputbox(""Welcome back " & user & ". Please input a program value"")"

VBScript Error 52 Bad file name or number

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.

How do I load data into the Data Portal of DIAdem with VBScript?

I want to load a ".tdm" file into DIAdem (National Instruments) with a VBScript but can't find how to do this. I have Dialog which opens a browse-window, which returns the path as a string. So I was hoping to have a function which would work something like this:
Call Data.Root.ChannelGroups.Load(myStringWithThePath)
Just noticed I got a tumbleweed-badge on this question. Here is the solution I found last week, so if anybody cares:
Set objDialog = CreateObject("UserAccounts.CommonDialog")
objDialog.Filter = "TDM-Datei(*.tdm)|*.tdm|All Files (*.*)|*.*"
objDialog.FilterIndex = 1
boolResult = objDialog.ShowOpen
If boolResult <> 0 Then
Call Data.Root.ChannelGroups.RemoveAll()
Call DataDelAll
Call DataFileLoad(objDialog.FileName)
End If

Resources