VBS Readline - using instr(), to match data whilst ignoring extra spaces

Adrian

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.

Damien

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

VBS Readline-使用instr()来匹配数据,同时忽略多余的空格

来自分类Dev

VBS Readline-使用instr()来匹配数据,同时忽略多余的空格

来自分类Dev

How can I replace blank extra spaces (not white-spaces) with '-'?

来自分类Dev

javascript - How to reduce extra white spaces except line breaks

来自分类Dev

Ignoring/skipping tokens using std::cin

来自分类Dev

Const.EXTRA_DATA错误

来自分类Dev

Route using underscores instead of spaces

来自分类Dev

using $and with $match in mongodb

来自分类Dev

Using sscanf to read strings with white spaces in C

来自分类Dev

time data does not match format

来自分类Dev

VBscript和InStr函数

来自分类Dev

Swift instr或strpos等效

来自分类Dev

替代使用InStr

来自分类Dev

在VBA中定义InStr

来自分类Dev

MySql的选择,替换,INSTR

来自分类Dev

Oracle INSTR完全匹配

来自分类Dev

访问Instr Vba

来自分类Dev

使用 Instr 复制整行

来自分类Dev

出现 INSTR 的 SUBSTR

来自分类Dev

äüöß的VBS编码

来自分类Dev

want to alter field separator to allow spaces in particular data field

来自分类Dev

使用readline()?

来自分类Dev

Python .readline()

来自分类Dev

用json.dump引发JSONDecodeError(“ Extra data”,s,end)

来自分类Dev

Ignoring duplicated primary keys when exporting data from one database to another in SQL

来自分类Dev

Instr()中的溢出错误

来自分类Dev

VBA InStr始终返回0

来自分类Dev

使用InStr进行While循环

来自分类Dev

如何使用INSTR()和SUBSTR()