Sign in to follow this  
Followers 0
KenNichols

_StringBetween Help Needed

12 posts in this topic

With the below code _StringBetween returns "(þÿ" it should return:

"(þÿ N i t r o P D F P r o f e s s i o n a l )"

or

"(þÿ M i c r o s o f t ® W o r d 2 0 1 0)"

I have also tried:

$txt = StringStripWS($txt, 8)

What am I missing?

#Include <String.au3>
#include <file.au3>
#include <array.au3>

$FilePath = FileOpenDialog("Select PDF", @DesktopDir & "\", "PDF (*.pdf)")
$file = FileOpen($FilePath, 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

$txt = FileRead($file)

$Producer = _StringBetween($txt, '/Producer','/')
;$Ascii = StringToASCIIArray($Producer[0])
;_ArrayDisplay($Ascii)
MsgBox(0,"PDF Info", $Producer[0])

[topic="21048"]New to AutoIt? Check out AutoIt 1-2-3![/topic] Need to make a GUI? You NEED KODA FormDesigner!

Share this post


Link to post
Share on other sites



What returns _ArrayDisplay ( $Producer ) ?


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

You have to keep in mind that often binary values contain null chars.

String searches generally end at the first null char ( which is assumed by what you've displayed, and the fact that when copying that kind of data, generally the null chars are replaced by spaces.


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

It Returns:

[0]| (þÿ

Ok.

What's the complete line where you search string ?


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

You have to keep in mind that often binary values contain null chars.

String searches generally end at the first null char ( which is assumed by what you've displayed, and the fact that when copying that kind of data, generally the null chars are replaced by spaces.

SmOke,

That makes sense, but shouldn't the StringStripWS take care of that?

Even when I use StringStripWS it returns the same value.


[topic="21048"]New to AutoIt? Check out AutoIt 1-2-3![/topic] Need to make a GUI? You NEED KODA FormDesigner!

Share this post


Link to post
Share on other sites

Ok.

What's the complete line where you search string ?

I search the entire PDF too much to paste.

$txt = FileRead($file)


[topic="21048"]New to AutoIt? Check out AutoIt 1-2-3![/topic] Need to make a GUI? You NEED KODA FormDesigner!

Share this post


Link to post
Share on other sites

SmOke,

That makes sense, but shouldn't the StringStripWS take care of that?

Even when I use StringStripWS it returns the same value.

No. Null is not a white space.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

No. Null is not a white space.

StringStripWS Remarks:

Whitespace includes Chr(9) thru Chr(13) which are HorizontalTab, LineFeed, VerticalTab, FormFeed, and CarriageReturn. Whitespace also includes the null string ( Chr(0) ) and the standard space ( Chr(32) ).

How else can I strip the nulls?


[topic="21048"]New to AutoIt? Check out AutoIt 1-2-3![/topic] Need to make a GUI? You NEED KODA FormDesigner!

Share this post


Link to post
Share on other sites

Not sure that actually stripped the Nulls though. Didn't in my test.

Anyway, I would just write something custom to do the leg work for me, maybe something like:

#Include <String.au3>
#include <file.au3>
#include <array.au3>

$FilePath = FileOpenDialog("Select PDF", @DesktopDir & "\", "PDF (*.pdf)")

$Producer = __myBinaryFileStringBetween($FilePath, '/Producer','/')
If IsArray($Producer) Then
    MsgBox(0,"PDF Info", $Producer[0])
Else
    MsgBox(16 + 262144, "Error", "Item not found")
EndIf


Func __myBinaryFileStringBetween($s_file, $s_start, $s_end, $v_case = -1)

    ; open in binary
    Local $h_file = FileOpen($s_file, 16)

    Local $s_data = Hex(FileRead($h_file))
    ; strip chr(0)
    $s_data = StringRegExpReplace($s_data, "(00)|([A-Za-z0-9]{2})", "$2")

    FileClose($h_file)

    ; revert back to string
    $s_data = String(BinaryToString("0x" & $s_data))

    ; return code just like you would
    Local $a_sbetween = _StringBetween($s_data, $s_start, $s_end, $v_case)
    Return SetError(@error, @extended, $a_sbetween)
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Not sure that actually stripped the Nulls though. Didn't in my test.

Anyway, I would just write something custom to do the leg work for me, maybe something like:

#Include <String.au3>
#include <file.au3>
#include <array.au3>

$FilePath = FileOpenDialog("Select PDF", @DesktopDir & "\", "PDF (*.pdf)")

$Producer = __myBinaryFileStringBetween($FilePath, '/Producer','/')
If IsArray($Producer) Then
    MsgBox(0,"PDF Info", $Producer[0])
Else
    MsgBox(16 + 262144, "Error", "Item not found")
EndIf


Func __myBinaryFileStringBetween($s_file, $s_start, $s_end, $v_case = -1)

    ; open in binary
    Local $h_file = FileOpen($s_file, 16)

    Local $s_data = Hex(FileRead($h_file))
    ; strip chr(0)
    $s_data = StringRegExpReplace($s_data, "(00)|([A-Za-z0-9]{2})", "$2")

    FileClose($h_file)

    ; revert back to string
    $s_data = String(BinaryToString("0x" & $s_data))

    ; return code just like you would
    Local $a_sbetween = _StringBetween($s_data, $s_start, $s_end, $v_case)
    Return SetError(@error, @extended, $a_sbetween)
EndFunc

I have never used StringRegExpReplace. I will have to do some homework to fully understand it.

The code you provide got me much closer to my desired result. With some tweaking I think it will be perfect. Thank You! You have been a huge help!


[topic="21048"]New to AutoIt? Check out AutoIt 1-2-3![/topic] Need to make a GUI? You NEED KODA FormDesigner!

Share this post


Link to post
Share on other sites

No problem, good luck.


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0