How can I get a count of files in a directory with scripting on Wonderware Archestra IDE - wonderware

Two files "test.txt" and "test2.txt" were created in C:\tmp_dir. I want to know how many files in "C:\tmp_dir".
My code:
dim s as string;
s = System.IO.Directory.GetFiles("C:\tmp_dir");
LogMessage(s.Length);
But it's obviously wrong. It's returns 40(amount of symbols in path and both files) instead of 2. How can I do this properly?

From https://stackoverflow.com/a/15867350/804773 :
dim s as Integer;
s = System.IO.Directory.GetFiles("C:\tmp_dir").Count();
LogMessage(Text(s,"#"));

Tanks a lot! As you mentioned "s" must be Integer not string. And with "length" instead of "count" works fine.
dim s as integer;
s = System.IO.Directory.GetFiles("C:\tmp_dir").length;
LogMessage(s);

Related

Calling Oracle 11g Stored Procedure Using VB6

I have a simple Oracle procedure as below. I am trying to call the procedure using VB6 and extract the output from the procedure.
CREATE OR REPLACE PROCEDURE EXTRACTTXN (reportdate IN DATE, p_recordset OUT SYS_REFCURSOR) AS
BEGIN
OPEN p_recordset FOR
SELECT
TXN_ID,
TXN_ACTION,
TXN_STATUS,
TXN_DATE,
TXN_AMOUNT
FROM TRANSACTIONS
WHERE
TRUNC(TXN_DATE) = TRUNC(reportdate)
END EXTRACTTXN;
The VB Code goes like this;
Sub FetchTransactions(ByVal ReportDate As Date, cnnMine as ADODB.Connection)
On Error GoTo TrapErr
Dim cmdMine As ADODB.Command, rsMine As ADODB.Recordset
cmdMine.ActiveConnection = cnnMine
cmdMine.CommandTimeout = 300
cmdMine.CommandType = adCmdStoredProc
cmdMine.CommandText = "EXTRACTTXN"
cmdMine.Parameters.Append cmdMine.CreateParameter("reportdate", adDate, adParamInput, , Format(ReportDate, "DD-MMM-YYYY"))
cmdMine.Parameters.Append cmdMine.CreateParameter("p_recordset", adVariant, adParamOutput)
Set rsMine = cmdMine.Execute
Do While rsMine.EOF
Debug.Print rsMine!TXN_ID, rsMine!TXN_ACTION, rsMine!TXN_STATUS, rsMine!TXN_DATE, rsMine!TXN_AMOUNT
rsMine.MoveNext
Loop
rsMine.Close
Exit Sub
TrapErr:
MsgBox Err.Number & " - " & Err.Description, vbExclamation, App.ProductName
End Sub
While running the code, I get the following Error:
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'EXTRACTTXN'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
Anything wrong in my code? Appreciate help.
Niz
The problem is that the types of your arguments as specified in your VB code don't match the types of the arguments as specified in your PL/SQL code. The most likely reason for your problem is that the Format function in VB6 returns a Variant type, not a Date type, and that Variant type is set to be a String type. See this for more information on the Format function.
In case you don't know, the way that Variant variables are set up is that they reserve 8 bytes to tell the world what the actual variable type is. So, if you pass your ReportDate in after applying the Format function to it, it will be a Variant that's telling the world it's a string. It's possible that the ADO Parameter object is happy with that (SQL Server is happy to parse properly-formatted strings into Date fields, after all) and Oracle isn't. In my limited experience with Oracle, I've found that it's fussier about that sort of thing than SQL Server.
Try losing the Format function and see if you at least get a different error.
I have managed to get this sorted. It's mainly due to me being new to Oracle and its complexity.
Here are the changes I made;
Stored Procedure Changes. Note that I have changed TRUNC(reportdate, 'DD') on the Where clause.
CREATE OR REPLACE PROCEDURE EXTRACTTXN (reportdate IN DATE, p_recordset OUT SYS_REFCURSOR) AS
BEGIN
OPEN p_recordset FOR
SELECT
TXN_ID,
TXN_ACTION,
TXN_STATUS,
TXN_DATE,
TXN_AMOUNT
FROM TRANSACTIONS
WHERE
TRUNC(TXN_DATE) = TRUNC(reportdate, 'DD')
END EXTRACTTXN;
VB Code Changes (Note that I have change the CommandText within parenthesis with a Call, removed the parameter name, changed the date format to DD/MMM/YYYY and removed the output parameter)
Sub FetchTransactions(ByVal ReportDate As Date, cnnMine as ADODB.Connection)
On Error GoTo TrapErr
Dim cmdMine As ADODB.Command, rsMine As ADODB.Recordset
cmdMine.ActiveConnection = cnnMine
cmdMine.CommandTimeout = 300
cmdMine.CommandType = adCmdStoredProc
cmdMine.CommandText = "{ call EXTRACTTXN}"
cmdMine.Parameters.Append cmdMine.CreateParameter(, adDate, adParamInput, , Format(ReportDate, "DD/MMM/YYYY"))
Set rsMine = cmdMine.Execute
Do While rsMine.EOF
Debug.Print rsMine!TXN_ID, rsMine!TXN_ACTION, rsMine!TXN_STATUS, rsMine!TXN_DATE, rsMine!TXN_AMOUNT
rsMine.MoveNext
Loop
rsMine.Close
Exit Sub
TrapErr:
MsgBox Err.Number & " - " & Err.Description, vbExclamation, App.ProductName
End Sub
The above worked perfectly.
Regards, Niz

What's wrong in Classic ASP StoredProcedure insert statement

Here is the classic ASP code
Set objCommandSec = CreateObject("ADODB.Command")
With objCommandSec
Set .ActiveConnection = MyConn
.CommandType = adCmdStoredProc
.CommandText = "ReportsPDFInsert"
.CreateParameter "#StatsID", adInteger, adParamInput
.Parameters("#StatsID") = xStats_ID
.CreateParameter "#MemberID", adInteger, adParamInput
.Parameters("#MemberID") = xMemberID
.CreateParameter "#LanguageID", adInteger, adParamInput
.Parameters("#LanguageID") = 1 '1=EN
.CreateParameter "#PDFFilename", adVarWChar , adParamInput
.Parameters("#PDFFilename") = PDFFilename
.Execute
End With
Here is the stored procedure code
ALTER PROCEDURE [dbo].[ReportsPDFInsert]
-- Add the parameters for the stored procedure here
#StatsID INT
,#MemberID INT
,#LanguageID INT
,#PDFFilename NVARCHAR(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
INSERT INTO [dbo].[ReportsPDF]
([StatsID]
,MemberID
,[LanguageID]
,[PDFFilename]
,[DateCreated])
VALUES
(#StatsID
,#MemberID
,#LanguageID
,#PDFFilename
,GETDATE())
END
I get error as
Error number: -2147217904
Error description: Procedure 'ReportsPDFInsert' expects parameter '#StatsID', which was not supplied.
Source: Microsoft OLE DB Provider for SQL Server
If I execute the stored procedure itself, then it is working fine. I have similar classic asp code in other page, and that works fine as well. yes, I made sure xStats_ID does have value. I printed just before the .Execute and I see the value.
Please somebody shed some light. Thanks
Try appending the parameters explicitly using something like this:
cmd.Parameters.Append cmd.CreateParameter("#StatsID",adInteger, adParamInput,xStats_ID)
instead of .Parameters("")
Here is another post that might help:
How to make a parametrized SQL Query on Classic ASP?
Only today I figured what was the problem.
I haven't included the adovbs.inc file for the constants to work.
And I don't know why it was throwing some other error message.
good reason to move away from Classic ASP [only if my boss listens]
Try dropping the "#" in your parameter names.

Converting call to a stored procedure from C# to VB.net

Dim sql1 As String = ("EXEC [dbo].[usp_GetReportData_All] #ReportID=N'{0}', #StartDate=N'{1}' #EndDate=N'{2}', #StartDate2=N'{3}' #EndDate2=N'{4}'", repotid1, startdata1, EndDate1, StartDate3,Enddate3 ) (this is what I tried to do in VB.net)
Ok normally I have this line of code in C# saved into a string then from there I use that string to run the stored procedure into a datatable. Apparently vb.net doesn't seem to like that format so I'm just wondering if it is possible to save this line into a string or not in vb.net
Oops mistake, this is what I do in C#:
string srcSQL = string.Format(then the line in parans up there)
As you can see in this examples of Console.Writeline(http://msdn.microsoft.com/en-us/library/aa324760(v=vs.71).aspx), you can use it for example:
Console.WriteLine("Grand total:\t{0,8:c}", Total);
For Example
Dim total As String
Dim result As String
total = "1000"
result = String.Format("restulado {0}", total)
MsgBox(result)
The var Total is formatted as currency
use String.Format("YourSQLText", parameter0, parameter1)

Translating PowerPoint VBA code to Delphi, "keep source formatting" issue

I am working with Delphi(2010), but I'm new with PowerPoint(2010)
I've found two codes for copying slides with "keep source formatting":
Sub test1()
Dim orig_slide, new_slide As Slide
Dim slide_range As SlideRange
Set orig_slide = ActivePresentation.Slides(2)
orig_slide.Copy
Set slide_range = ActivePresentation.Slides.Paste(6)
Set new_slide = slide_range.Item(1)
new_slide.Design = orig_slide.Design
new_slide.ColorScheme = orig_slide.ColorScheme
End Sub
Sub test2()
ActivePresentation.Slides(2).Select
ActiveWindow.Selection.Copy
ActiveWindow.View.PasteSpecial (DataType = ppPasteOLEObject)
End Sub
They both are giving desired results in PowerPoint but in Delphi i get exceptions :
test1, line
new_slide.Design = orig_slide.Design
exception class EOleSysError with message 'Member not found'
test2, line
ActiveWindow.View.PasteSpecial (DataType = ppPasteOLEObject)
exception class EOleException with message 'View.PasteSpecial : Invalid request. The specified data type is unavailable'
I am using Slide Sorter View, copying and pasting are working ok, I'm only trying to add "keep source formatting" command.
Thanks in advance
I think I've found a solution :
This code in Delphi (doesn't work)
var OrigSlide, NewSlide : Variant;
NewSlide.Design := OrigSlide.Design;
on the right side, Delphi seems to accept only variant_variable, it doesn't accept variant_variable.property
Left side seems to work in opposite way ?!?
When I replaced it with this code, it works
OrigSlide := OrigSlide.Design;
NewSlide.Design := OrigSlide;
But I can only guess why.

External App: Check if an Outlook Folder exists

SOLUTION BELOW
I've been looking all over the net to find a solution for this, but it seems quite hard to get an answer for this in Delphi...
Skip this if you're familiar with Outlook
Some explanation before:
The Contacts Folder in Outlook is organized like a foldertree in Windows. The Contacts are stored in the Contacts Folder itself or within subfolders.
My Code does add Contacts from an external Database into the Outlook contacts Database. To prevent double entries the programm is supposed to check all contacts and see if it can find an 'older' version of the contact entry and update it, or if not, create a new one.
Therefore I wrote a recursion which loops through the folders and checks the contacts.
Within a folder you can get the subfolder by (besides Next, Previous and Last)
Contacts:= Contacts.Folders.Getfirst
//The now selected Folder is the first subfolder within the previous selected one
If I am trying to get any property of this Subfolder like 'Items.Count' or anything else, an error occurs because this folder doesn't exist.
Therefore I want to check if the Folder exists or not, and skip to loop through this subfolder because otherwise the loop would break here and the program stops.
Skip until here if you're familiar with Outlook workings
THE PROBLEM:
In Debugger this Contacts/Folder Variable (an OleVariant, Pointer to the now selected Folder) contains values similar to this: '$0074974C'.
If there is no subfolder this value returns '$00000000'. This seems to be a pointer.
How should I check if a folder exists or not?
const
olFolderContacts = $0000000A;
var
outlook, NameSpace, Contact, ContactsRoot, Contacts: OleVariant;
begin
Outlook := CreateOleObject('Outlook.Application');
NameSpace := Outlook.GetNameSpace('MAPI');
ContactsRoot := NameSpace.GetDefaultFolder(olFolderContacts);
Contacts:= ContactsRoot;
//We're now in the Contacts Folder
Contacts:= Contacts.folders.getfirst;
//First Subfolder
What didn't work:
Check if
Contacts = '$00000000' (As string)
Contacts = '$00000000' (As OleVariant)
var
val:TVarRec;
code:
val:=Contacts;
string(Contacts.VWideChar) = '$00000000'
var
vntNothing: OLEVariant;
code:
TVarData(vntNothing).VType := varDispatch;
TVarData(vntNothing).VDispatch := Nil;
Contacts = vntNothing
Contacts = unassigned
...
...
In VBA this problem has a simple solution
if Contacts = Nothing
But there is no 'Nothing' in Delphi...
Ideas?
You could first check the count on the Folders collection:
if Contacts.Folders.Count = 0 then
or
Contacts := Contacts.Folders.GetFirst;
if VarIsClear(Contacts) then
You could try this:
if IUnknown(Contacts) = nil then
//
var
x: string;
in code:
x:= format('%p%',[Pointer(TVarData(contacts).VDispatch)]);
if x = '00000000' then
'New Contact'
else
'open folder and search within this one'
Co-worker had the solution.. Thanks for your time :)

Resources