Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 01-17-2013, 09:19 AM
druski420 druski420 is offline
Registered User
 
Join Date: Jan 2013
Posts: 3
Question Script to take input box variable, search text file, write new file w/ result

I'm trying take a current script using:

#$language = "VBScript"
#$interface = "1.0"

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Dim fso, file, strIP1, strIP2, strIP3, strCAMNUM, strCAMNAME
set fso = createobject("scripting.filesystemobject")

strCAMNAME = crt.Dialog.Prompt(_
"Please specify Camera Name", _
"CAM NAME", _
"")
If strCAMNAME= "" Then Exit Sub
filename = ("" & strIP1 & "" & strIP2 & "-" & strCAMNUM & "")
filepath = "t:\configs\first cash\" & filename & ".cfg"
set objstream = fso.OpenTextFile(filepath, ForWriting, True)
objstream.writeline "The model name is <MODELNAME>."
objstream.close
end sub

(extra parts left out for brevity) and instead make 2 input boxes, 1 for site number and 1 for camera number, take that info from those variables, search a folder for a file named the site number, and search in that file for the line starting with the camera number. Finding that line should then write the info from that line into a new file.

Any assistance is appreciated.
Reply With Quote
  #2  
Old 01-17-2013, 09:47 AM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Hello druski420,

What part of the code is working, and what part in particular would you like assistance with?
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #3  
Old 01-17-2013, 09:50 AM
druski420 druski420 is offline
Registered User
 
Join Date: Jan 2013
Posts: 3
The whole script is working, I just have to manually input 5 different variables to build my files. What I'm trying to do is remove the need to manually input 5 variables, just input 2 and have the script pull the other 3 variables from a csv and plug into the file I'm building.
Reply With Quote
  #4  
Old 01-17-2013, 10:06 AM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Quote:
have the script pull the other 3 variables from a csv and plug into the file I'm building
Thanks for clarifying. You'd like VBScript code (not SecureCRT specific) that shows you how to search for text in a file and parse that text?

If my summary is correct, then I think the algorithm would be:
  • Read a file line by line
  • Search each line for text (probably using Instr, or possibly a regular expression depending on what you're trying to detect)
  • If Instr detects a match, write the line to the new file
If you agree with the basic algorithm and think I'm understanding your goal correctly, I'll see if we have any VBScript examples of reading files line by line, searching, using Instr, etc.. Since this part of the task doesn't sound SecureCRT specific, there may also be simple examples somewhere on the Internet.
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #5  
Old 01-17-2013, 11:03 AM
druski420 druski420 is offline
Registered User
 
Join Date: Jan 2013
Posts: 3
That is exactly what I had in mind.
Reply With Quote
  #6  
Old 01-17-2013, 02:30 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
The SecureCRT Scripting Manual has a robust example named ReadDataFromHostFile-SendCommandsFromCommandsFile.vbs in chapter 7.2 (pp. 90). The function Function ReadDataFromFile is very reusable.

After reading data from the file, the code interates over each line from the file (with For nIndex = 1 To nLineCount). That's where you'd probably want to use Instr.

I've attached ReadDataFromFile.txt in case having the example from the scripting manual helps. I'm also including a pseudocode mockup of what I think you could use from ReadDataFromFile, plus use of Instr.
Code:
Sub MainSub()
' Untested Pseudocode based on ReadDataFromFile.vbs

    Dim vLines()
    Dim nLineCount, nCommentLines
    If Not ReadDataFromFile(g_strDataFile, _
                            g_strComment, _
                            vLines, _
                            nLineCount, _
                            nCommentLines) Then
        MsgBox "No lines were found in file: " & vbcrlf & vbtab & g_strDataFile
        Exit Sub
    End If

    ' Iterate through each element of our vLines array 
    ' and search for string
    For nIndex = 1 To nLineCount
        ' Arrays are indexed at zero... so subtract...
        strFileText = vLines(nIndex - 1)

        If Instr(strFileTest, "String1", 1) Then 
	  WriteDataToFile(strFileText)
        ElseIf Instr(strFileTest, "String2", 1) Then
	  WriteDataToFile(strFileText)
        ElseIf Instr(strFileTest, "String3", 1) Then
	  WriteDataToFile(strFileText)
        Else
	  MsgBox strFileText & "doesn't match, not written to file"
        End If
    Next
End Sub


'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function ReadDataFromFile(strFile, _
                          strComment, _
                          ByRef vLines, _
                          ByRef nLineCount, _
                          ByRef nCommentLines)
' Returns True if the file was found.
'        strFile: IN  parameter specifying full path to data file.
'     strComment: IN  parameter specifying string that preceeded
'                    by 0 or more space characters will indicate
'                    that the line should be ignored.
'        vLines: OUT parameter (destructive) containing array
'                    of lines read in from file.
'    nLineCount: OUT parameter (destructive) indicating number
'                    of lines read in from file.
' nCommentLines: OUT parameter (destructive) indicating number
'                    of comment/blank lines found
'
'
    ' Check to see if the file exists... if not, bail early.
    If Not g_fso.FileExists(strFile) Then Exit Function

    ' Start of with a reasonable size for the array:
    ReDim vLines(5)

    ' Open a TextStream Object to the file...
    Set objTextStream = g_fso.OpenTextFile(strFile, 1, False)

    ' Used for detecting comment lines, a regular expression object
    Set re = New RegExp
    ' re.Pattern = "^[ \t]*(?:" & strComment & ")+.*$"
    re.Pattern = "(^[ \t]*(?:" & strComment & ")+.*$)|(^[ \t]+$)|(^$)"
    re.Multiline = False
    re.IgnoreCase = False

    ' Now read in each line of the file and add an element to
    ' the array for each line that isn't just spaces...
    nLineCount = 0
    nCommentLines = 0

    Do While Not objTextStream.AtEndOfStream
        strLine = ""

        ' Find out if we need to make our array bigger yet
        ' to accomodate all the lines in the file.  For large
        ' files, this can be very memory-intensive.
        If UBound(vLines) >= nLineCount Then
            Redim Preserve vLines(nLineCount + 5)
        End If

        strLine = Trim(objTextStream.ReadLine)

        ' Look for comment lines that match the pattern
        '   [whitespace][strComment]
        If re.Test(strLine) Then
            ' Line matches our comment pattern... ignore it
            nCommentLines = nCommentLines + 1
        Else
            vLines(nLineCount) = strLine
            nLineCount = nLineCount + 1
        End If
    Loop

    objTextStream.Close

    ReadDataFromFile = True
End Function
Attached Files
File Type: txt ReadDataFromFile.txt (4.8 KB, 411 views)
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
Reply

Tags
text file


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 01:37 PM.