Siemens WinCC - Counting rows of a .csv file

Rob...

Lifetime Supporting Member
Join Date
Jul 2016
Location
Manchester
Posts
483
Been scratching my head at what I thought should be a relatively simple task.

I need to count how many rows a .csv file has, so I can later read data from the last row.

I have everything working, bar that. This is the VBS code I have so far to count the rows:

Code:
  Set fso = CreateObject("Scripting.FileSystemObject")
  fc = fso.OpenTextFile((Source),1,1).ReadAll
  RowArray = Split(fc,vbCrLf)
  NumberOfRows = CInt((UBound(RowArray)))
  	If NumberOfRows = 0 Then
		NumberOfRows = 1
	End If
  fso.close

Now this actually works, and produces the value I need. However, it throws Error 438 :- Object doesn't support this property or method.

Anyone see where I'm going wrong, or have a better solution?

Next will be to write it to also work with WinCE, but i'll cross that hurdle later.
 
Does the FileSystemObject have a .close method? Maybe it did in 2004 or later, but currently I think it does not (cf. this link)? If it does, it would start with an uppercase C.

I think [Set fso = Nothing] is what you want instead of fso.close.
 
Does the FileSystemObject have a .close method? Maybe it did in 2004 or later, but currently I think it does not (cf. this link)? If it does, it would start with an uppercase C.

I think [Set fso = Nothing] is what you want instead of fso.close.

That was it.... set to nothing.

I'd actually already written this in my function to read the csv... too long looing at the screen I think.

Code:
        Set fso = CreateObject("Scripting.FileSystemObject")
	fc = fso.OpenTextFile((Source),1,1).ReadAll
  	RowArray = Split(fc,vbCrLf)
  	NumberOfRows = CInt((UBound(RowArray)))
  	If NumberOfRows = 0 Then
		NumberOfRows = 1
	End If
  	Set fso = Nothing
 
I would add on beginning and end.

Dim fso

Dim fc
Dim RowArray
Dim NumberOfRows


'Error handling

On Error Resume Next





Set fso = CreateObject("Scripting.FileSystemObject")

fc = fso_OpenTextFile((Source),1,1).ReadAll


RowArray = Split(fc,vbCrLf)
NumberOfRows = CInt((UBound(RowArray)))
If NumberOfRows = 0 Then
NumberOfRows = 1
End If

Set fso = Nothing





'close file for next read / write

fc.Close
 
@Lare this is part of a larger function. It's on an snip of what I was having trouble with.

FYI anyone interested, this albeit janky method works in WinCE.

Code:
Set fso = CreateObject("FileCtl.File") 		
	fso.Open Source, 1	
	While fso.EOF = False
		Data=fso.LineInputString
		NumberOfRows = NumberOfRows + 1
	Wend	
	Set fso = Nothing
 
@Lare this is part of a larger function. It's on an snip of what I was having trouble with.

FYI anyone interested, this albeit janky method works in WinCE.

Code:
Set fso = CreateObject("FileCtl.File")         
    fso.Open Source, 1    
    While fso.EOF = False
        Data=fso.LineInputString
        NumberOfRows = NumberOfRows + 1
    Wend    
    Set fso = Nothing




(y). Some years ago I had problems for writing to same text file as it was not closed after first opening and writing. fc.close worked for that vbs coding.
 

Similar Topics

Hello colleagues, On a Siemens Multi panel (MP 377 12") of a machine (S7-300) at a customer's site, some parameters need to be changed that have...
Replies
7
Views
305
I thought I was nearly finished on this TIA Portal/s7-1212C project (famous last words)... Up until now, I'd developed the PLC/HMI such that the...
Replies
10
Views
1,557
Hi, I have a HMI which is a KTP1200 Basic. Let's suppose USER03 is logged in. How do I create a simple "Lock Screen" that appears if the screen...
Replies
2
Views
677
Hi, At the moment, If the user fails to provide a valid username/password, the image attached appears "Invalid password or user name. Logon has...
Replies
12
Views
1,576
Hello guys, Im using a siemens with WINCC in a computer and need to import to tag´s data fom a specific row in a SCV file, i have a working...
Replies
15
Views
1,692
Back
Top Bottom