# $language = "VBScript" # $interface = "1.0" ' SaveSelectedTextToFile.vbs ' ' Last Modified: ' 25 Jan, 2023: ' - Utilize the FileSaveDialog() method instead of the FileOpenDialog() ' method. ' - When determining the default name for the TXT file to be written, ' use the protocol name if the RemoteAddress is empty (as in the case ' where we're connected to a local shell or Serial Protocol) along ' with a time/date tag. ' ' Description: ' If non-whitespace text is selected within the terminal screen, the user ' will be prompted for a location + filename in which to store the selected ' text as a CSV file. Multiple space characters within the data will be ' converted to a single comma character. ' ' Demonstrates: ' - How to use the Screen.Selection property to access text selected ' - within the terminal window. ' - How to use the Scripting.FileSystemObject to write data to a ' text file. ' - How to use RegExp's Replace() method to convert sequential space ' characters into a single comma character. ' ' ' g_nMode is used only if the user specifies a file that already exists, in ' which case the user will be prompted to overwrite the existing file, append ' to the existing file, or cancel the operation. Dim g_nMode Const ForWriting = 2 Const ForAppending = 8 ' Be "tab safe" by getting a reference to the tab for which this script ' has been launched: Set objTab = crt.GetScriptTab Set g_shell = CreateObject("WScript.Shell") Set g_fso = CreateObject("Scripting.FileSystemObject") SaveSelectedTextToCSVFile '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Sub SaveSelectedTextToCSVFile() ' Capture the selected text into a variable. The 'Selection' property ' is available in SecureCRT 6.1 and later. This line of code will cause ' an error if launched in a version of SecureCRT prior to v6.1. strSelection = objTab.Screen.Selection ' Check to see if the selection really has any text to save... we don't ' usually want to write out an empty file. If Trim(strSelection) = "" Then crt.Dialog.MessageBox "Nothing to save!" Exit Sub End If strTimeDate = GetCurDateTimeTag strBasename = objTab.Session.RemoteAddress If strbasename = "" Then strBasename = objTab.Session.Config.GetOption("Protocol Name") & _ "_" & strTimeDate & ".csv" Else strBasename = objTab.Session.RemoteAddress & _ "_" & strTimeDate & ".csv" End If strFilename = g_shell.SpecialFolders("MyDocuments") & "\" & strBasename Do strFilename = crt.Dialog.FileSaveDialog( _ "Save as CSV File", _ "Save", _ strFilename, _ "CSV Files (*.csv)|*.csv|Text Files (*.txt)|*.txt||") If strFilename = "" Then Exit Sub ' Do some sanity checking if the file specified by the user already ' exists... If g_fso.FileExists(strFilename) Then nResult = crt.Dialog.MessageBox( _ "Do you want to replace the contents of """ & _ strFilename & _ """ with the selected text?" & vbcrlf & vbcrlf & _ vbtab & "Yes = overwrite" & vbcrlf & vbcrlf & _ vbtab & "No = append" & vbcrlf & vbcrlf & _ vbtab & "Cancel = end script" & vbcrlf, _ "Replace Existing File?", _ vbYesNoCancel) Select Case (nResult) Case vbYes g_nMode = ForWriting Exit Do Case vbNo g_nMode = ForAppending Exit Do Case Else Exit Sub End Select Else g_nMode = ForWriting Exit Do End If Loop ' Automatically append a .csv if the filename supplied doesn't include ' any extension. If g_fso.GetExtensionName(strFilename) = "" Then strFilename = strFilename & ".csv" End If ' Replace instances of one or more space characters with a comma. Use ' the VBScript built-in RegExp object's Replace method to perform this ' task Set re = New RegExp ' The pattern below means "one or more sequential space characters" re.Pattern = "[ ]+" re.Global = True re.MultiLine = True strCSVData = re.Replace(strSelection, ",") Do On Error Resume Next Set objFile = g_fso.OpenTextFile(strFilename, g_nMode, True, -1) nError = Err.Number strErr = Err.Description On Error Goto 0 If nError = 0 Then Exit Do ' Display a message indicating there were problems opening ' the file. nResponse = crt.Dialog.MessageBox( _ "Failed to open """ & strFilename & """ (" & nError & "): " & _ vbcrlf & vbtab & strErr & vbcrlf & vbcrlf & _ "Check to see if the file is already open in another " & _ "application and make sure you have permissions to " & _ vbcrlf & "edit the file and create new files within the " & _ "destination folder.", _ "Save Operation Failed", _ vbRetryCancel) If nResponse <> vbRetry Then Exit Sub Loop objFile.Write strCSVData & vbcrlf objFile.Close g_strMode = "Wrote" If g_nMode = ForAppending Then g_strMode = "Appended" crt.Dialog.MessageBox _ g_strMode & " " & Len(strSelection) & " bytes to file:" & vbcrlf & _ vbcrlf & strFilename ' Now open the CSV file in the default .csv file application handler... g_shell.Run chr(34) & strFilename & chr(34) End Sub '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Function GetCurDateTimeTag() ' Use WMI to get at the current time values Set objWMIService = GetObject("winmgmts:\\.\root\cimv2") Set colItems = objWMIService.ExecQuery( _ "Select * from Win32_OperatingSystem") For Each objItem In colItems strLocalDateTime = objItem.LocalDateTime Next ' strLocalDateTime has the following pattern: ' 20111013093717.418000-360 ' That is: YYYYMMDDHHMMSS.MILLIS(zone); Take the left-most 18 digits... strCurDateTimeTag = Left(strLocalDateTime, 18) ' Return the current date/time tag as value of this function GetCurDateTimeTag = strCurDateTimeTag End Function