Question

I'm trying to find a way to enhance the reliability of my script. It already works but can be thrown off with a simple extra space in the imported text file.

So I'd like to change my script to Readline if I can find a way to do something like:

Example of text in the .txt file:

    FLIGHTS OVER  TUSKY  PLEASE FILE: 
        AT OR WEST OF A LINE   RBV..LLUND..BAYYS..PUT..DIRECT
    FLIGHTS OVER EBONY PLEASE FILE:
        AT OR WEST OF A LINE   RBV..LLUND..BAYYS..PUT..DIRECT

I know the following doesn't work but if there was a simple modification this would be good.

set WshShell = WScript.CreateObject("WScript.Shell")
Return = WshShell.Run("C:\Downloads\software\putty.exe -load "testing")

set objFSO = CreateObject("Scripting.FileSystemObject") 
set objFile = objFSO.OpenTextFile("C:\Users\AW\Desktop\Entries1.txt") 

strLine = objFile.ReadAll

If InStr(strLine1, "OVER  TUSKY  PLEASE") and InStr(strLine2, "BAYYS..PUT..DIRECT") Then
    trans307="TUSKY"
    ind306="4"

WHAT I'M USING NOW: I edit the text file in notepad++ to FIND & REPLACE "\n" with "" and "\r" with " " and then it's all one text string and I search for strings within that string.

If InStr(strLine, "FLIGHTS OVER  TUSKY  PLEASE FILE: AT OR WEST OF A LINE      ..RBV..LLUND..BAYYS..PUT..DIRECT") _
or InStr(strLine, "FLIGHTS OVER  TUSKY  PLEASE FILE: AT OR WEST OF A LINE      RBV..LLUND..BAYYS..PUT...DIRECT") Then
    trans308C="TUSKY"
    ind308C="4"

Problem: If the creators of the text file put another space " " anywhere in this line "AT OR WEST OF A LINE RBV..LLUND..BAYYS..PUT..DIRECT" the script will not identify the string. In the above example I have had to create another or InStr(strLine, "") statement with an extra space or with a couple of dots.

UPDATE: I will try something like:

set objFSO = CreateObject("Scripting.FileSystemObject") 
set objFile = objFSO.OpenTextFile("C:\Users\AW\Desktop\Entries1.txt") 

strLine1 = objFile.Readline(1)
strLine2 = objFile.Readline(2)

If InStr(strLine1, "FLIGHTS OVER  TUSKY") and InStr(strLine2, "RBV..LLUND..BAYYS..PUT..DIRECT") Then
    trans1="TUSKY"
    ind1="4"

and see if I can get that to read 2 lines at a time, and loop through the text file.

Was it helpful?

Solution

If you're scared of regex and looking for an alternative, you could create a clunky function to add to your script. Based on your samples, it would seem that fullstops are also never normally used for normal purposes and tend to represent spaces. (I would recommend using Regex instead!)

Using these presumptions, you could create a clunky function like this, that looks for fullstops, and converts them to spaces, removing extra spaces.. Obviously, this relies heavily on your input source files not changing too much - you really should be using a regex to work this stuff out properly.

You could test for the basic expected results using something like the function below. For example say you had a line of text set in firLine with multiple spaces or fullstops, the function would recognize this:

firLine = "THIS.IS.A.TEST..YOU...SEE      MULTIPLE SPACES"
if instr(sanitize(firLine),"THIS IS A TEST YOU SEE MULTIPLE SPACES") then
       wscript.echo "Found it"
End If

Here's the clunky function that you could just paste at the end of your script:

Function sanitize(srStr)
    Dim preSanitize, srC, spaceMarker
    preSanitize = ""

    for srC = 1 to len(srStr)
        if mid(srStr, srC, 1) = "." then 
            preSanitize = preSanitize & " "
        else
            preSanitize = preSanitize & mid(srStr, srC, 1)
        End If

    spaceMarker = false
    sanitize = ""

    for srC = 1 to len(preSanitize)
        If mid(preSanitize, srC, 1) = " " then
            if spaceMarker = false then 
                sanitize = sanitize & mid(preSanitize, srC, 1)
                spaceMarker = true          
            End If
        else
            sanitize = sanitize & mid(preSanitize, srC, 1)
            spaceMarker = false
        End If
    Next


End Function

OTHER TIPS

InStr() is a good tool for checking whether a strings contains a fixed/literal string or not. To allow for variation, you should use Regular Expressions (see this or that).

First of all, however, you should work on your specs. Describe in plain words and with some samples what you consider (not) to be a match.

E.g.: A string containing the words "FLIGHTS", "OVER", and "TUSKY" in that order with at least one space in between is a match - "FLIGHTS OVER TUSKY", "FLIGHTS OVER TUSKY"; "FLIGHTS OVER TUSKANY" is a 'near miss' - what about "AIRFLIGHTS OVER TUSKY"?

GREAT NEWS! I finally figured out how to do this.

Here is a snippet from "Entries1.txt"

FLIGHTS OVER BRADD KANNI PLEASE FILE:
VIA J174.RIFLE..ACK..DIRECT
OR RBV.J62.ACK..DIRECT
FLIGHTS OVER KANNI WHALE PLEASE FILE:
VIA J174.RIFLE..ACK..DIRECT OR
FLIGHTS OVER WHALE PLEASE FILE:"
ETC, ETC

set WshShell = WScript.CreateObject("WScript.Shell")
set objFSO = CreateObject("Scripting.FileSystemObject") 
set objFile = objFSO.OpenTextFile("C:\Users\AW\Desktop\Entries1.txt") 

Do until objFile.AtEndOfStream
firLine = objFile.ReadLine

If InStr(firLine, "FLIGHTS OVER  KANNI  WHALE  PLEASE") Then
    secLine = objFile.ReadLine
If InStr(secLine, "J174.RIFLE..ACK..DIRECT") Then
  'I'm going to change the below once I piece it all together.
    WScript.Echo "works" 
    Else WScript.Echo "Not found"

'cut, paste and modify all my "IF" statements below

End If
End If
loop
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top