Hi guys,
I'm not very good when it comes to VB scripting, trying to learn it by doing at the moment, so I'd appreciate some help!
I took a sample script from Siemens and made some small changes to implement some long term archiving in WinCC Advanced V13, where the customer can select Year,Month,Day and load the archive into a trend. It works fine the first time I call the script, but when I change the date and run it again it seems to not fully complete the script. I've looked in the folder where logs are recorded and can see that the last two files were never deleted.
Below is the original script taken from Siemens:
I'm not very good when it comes to VB scripting, trying to learn it by doing at the moment, so I'd appreciate some help!
I took a sample script from Siemens and made some small changes to implement some long term archiving in WinCC Advanced V13, where the customer can select Year,Month,Day and load the archive into a trend. It works fine the first time I call the script, but when I change the date and run it again it seems to not fully complete the script. I've looked in the folder where logs are recorded and can see that the last two files were never deleted.
Below is the original script taken from Siemens:
Code:
Sub Load_Archive(ByRef StoragePath)
Dim fso, FName, Variable_Date, Variable_Time
Dim ValueYear, ValueMonth, ValueDay, ValueHour, FileName, DataFileName
'Hilfsvariablen um nachfolgend den Archivierungspfad zusammen zu stellen
'Auxiliary variable to combine the storage path
ValueYear = SmartTags("Selection_Year")
ValueMonth = SmartTags("Selection_Month")
ValueDay = SmartTags("Selection_Day")
ValueHour = SmartTags("Selection_Hour")& "0.csv"
'Archivierungspfad auswählen
'Compose storage path
FName = StoragePath & "Year_" & ValueYear & "\" & "Month_" & ValueMonth & "\" & "Day_" & ValueDay & "\"
'Name der Datei, die geladen werden soll
'Name of the data file to be loaded
FileName = "Press_01_" & ValueHour
'Dateizugriff
'File access
Set fso = CreateObject("Scripting.FileSystemObject")
'Überprüfen ob der vorgegebene Archivname vorhanden ist
'Check if the predefined archiv exists
DataFileName = FName & FileName
If Not fso.FileExists(DataFileName) Then
'Datei nicht vorhanden -> Systemmeldung ausgeben / Projektierte "Textbox" einblenden
'Data file not available -> show system alarm / show "Textbox"
'ShowSystemAlarm "Bitte überprüfen Sie das vorgegebene Archivdatum"
'ShowSystemAlarm "Recheck the specific date of the Archive data file"
SetBit "TrendView_Visible" 'Im "Bild 2" wird ein Meldetext eingeblendet / In "Screen 2" there will be show a message text
Else
'Start des Archivs "Data_Logs_Trend_View"
'Start of the archive "Data_Logs_Trend_View"
StartLogging hmiDataLog, "Data_Logs_Trend_View"
'Solange das Folgearchiv "Data_Logs_Trend_View1.csv" nicht vorhanden ist -> Warten!
'Waiting until the subsequent archive "Data_Logs_Trend_View1.csv" is available
While Not fso.FileExists (StoragePath & "Data_Logs_Trend_View1.csv")
Wend
'Folgearchiv "Data_Logs_Trend_View1.csv" in das Archiv "Temp_Data_Logs_Trend_View.csv" kopieren
'Copy the subsequent archives "Data_Logs_Trend_View1.csv" to the archive "Temp_Data_Logs_Trend_View.csv"
fso.CopyFile StoragePath & "Data_Logs_Trend_View1.csv", StoragePath & "Temp_Data_Logs_Trend_View.csv"
'Solange das Archiv "Temp_Data_Logs_Trend_View.csv" nicht vorhanden ist -> Warten!
'Waiting until the archive "Temp_Data_Logs_Trend_View.csv" is available!
While Not fso.FileExists (StoragePath & "Temp_Data_Logs_Trend_View.csv")
Wend
'Archivierung des Archivs "Data_Logs_Trend_View" stoppen.
'Stop logging to the archive "Data_Logs_Trend_View"
StopLogging hmiDataLog, "Data_Logs_Trend_View"
'Alle Archive schließen
'Close all logs
CloseAllLogs
'Für die Initialisierung der Archive wird eine gewisse Zeit benötigt. Es kann ansonsten zu einem Zugriffehler kommen,
'wenn das System auf das Archiv zugreifen will. -> Wartezeit
'A certain waiting time is required for the initialization of the archives
'to avoid access conflicts
Dim Counter1
Counter1 = 0
While Counter1 < 60000
Counter1 = Counter1 + 1
Wend
'Das Archiv "Data_Logs_Trend_View0.csv", "Data_Logs_Trend_View1.csv" und "Temp_Data_Logs_Trend_View.csv"
'kann jetzt gelöscht werden.
'The archives "Data_Logs_Trend_View0.csv", "Data_Logs_Trend_View1.csv" and "Temp_Data_Logs_Trend_View.csv"
'can be deleted now
fso.DeleteFile StoragePath & "Data_Logs_Trend_View0.csv"
fso.DeleteFile StoragePath & "Data_Logs_Trend_View1.csv"
fso.DeleteFile StoragePath & "Temp_Data_Logs_Trend_View.csv"
'Wartezeit
'Waiting time
Dim Counter2
Counter2 = 0
While Counter2 < 60000
Counter2 = Counter2 + 1
Wend
'Kopiert den Inhalt des angewählten Archivs in das Archiv für die Trendkurve
'Copy the content from the selected archive to the archive for the trend view
fso.CopyFile DataFileName , StoragePath & "Data_Logs_Trend_View0.csv", True
'Alle Archive wieder öffnen
'Open all logs
OpenAllLogs
'Starten des Archivs "Archive_01"
'Start logging to archive "Archive_01"
StartLogging hmiDataLog, "Archive_01"
End If
'Die Kurvenanzeige auf den gewählten Zeitraum setzen
'Set the "Trend view" to the selected time
Variable_Date = SmartTags("Selection_Day") & " " & SmartTags("Selection_Month") & " " & SmartTags("Selection_Year")
Variable_Time = SmartTags("Selection_Hour") & ":30:00" ' Durch die Vorgabe der "Minute" (30) wird die Mitte der Stunde in der Kurvenanzeige angezeigt
SmartTags("TrendView_ExternalTime") = CDate(Variable_Date & " " & Variable_Time)
'Verwendeten Speicher wieder freigeben
'Free used storage
Set fso = Nothing
End Sub