' EV script 001 - New EV files for folder of data.vbs ' Example Echoview COM script downloaded from www.echoview.com ' Copyright Myriax Software, 2008 ' Created by Rowan James December 2008 ' For support, contact the Echoview support team ' This script mostly uses Echoview COM features from version 4.40, ' but was developed with 4.60, and may not work with older versions. ' Helps find bugs in the script itself option explicit MsgBox "Example script to create EV files for all files in a folder" & vbCrLf & "Version 1.2", vbInformation, "Echoview Example COM Script" ' Change History ' 1.1 -> 1.2 ' Add file naming method 2, and the option to select which is used ' 1.0 -> 1.1 ' Made usage of file extentions case insensitive ' Added example per-EV file action to export a line '----------------------------------------------------------------------------- ' Set default values for the script Const DefaultTemplate = "C:\Example\My Template.ev" Const DefaultDataPath = "" Const DefaultDataExtension = "EK5" Const DefaultEvFilePath = "" Const DefaultDataFilesPerEvFile = 30 Const DefaultEvFilePrefix = "Script Created File - " ' The last part of the EV file name can be generated by one of the following methods: ' 1 - Use the time of the first ping; may not work reliably for all data formats ' 2 - Use the name of the first data file Const FileNamingMethod = 2 ' For the example line export ' Be sure to change the name of the line to something that makes sense for your data/template ' Empty quotes means don't try to export a line Const LineToExportName = "" 'Const LineToExportName = "Fileset1: Sv Q1 telegrams T1 sounder-detected bottom" '----------------------------------------------------------------------------- ' We want these objects to be available all through the script Dim FSO Set FSO = CreateObject("Scripting.FileSystemObject") Dim EvApp Set EvApp = CreateObject("EchoviewCOM.EvApplication") EvApp.Minimize() '----------------------------------------------------------------------------- Sub QuitWithError(ErrorMessage) MsgBox ErrorMessage + vbCrLf + vbCrLf + "Exiting.", vbOk + vbError, "Error" WScript.quit 1 End Sub '----------------------------------------------------------------------------- ' We create a function (procedure) to make getting a folder name from the user a bit easier Function GetFolderName(DialogTitle, DefaultPath) ' We use the IE folder browser object Dim objDlg Set objDlg = CreateObject("Shell.Application") Dim objFileBrowser If FSO.FolderExists(DefaultPath) Then Set objFileBrowser = objDlg.BrowseForFolder(0, DialogTitle, &H1, DefaultPath) Else Set objFileBrowser = objDlg.BrowseForFolder(0, DialogTitle, &H1, 17) End If Dim FolderName ' We check the value we got back ' Here we use TypeName to detect the result. If InStr(1, TypeName(objFileBrowser), "Folder") > 0 Then On Error Resume Next FolderName = objFileBrowser.ParentFolder.ParseName(objFileBrowser.Title).Path ' Handle the case where a top-level (drive) folder was selected If Err <> 0 Then Err.Clear Dim tmp tmp = InStr(1, objFileBrowser.Title, ":") If tmp > 0 Then FolderName = Mid(objFileBrowser.Title, (tmp - 1), 2) End If End If On Error GoTo 0 Else ' We exit if no folder was selected QuitWithError "No folder selected." End If If Not FSO.FolderExists(FolderName) Then QuitWithError "Invalid folder selected." End If ' return the result GetFolderName = FolderName + "\" End Function '----------------------------------------------------------------------------- ' A function to browse for the template file Function GetTemplateFile Dim Result Result = InputBox("Please enter the full path and name of the template file you would like to use.","Template File", DefaultTemplate) If Result = "" Then ' User cancelled WScript.quit 0 End If If Not FSO.FileExists(Result) Then QuitWithError "The file you entered doesn't exist." End If If StrComp(FSO.GetExtensionName(Result), "ev", vbTextCompare) <> 0 Then QuitWithError "Template file must have .EV file extension." End If GetTemplateFile = Result End Function '----------------------------------------------------------------------------- ' Get a number from the user, or quit Function GetNumber(Prompt, Default) Dim Value Value = InputBox(Prompt, "Please enter a Number", Default) If Value = "" Then ' user pressed cancel or entered an empty string WScript.quit 1 End If If Not IsNumeric(Value) Then ' user enterd something other than an integer QuitWithError "You must enter a plain number in that box." End If GetNumber = CLng(Value) End Function '----------------------------------------------------------------------------- 'Pads the ev file names so that they are a set number of characters Function PadWithZeros(strPad, nTotalLength) PadWithZeros = string(nTotalLength - Len(strPad), "0") & strPad End Function '----------------------------------------------------------------------------- ' Return a formatted, padded date from a time Function FormatEvFileDate(DataDate) Dim strResult ' If you want a different date format, do your thing here ' remember that some characters can't be part of filenames, like "/" and "\" strResult = Year(DataDate) & "-" & PadWithZeros(Month(DataDate),2) & "-" & PadWithZeros(Day(DataDate),2) strResult = strResult & " " & PadWithZeros(Hour(DataDate),2) & "-" & PadWithZeros(Minute(DataDate),2) & "-" & PadWithZeros(Second(DataDate),2) FormatEvFileDate = strResult End Function '----------------------------------------------------------------------------- ' Save a file formatted with the start time of the first data file Sub SaveAndCloseFile(objEvFile, FilePath, NamePrefix) If objEvFile.Filesets.Item(0).DataFiles.Count = 0 Then QuitWithError "Tried to save an EV file with no data files" End If '--------------------------------------------------------------------- ' This is a good place to do any per-EV file work you want to do ' like exporting lines, or whatnot. ' Example: ' Export a line (if it exists) to a temporary location (will overwrite itself with each file) If Not LineToExportName = "" Then Dim TheLine Set TheLine = objEvFile.Lines.FindByName(LineToExportName) If Not TheLine Is Nothing Then TheLine.Export(EvFilePath & "Temp.evl") Else MsgBox "Could not find the requested line:" & vbCrLf & LineToExportName End If End If ' We use the start time of the first file to name the EV file '--------------------------------------------------------------------- ' Now we save the completed new file Dim FullFileName If FileNamingMethod = 1 Then '------------------------------------------------------------- ' File naming method 1: use the time of the first ping Dim StartTime StartTime = objEvFile.Filesets.Item(0).DataFiles.Item(0).FirstTime FullFileName = FilePath & NamePrefix & FormatEvFileDate(StartTime) & ".EV" ElseIf FileNamingMethod = 2 Then '------------------------------------------------------------- ' File naming method 2: use the name of the first data file Dim FirstDataFileName FirstDataFileName = FSO.GetBaseName(objEvFile.Filesets.Item(0).DataFiles.Item(0).FileName) FullFileName = FilePath & NamePrefix & FirstDataFileName & ".EV" End If ' Actually save the file to that name If objEvFile.SaveAs(FullFileName) = False Then QuitWithError "Could not save EV file " + FullFileName End If EvApp.CloseFile objEvFile End Sub '----------------------------------------------------------------------------- ' Now we start the setup phase; get the numbers and paths we need ' We use the function to get the data folder Dim DataFilePath DataFilePath = GetFolderName("Browse to the folder containing data files", DefaultDataPath) ' Ask the user how many data files they want to put in each EV file Dim NumberDataFilesToAdd NumberDataFilesToAdd = GetNumber("How many data files do you want in each EV file?", DefaultDataFilesPerEvFile) ' Ask the user what file extension they want to use Dim DataFileExt DataFileExt = InputBox("What data file extension do you want to add to the EV files?", "Data File Extension", DefaultDataExtension) If DataFileExt = "" Then QuitWithError "Need a data file extension." End If ' Now get the output EV file folder Dim EvFilePath EvFilePath = GetFolderName("Browse to the location you would like to create EV files in", DefaultEvFilePath) ' Now get the name to save EV files with Dim EvFilePrefix EvFilePrefix = InputBox("Please enter a name to give the EV files", "EV File Name", DefaultEvFilePrefix) If EvFilePrefix = "" Then QuitWithError "Need a file name." End If ' Ask for a template file Dim UseTemplate Dim TemplateFile UseTemplate = MsgBox("Would you like to use a template file?", vbYesNo + vbQuestion, "Use A Template?") If UseTemplate = vbYes Then TemplateFile = GetTemplateFile() End If '----------------------------------------------------------------------------- ' Show the configuration and give the option to cancel Dim ConfigMessage ConfigMessage = "Reading data files from: " & DataFilePath ConfigMessage = ConfigMessage & vbCrLf & "Writing EV files to: " & EvFilePath ConfigMessage = ConfigMessage & vbCrLf & "Adding " & NumberDataFilesToAdd & " data files per EV file." If UseTemplate = vbYes Then ConfigMessage = ConfigMessage & vbCrLf & "Using template file: " & TemplateFile Else ConfigMessage = ConfigMessage & vbCrLf & "Not using a template." End If If MsgBox(ConfigMessage, vbOkCancel + vbInformation, "Continue With This Configuration?") <> vbOk Then WScript.quit 1 End If '----------------------------------------------------------------------------- ' Create the source folder object Dim DataFileFolder Set DataFileFolder = FSO.GetFolder(DataFilePath) '----------------------------------------------------------------------------- ' The actual body of the script... Dim File Dim EvFile Set EvFile = Nothing For Each File In DataFileFolder.Files If StrComp(FSO.GetExtensionName(DataFilePath + File.Name), DataFileExt, vbTextCompare) = 0 Then ' We need a new EV file (haven't opened one, or we just closed it) If EvFile Is Nothing Then Set EvFile = EvApp.NewFile(TemplateFile) End If EvFile.Filesets.Item(0).DataFiles.Add(DataFilePath + File.Name) If EvFile.Filesets.Item(0).DataFiles.Count >= NumberDataFilesToAdd Then ' This file is full; any more data files are to be ' added to a new file SaveAndCloseFile EvFile, EvFilePath, EvFilePrefix Set EvFile = Nothing End If End If Next If Not EvFile Is Nothing Then ' The last file needs to be saved and closed (wasn't full) SaveAndCloseFile EvFile, EvFilePath, EvFilePrefix Set EvFile = Nothing End If EvApp.Restore()