Renam File based on Textfile Input

  • 46 Views
  • Last Post 09 September 2020
AmiAmaterasu posted this 08 September 2020

Hey guys,

Im trying to write a script, which automatically renames the file based on the input of a textfile and the OCZ Zone.

So the idea is as follows:

The OCR Zone gets parsed for the ABN by the script and compares it to a textfile. If part of the ocr zone matches a line in the textfile, it will create a new variable which contains the associated VendorID for the ABN. This variable can now be used to rename the file accordingly.

e.g.
The textfile could look like this
51 824 753 556=K67890
41 824 753 556=K54321
...
41 824 753 556 is found, so the value of the new variable (e.g. filename) will be set as K54321 and can now be used to rename the file.

The OCR Zone is set quite large as we get numerous invoices with different layouts so the position of the ABN varies.

I tried to write a script, but as I've never used VBScript before it doesnt quite work well. Used part of https://forum.scanshare.com/thread/script-extract-data-from-invoice/ as you can see.

I appreciate your help.

ocrText = Metadata.Values("OCR_ZONE01")
VendorNumber = Metadata.Values("VendorID")

Dim Target, Target2
Target = ".*(ABN: |ABN:|Tax Number: |A.B.N. |A.B.N.|ABN |ABN|abn:|abn: |A.B.N |)\$*(([0-9]{2} [0-9]{3} [0-9]{3} [0-9]{3})|([0-9]{11}))"

Dim arrLines
arrLines = Split(ocrText, "\r\n")

call Metadata.SetValues("MY_SUPPLIER_ABN", "## NOT FOUND ##")

Dim matchedValue

For Each strline in arrLines
    match = GetFirstMatch(target, strline)
    If match <> "" Then
        call Metadata.SetValues("MY_SUPPLIER_ABN", match)
    End If
Next

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fspenTextFile(C:\ProgramData\Scanshare\Test.txt)

Do Until f.AtEndOfStream
  LineTxt = WScript.Echo f.ReadLine
  search="="
  Business=left (LineTxt, instr(LineTxt, search)-1)
  Vendor=right (LineTxt, len(LineTxt)-instrrev (LineTxt, search))
  if  Business = MY_SUPPLIER_ABN then
    Vendor = VendorNumber
  end if

f.Close

Function GetFirstMatch(PatternToMatch, StringToSearch)
    Dim objRE , CurrentMatch, objMatch

    Set objRE  = New RegExp
    objRE.Pattern = PatternToMatch
    objRE.IgnoreCase = True
    objRE.Global = False

    Set objMatch = objRE.Execute(StringToSearch)
   
    GetFirstMatch = ""
    If objMatch.Count = 1 Then

        GetFirstMatch = objMatch.Item(0).Submatches(1)
    End If
       
    Set objRE  = Nothing
End Function

luca.scarpati posted this 09 September 2020

Hi,

 

your script is a bit confusing and appears to be copied&pasted in various parts...but don't worry it's normal if you approach for the first time smile.

 

However your purpose is totally feasible with Scanshare application using a script.

Attached you will find the changes to your script that make it work, obviously it is a test and it works according to the "text" that we have inserted in the variable "ocrtext".

You will easily get it used according to your needs innocent.

 

Have a nice afternoon!

 

Best regards,

Luca

Attached Files

Close