Jump to content

Search for "specific" keyword in files


Recommended Posts

I am trying to search a .txt file for a specific keyword.

I have a text file with this text:

"02-26-06 05:08PM 8131799 sdat4706.exe"

I want to create a variable wich contains the text sdat****.exe

For an example i want to search for sdat + the next 4 characters. Only the last 4 charachters/numbers are every week a different number.

I have tried stringsplit and stringinstr etc. But i am still a noob at AutoIt and i would apreciate if you guys could give me a helping hand.

Link to comment
Share on other sites

  • Moderators

Here's a shot, there's a few ways of doing it though, I'll post another in a moment after I get some coffee

#include <file.au3>
Local $fz_Path = @DesktopDir & '\FileName.txt'
Local $nArray = ''

_FileReadToArray($fz_Path, $nArray)

$Sdat = Findsdat($nArray, 'sdat')
;Test the return array
For $x = 1 To UBound($Sdat) - 1
    MsgBox(0, 'Test', $Sdat[$x])
Next

Func Findsdat(ByRef $aArray, $FindsDat)
    Local $RetArray = ''
    For $i = 1 To UBound($aArray) - 1
        If StringInStr($aArray[$i], $FindsDat) Then
            $RetArray = $RetArray & StringLeft(StringTrimLeft($aArray[$i], StringInStr($aArray[$i], $FindsDat) + 3), 4) & Chr(01)
        EndIf
    Next
    Return StringSplit(StringTrimRight($RetArray, 1), Chr(01))
EndFunc

Edit (the others I said I would post):

Here, this should make w0uter happy :o

Local $s_FRead = FileRead(@DesktopDir & '\testing.txt')
Local $Sdat = StringRegExp($s_FRead , ' sdat(.*?).exe', 3)
Local $test = ''
For $x = 0 To UBound($Sdat) - 1
    $test = $test & $Sdat[$x] & @LF
Next
MsgBox(0, 'Test', $test)

Or

#include <file.au3>
Local $s_Path = @DesktopDir & '\testing.txt'
Local $nArray = ''
_FileReadToArray($s_Path, $nArray)
$sDat = FindSdat($nArray)

Func FindSdat(ByRef $nArray)
    Local $RetArray = ''
    For $x = 1 To UBound($nArray)
        If StringInStr($nArray, 'sdat') Then $RetArray = $RetArray & _StringBetween($nArray[$x], 'sdat', '.exe') & Chr(01)
        Next
    Return StringSplit(StringTrimRight($RetArray, 1), Chr(01))
EndFunc

Func _StringBetween($s_String, $s_Start, $s_End = 0)
    $s_Start = StringInStr($s_String, $s_Start)+StringLen($s_Start)
    return StringMid($s_String, $s_Start, StringInStr($s_String, $s_End)-$s_Start)
EndFunc
Edited by SmOke_N

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.

Link to comment
Share on other sites

Don't know if i got a headache of seeying all those variables and codes :o:geek: or the whining people around me :sorcerer:

But thx, i hope i will understand it someday, the first one works, the 2nd doesn't. But i will focus on the first, and hopefully someday i can do it on my own ;)

Link to comment
Share on other sites

  • Moderators

Don't know if i got a headache of seeying all those variables and codes :o:geek: or the whining people around me :sorcerer:

But thx, i hope i will understand it someday, the first one works, the 2nd doesn't. But i will focus on the first, and hopefully someday i can do it on my own ;)

Made an edit to the first one, because FileRead() has some issue with "Having" to have the optional [count], I suggest you just use the Beta to test it, it will be much quicker if the file your reading is large.

If you have a question on what I did, just post it and I'll try to explain.

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.

Link to comment
Share on other sites

I am trying to combine it to download a file from a ftp.

but how can i get the outcome of the array wich is 4706 in a variable wich i can use for an example:

MsgBox(0, 'test', "test" & $Sdat[$x])

This doesn't work, but maybe i am way off with this and you guys will have a good laugh about it :o ?

And i will look at your first code and put some commentary with it and maybe you can say if i am right or wrong and explain?

Link to comment
Share on other sites

  • Moderators

I am trying to combine it to download a file from a ftp.

but how can i get the outcome of the array wich is 4706 in a variable wich i can use for an example:

MsgBox(0, 'test', "test" & $Sdat[$x])

This doesn't work, but maybe i am way off with this and you guys will have a good laugh about it :o ?

And i will look at your first code and put some commentary with it and maybe you can say if i am right or wrong and explain?

The outcome of the array is already in a variable...

$Sdat[$x] is the variable:

So let's say that I have 2 results after my search, and lets say the results were 4075 and 4076, it's now in an array... $Sdat[1] = 4075 and $Sdat[2] = 4076, both have the same 'Variable Name' but the element number ([1] / [2]) makes them unique from each other.

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.

Link to comment
Share on other sites

So if i understand it good i can adjust this code:

#include <file.au3>
Local $fz_Path = @DesktopDir & '\FileName.txt'
Local $nArray = ''

_FileReadToArray($fz_Path, $nArray)

$Sdat = Findsdat($nArray, 'sdat')
;Test the return array
For $x = 1 To UBound($Sdat) - 1
    MsgBox(0, 'Test', $Sdat[$x])
Next

Func Findsdat(ByRef $aArray, $FindsDat)
    Local $RetArray = ''
    For $i = 1 To UBound($aArray) - 1
        If StringInStr($aArray[$i], $FindsDat) Then
            $RetArray = $RetArray & StringLeft(StringTrimLeft($aArray[$i], StringInStr($aArray[$i], $FindsDat) + 3), 4) & Chr(01)
        EndIf
    Next
    Return StringSplit(StringTrimRight($RetArray, 1), Chr(01))
EndFunc

With after NEXT with this command "MsgBox(0, 'Test', $Sdat[$1])" ?

The reason what i want to do is to combine it with this code

ProgressOn('Downloading', '')
Local $netDnFileSize = 

InetGetSize("ftp://ftp.mcafee.com/pub/antivirus/superdat/intel/sdat" & $Sdat[$x])
InetGet("ftp://ftp.mcafee.com/pub/antivirus/superdat/intel/", 

"C:\Mareco\Antivirus\SDAT.exe", 1, 1)
While @InetGetActive
    ProgressSet((@InetGetBytesRead / $netDnFileSize) * 100, '', 'Download is' & "  " & 

Round((@InetGetBytesRead / $netDnFileSize) * 100) & '% voltooid')
    Sleep(250)
Wend
ProgressOff()

But i can't use the Variable $Sdat[$x] on his own because i think this is only one part of the string.

Don't i need to put $Sdat[$x] in a string so i can use it every where in the code when i want to?

I hope you understand what i am trying to say.

Link to comment
Share on other sites

  • Moderators

So if i understand it good i can adjust this code:

#include <file.au3>
Local $fz_Path = @DesktopDir & '\FileName.txt'
Local $nArray = ''

_FileReadToArray($fz_Path, $nArray)

$Sdat = Findsdat($nArray, 'sdat')
;Test the return array
For $x = 1 To UBound($Sdat) - 1
    MsgBox(0, 'Test', $Sdat[$x])
Next

Func Findsdat(ByRef $aArray, $FindsDat)
    Local $RetArray = ''
    For $i = 1 To UBound($aArray) - 1
        If StringInStr($aArray[$i], $FindsDat) Then
            $RetArray = $RetArray & StringLeft(StringTrimLeft($aArray[$i], StringInStr($aArray[$i], $FindsDat) + 3), 4) & Chr(01)
        EndIf
    Next
    Return StringSplit(StringTrimRight($RetArray, 1), Chr(01))
EndFunc

With after NEXT with this command "MsgBox(0, 'Test', $Sdat[$1])" ?

The reason what i want to do is to combine it with this code

ProgressOn('Downloading', '')
Local $netDnFileSize = 

InetGetSize("ftp://ftp.mcafee.com/pub/antivirus/superdat/intel/sdat" & $Sdat[$x])
InetGet("ftp://ftp.mcafee.com/pub/antivirus/superdat/intel/", 

"C:\Mareco\Antivirus\SDAT.exe", 1, 1)
While @InetGetActive
    ProgressSet((@InetGetBytesRead / $netDnFileSize) * 100, '', 'Download is' & "  " & 

Round((@InetGetBytesRead / $netDnFileSize) * 100) & '% voltooid')
    Sleep(250)
Wend
ProgressOff()

But i can't use the Variable $Sdat[$x] on his own because i think this is only one part of the string.

Don't i need to put $Sdat[$x] in a string so i can use it every where in the code when i want to?

I hope you understand what i am trying to say.

This actually looks like something I did for someone a while back lol... does this help?

ProgressOn('Downloading', '')
Local $netDnFileSize = InetGetSize("ftp://ftp.mcafee.com/pub/antivirus/superdat/intel/sdat" & $Sdat[$x])
InetGet("ftp://ftp.mcafee.com/pub/antivirus/superdat/intel/", "C:\Mareco\Antivirus\SDAT.exe", 1, 1)
While @InetGetActive
    ProgressSet((@InetGetBytesRead / $netDnFileSize) * 100, '', 'Download is' & "  " & Int((@InetGetBytesRead / $netDnFileSize) * 100) & '% voltooid')
    Sleep(250)
Wend
ProgressOff()

Edit:

I'm not sure about the ftp thing, never played around with that before... I don't know if anyone can concur if that actually works.

Edited by SmOke_N

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.

Link to comment
Share on other sites

This actually looks like something I did for someone a while back lol... does this help?

ProgressOn('Downloading', '')
Local $netDnFileSize = InetGetSize("ftp://ftp.mcafee.com/pub/antivirus/superdat/intel/sdat" & $Sdat[$x])
InetGet("ftp://ftp.mcafee.com/pub/antivirus/superdat/intel/", "C:\Mareco\Antivirus\SDAT.exe", 1, 1)
While @InetGetActive
    ProgressSet((@InetGetBytesRead / $netDnFileSize) * 100, '', 'Download is' & "  " & Int((@InetGetBytesRead / $netDnFileSize) * 100) & '% voltooid')
    Sleep(250)
Wend
ProgressOff()

Edit:

I'm not sure about the ftp thing, never played around with that before... I don't know if anyone can concur if that actually works.

This could be yours, i search the forums and try to combine all sort of things. So if it is yours or if it is in one off my topics before, Thank you AGAIN :o

But how can i combine these 2 scripts to use the SDAT variable?

Link to comment
Share on other sites

  • Moderators

This could be yours, i search the forums and try to combine all sort of things. So if it is yours or if it is in one off my topics before, Thank you AGAIN :geek:

But how can i combine these 2 scripts to use the SDAT variable?

I'm not quite sure I'm following, but here is a stab at it (Using method one since you said you understood that one or it's working for you:
#include <file.au3>
Local $fz_Path = @DesktopDir & '\FileName.txt'
Local $nArray = ''

_FileReadToArray($fz_Path, $nArray)

$Sdat = Findsdat($nArray, 'sdat')

For $x = 1 To UBound($Sdat) - 1
    ProgressOn('Downloading File ' & $x, '')
    Local $netDnFileSize = InetGetSize("ftp://ftp.mcafee.com/pub/antivirus/superdat/intel/sdat" & $Sdat[$x] & '.exe'); don't you need the .exe on the end?
    InetGet("ftp://ftp.mcafee.com/pub/antivirus/superdat/intel/", "C:\Mareco\Antivirus\SDAT.exe", 1, 1)
    While @InetGetActive
        ProgressSet((@InetGetBytesRead / $netDnFileSize) * 100, '', 'Download is' & "  " & Int((@InetGetBytesRead / $netDnFileSize) * 100) & '% voltooid')
        Sleep(250)
    Wend
Next
ProgressOff()

Func Findsdat(ByRef $aArray, $FindsDat)
    Local $RetArray = ''
    For $i = 1 To UBound($aArray) - 1
        If StringInStr($aArray[$i], $FindsDat) Then
            $RetArray = $RetArray & StringLeft(StringTrimLeft($aArray[$i], StringInStr($aArray[$i], $FindsDat) + 3), 4) & Chr(01)
        EndIf
    Next
    Return StringSplit(StringTrimRight($RetArray, 1), Chr(01))
EndFunc
I stuck it in the for loop assuming there is more than one file you need to download. It shows you each download it's on represented by $x ... I put this $Sdat[$x] & '.exe' with a comment, because there was no .exe on the end (assuming it is exe). Notice the ProgressOff() is outside the For/Next Loop.

Edit: Just an edit :o

Edited by SmOke_N

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.

Link to comment
Share on other sites

Pfff, this is hard to understand, but hell i won't give up :o

On this FTP site : ftp://ftp.mcafee.com/pub/antivirus/superdat/intel/

You can download a sdatxxxx.exe. The number xxxx changes every week/day, not on a specific moment.

I want to make a script wich automatically download THAT file to my harddrive.

Maybe i am trying to make this script work, while it can be done way more easier. I don't know.

But to your question, yes i need the .exe.

No it is just one file wich i have to download at this moment.

If i put a messagebox below this line: $Sdat = Findsdat($nArray, 'sdat')

like: MsgBox(0, 'test', "$sdat = " & $sdat)

$sdat is still empty.

Don't know if i am so stupid or this is hard to understand for a beginner. ;)

-Edit-

yes, i am stupid AND it is hard for a beginner. (i will say it myself :geek: )

Edited by Iznogoud
Link to comment
Share on other sites

  • Moderators

Pfff, this is hard to understand, but hell i won't give up :o

On this FTP site : ftp://ftp.mcafee.com/pub/antivirus/superdat/intel/

You can download a sdatxxxx.exe. The number xxxx changes every week/day, not on a specific moment.

I want to make a script wich automatically download THAT file to my harddrive.

Maybe i am trying to make this script work, while it can be done way more easier. I don't know.

But to your question, yes i need the .exe.

No it is just one file wich i have to download at this moment.

If i put a messagebox below this line: $Sdat = Findsdat($nArray, 'sdat')

like: MsgBox(0, 'test', "$sdat = " & $sdat)

$sdat is still empty.

Don't know if i am so stupid or this is hard to understand for a beginner. ;)

-Edit-

yes, i am stupid AND it is hard for a beginner. (i will say it myself :geek: )

Ha... at least your trying!

The reason it's returning nothing or an empty string, is now $Sdat is an array variable... which means you have to call the specific element you want to get the value of. So let's say I want to just see the 1st Element, and I don't want a For/Next loop to go through it one by one. I could do:

If IsArray($Sdat) Then MsgBox(0, 'Test to show', $Sdat[1])
1 being the first element of that array.

Now, If I want to see all the elements:

For $i = 1 To Ubound($Sdat) - 1
    MsgBox(0, 'Testing all elements', 'There are ' & (Ubound($Sdat) - 1) - $i & ' more elements to look at.' & _ 
    @CRLF & 'Element ' & $i & ' is: ' & $Sdat[$i])
Next
Does that help, or am I confusing you more? (I could confuse a table!!)

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.

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...