Jump to content

How Do I Extract ?


Recommended Posts

Hi,

Within this document.....bike.txt , I have some text that reads.......Subdomain bike.orps.host22.com has. I need a function that looks for "Subdomain" and "has". Once it finds it, it extracts whatever is in between the "Subdomain" and "has" and places it in a clipboard ready to paste. I have a function already written that needs to be altered. Could you take look at it and help me out ? Thanks

Func _GetTitle()
    Sleep(250)
    Opt("WinTitleMatchMode", 2)
    $text = WinGetText("Notepad")
    $posStart = StringInStr($text,'Subdomain')
    $posEnd   = StringInStr($text,'has')

    If $posStart = 0 or $posEnd = 0 Then
        MsgBox(0,'ERROR','This File doesnt Contain a Title')
        $result = '' ; This File doesnt Contain a Title 
    Else
        $result = StringMid($text,$posStart+9,$posEnd-($posStart+3))
    EndIf
    $result= StringLower($result)
    Return $result
EndFunc 

Func _SendTitle($Title)
    Sleep(250)
    ControlSend("Notepad",'','', String($Title))
    ControlSend('[Class:Notepad]', '', 'Edit1', $Title)
    Sleep(250)  
EndFunc
Link to comment
Share on other sites

I'm not sure why you use notepad for your code.

If this is a must, then my example isn't suitable for you.

I took the liberty of not using notepad.

The search is done in memory.

Result is put on the clipboard.

It's a quick and dirty approach, but it does the trick

#include <file.au3>
Global $hFile
Global $SomeTxt
Global $StartPos, $EndPos
Global $Find_This, $Find_This_Too
Global $MyResult

$Find_This = "Subdomain"
$Find_This_Too = "has"
$hFile = FileOpen("Bike.txt",0)    ;Open file in read mode

If @error Then
    MsgBox(0,"Warning", "Unable to open file")
    Exit
EndIf

$SomeTxt = FileRead($hFile)    ;reads the complete file
FileClose($hFile)
$StartPos = StringInStr($SomeTxt,$Find_This) + StringLen($Find_This) ;finds the first string and puts the pos direct behind the found string

If $StartPos = 0 Then        ;means $Find_This not found, no need to continue
    MsgBox(0,"Warning", "Unable to find: " & $StartPos)
    Exit
Else
    ConsoleWrite("$StartPos= " & $StartPos & @CRLF)        ;not needed just added to check if code works
EndIf

$EndPos = StringInStr($SomeTxt,$Find_This_Too,0,1,$StartPos) ;finds the first occurence of $Find_This_Too after $Start_pos

If $EndPos = 0 Then        ;means $Find_This_Too was not found, no need to continue
    MsgBox(0,"Warning", "Unable to find: " & $EndPos)
    Exit
Else
    ConsoleWrite("$EndPos= " & $EndPos & @CRLF)        ;not needed just added to check if code works
EndIf

$MyResult = (StringMid($SomeTxt,$StartPos,$EndPos-$StartPos))
$MyResult = StringStripWS($MyResult,3)

If StringLen($MyResult) <> 0 Then
    MsgBox(0,"Finished", "You were looking for: " & $MyResult    )
    ClipPut($MyResult)    ;Put the result on the clipboard
Else
    MsgBox(0,"Warning", "Unable to find the wanted string")
EndIf

[[EDIT]] Removed the word "quick", since there are faster solutions >_<

Edited by Scriptonize

If you learn from It, it's not a mistake

Link to comment
Share on other sites

You may want to have look at the _StringBetween() function as it does what you want to do

$MyResult = _StringBetween($SomeTxt,"Subdomain","has")

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

This is what I got.......

unable to find.pdf

In regards to ($MyResult = _StringBetween($SomeTxt,"Subdomain","has"))

I've tried to place it within the code but I kep getting errors.

#include <file.au3>
Global $hFile
Global $SomeTxt
Global $StartPos, $EndPos
Global $Find_This, $Find_This_Too
Global $MyResult

$Find_This = "Subdomain"
$Find_This_Too = "has"
$hFile = FileOpen("Bike.txt",0)    ;Open file in read mode

If @error Then
    MsgBox(0,"Warning", "Unable to open file")
    Exit
EndIf

$SomeTxt = FileRead($hFile)    ;reads the complete file
FileClose($hFile)
$StartPos = StringInStr($SomeTxt,$Find_This) + StringLen($Find_This) ;finds the first string and puts the pos direct behind the found string

If $StartPos = 0 Then        ;means $Find_This not found, no need to continue
    MsgBox(0,"Warning", "Unable to find: " & $StartPos)
    Exit
Else
    ConsoleWrite("$StartPos= " & $StartPos & @CRLF)        ;not needed just added to check if code works
EndIf

$EndPos = StringInStr($SomeTxt,$Find_This_Too,0,1,$StartPos) ;finds the first occurence of $Find_This_Too after $Start_pos

If $EndPos = 0 Then        ;means $Find_This_Too was not found, no need to continue
    MsgBox(0,"Warning", "Unable to find: " & $EndPos)
    Exit
Else
    ConsoleWrite("$EndPos= " & $EndPos & @CRLF)        ;not needed just added to check if code works
EndIf

$MyResult = _StringBetween($SomeTxt,"Subdomain","has")

If StringLen($MyResult) <> 0 Then
    MsgBox(0,"Finished", "You were looking for: " & $MyResult    )
    ClipPut($MyResult)    ;Put the result on the clipboard
Else
    MsgBox(0,"Warning", "Unable to find the wanted string")
EndIf
Link to comment
Share on other sites

Scriptonize your freakin AWESOME. All I did to your script was lower case the "B" in bike.txt and placed the path "D:\Objects To Burn\Documents\bike.txt" and it worked.

$Find_This = "Subdomain"
$Find_This_Too = "has"
$hFile = FileOpen("D:\Objects To Burn\Documents\bike.txt",0)    ;Open file in read mode
Link to comment
Share on other sites

$Find_This = "Subdomain"
$Find_This_Too = "has"
$hFile = FileOpen("D:\Objects To Burn\Documents\bike.txt",0)    ;Open file in read mode

There should be no need to change the first character of the file name into lower/upper case.

We are working on windows aren't we?

Unix/Linux do make a difference between file names with upper/lower case,

Windows however doesn't.

Anyway, glad I could help.

PS:

Instead of using the ["code"] tags at this forum, you should use the autoit code tags

[autoit][/autoit]
in here.

It makes your code better to read and also if you click on highlighted functions the auto help pop's up.

Edited by Scriptonize

If you learn from It, it's not a mistake

Link to comment
Share on other sites

  • 4 weeks later...

Scriptonize,

Thanks for the script last month, I've done a few adjustments since then. The primary purpose of this script you wrote was to extract and paste onto the clipboard. I would like to know if you could show me how to make a small alteration? How can we reverse this script now instead of extracting and then pasting on the clipboard, how can we rewrite it to take whats on the clipboard and paste within the variables? The current script is below..........

#include <file.au3>
Global $hFile
Global $SomeTxt
Global $StartPos, $EndPos
Global $Find_This, $Find_This_Too
Global $MyResult

$Find_This = "<script type="
$Find_This_Too = "</script>"
$hFile = FileOpen("D:\Documents and Settings\Taevon Jones\Desktop\HOME-MASTER-PHP ",0)    ;Open file in read mode

If @error Then
    MsgBox(0,"Warning", "Unable to open file")
    Exit
EndIf

$SomeTxt = FileRead($hFile)    ;reads the complete file
FileClose($hFile)
$StartPos = StringInStr($SomeTxt,$Find_This) + StringLen($Find_This) ;finds the first string and puts the pos direct behind the found string

If $StartPos = 0 Then        ;means $Find_This not found, no need to continue
    MsgBox(0,"Warning", "Unable to find: " & $StartPos)
    Exit
Else
    ConsoleWrite("$StartPos= " & $StartPos & @CRLF)        ;not needed just added to check if code works
EndIf

$EndPos = StringInStr($SomeTxt,$Find_This_Too,0,1,$StartPos) ;finds the first occurence of $Find_This_Too after $Start_pos

If $EndPos = 0 Then        ;means $Find_This_Too was not found, no need to continue
    MsgBox(0,"Warning", "Unable to find: " & $EndPos)
    Exit
Else
    ConsoleWrite("$EndPos= " & $EndPos & @CRLF)        ;not needed just added to check if code works
EndIf

$MyResult = (StringMid($SomeTxt,$StartPos,$EndPos-$StartPos))
$MyResult = StringStripWS($MyResult,3)

If StringLen($MyResult) <> 0 Then
    MsgBox(0,"Finished", "You were looking for: " & $MyResult    )
    ClipPut($MyResult)    ;Put the result on the clipboard
Else
    MsgBox(0,"Warning", "Unable to find the wanted string")
EndIf

I'm not sure why you use notepad for your code.

If this is a must, then my example isn't suitable for you.

I took the liberty of not using notepad.

The search is done in memory.

Result is put on the clipboard.

It's a quick and dirty approach, but it does the trick

#include <file.au3>
Global $hFile
Global $SomeTxt
Global $StartPos, $EndPos
Global $Find_This, $Find_This_Too
Global $MyResult

$Find_This = "Subdomain"
$Find_This_Too = "has"
$hFile = FileOpen("Bike.txt",0)    ;Open file in read mode

If @error Then
    MsgBox(0,"Warning", "Unable to open file")
    Exit
EndIf

$SomeTxt = FileRead($hFile)    ;reads the complete file
FileClose($hFile)
$StartPos = StringInStr($SomeTxt,$Find_This) + StringLen($Find_This) ;finds the first string and puts the pos direct behind the found string

If $StartPos = 0 Then        ;means $Find_This not found, no need to continue
    MsgBox(0,"Warning", "Unable to find: " & $StartPos)
    Exit
Else
    ConsoleWrite("$StartPos= " & $StartPos & @CRLF)        ;not needed just added to check if code works
EndIf

$EndPos = StringInStr($SomeTxt,$Find_This_Too,0,1,$StartPos) ;finds the first occurence of $Find_This_Too after $Start_pos

If $EndPos = 0 Then        ;means $Find_This_Too was not found, no need to continue
    MsgBox(0,"Warning", "Unable to find: " & $EndPos)
    Exit
Else
    ConsoleWrite("$EndPos= " & $EndPos & @CRLF)        ;not needed just added to check if code works
EndIf

$MyResult = (StringMid($SomeTxt,$StartPos,$EndPos-$StartPos))
$MyResult = StringStripWS($MyResult,3)

If StringLen($MyResult) <> 0 Then
    MsgBox(0,"Finished", "You were looking for: " & $MyResult    )
    ClipPut($MyResult)    ;Put the result on the clipboard
Else
    MsgBox(0,"Warning", "Unable to find the wanted string")
EndIf

[[EDIT]] Removed the word "quick", since there are faster solutions :D

Edited by styles3000
Link to comment
Share on other sites

ClipGet instead of ClipPut

Dantay,

Thanks for your input. I change the script from ClipPut to ClipGet. But I recieved an error message.....

>"D:\Program Files\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /prod /ErrorStdOut /in "D:\Objects To Burn\Adsense\Auto IT Scripts\webhost extract.au3" /autoit3dir "D:\Program Files\AutoIt3" /UserParams    
+>13:25:15 Starting AutoIt3Wrapper v.1.10.1.14    Environment(Language:0409  Keyboard:00000409  OS:WIN_XP/Service Pack 3  CPU:X86  ANSI)
>Running AU3Check (1.54.14.0)  from:D:\Program Files\AutoIt3
D:\Objects To Burn\Adsense\Auto IT Scripts\webhost extract.au3(42,22) : ERROR: ClipGet() [built-in] called with wrong number of args.
    ClipGet($MyResult)
~~~~~~~~~~~~~~~~~~~~~^
D:\Objects To Burn\Adsense\Auto IT Scripts\webhost extract.au3 - 1 error(s), 0 warning(s)
!>13:25:15 AU3Check ended.rc:2
+>13:25:24 AutoIt3Wrapper Finished
>Exit code: 0    Time: 10.501

Here's my script....

#include <file.au3>
Global $hFile
Global $SomeTxt
Global $StartPos, $EndPos
Global $Find_This, $Find_This_Too
Global $MyResult

$Find_This = "<script type="
$Find_This_Too = "</script>"
$hFile = FileOpen("D:\Documents and Settings\Taevon Jones\Desktop\HOME-MASTER-PHP ",0)    ;Open file in read mode

If @error Then
    MsgBox(0,"Warning", "Unable to open file")
    Exit
EndIf

$SomeTxt = FileRead($hFile)    ;reads the complete file
FileClose($hFile)
$StartPos = StringInStr($SomeTxt,$Find_This) + StringLen($Find_This) ;finds the first string and puts the pos direct behind the found string

If $StartPos = 0 Then        ;means $Find_This not found, no need to continue
    MsgBox(0,"Warning", "Unable to find: " & $StartPos)
    Exit
Else
    ConsoleWrite("$StartPos= " & $StartPos & @CRLF)        ;not needed just added to check if code works
EndIf

$EndPos = StringInStr($SomeTxt,$Find_This_Too,0,1,$StartPos) ;finds the first occurence of $Find_This_Too after $Start_pos

If $EndPos = 0 Then        ;means $Find_This_Too was not found, no need to continue
    MsgBox(0,"Warning", "Unable to find: " & $EndPos)
    Exit
Else
    ConsoleWrite("$EndPos= " & $EndPos & @CRLF)        ;not needed just added to check if code works
EndIf

$MyResult = (StringMid($SomeTxt,$StartPos,$EndPos-$StartPos))
$MyResult = StringStripWS($MyResult,3)

If StringLen($MyResult) <> 0 Then
    MsgBox(0,"Finished", "You were looking for: " & $MyResult    )
    ClipGet($MyResult)    ;Put the result on the clipboard
Else
    MsgBox(0,"Warning", "Unable to find the wanted string")
EndIf
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...