hello,
I need some help.
I would like to search a string in a file (always the same file) and then display this string and the rest of concerned line.
- Ex : the file name is : laison.txt
a little extract of the text file (very big text file 3mb)
101C COMMANDE ELECTRO COA ARG****
COMMANDE ELECTROVANNE CORRECTION D'ASSIETTE ARRIERE GAUCHE
101D COMMANDE ELECTRO COA AVD
COMMANDE ELECTROVANNE CORRECTION D'ASSIETTE AVANT DROIT
101E COMMANDE ELECTRO COA ARD
COMMANDE ELECTROVANNE CORRECTION D'ASSIETTE ARRIERE DROIT
- Entering the string to search in a textbox
- Ex : string to search "101D"
- And so display (in a msgbox or a tooltips) :
101D COMMANDE ELECTRO COA AVD
COMMANDE ELECTROVANNE CORRECTION D'ASSIETTE AVANT DROIT
How to realize that simply (or not)
Thank you very much for your help.
Search text in a file and display result
Re: Search text in a file and display result
Hi Gildamac!
Il est en français ton fichier!
Hope it helps you
Il est en français ton fichier!
Code: Select all
string =
(
101C COMMANDE ELECTRO COA ARG****
COMMANDE ELECTROVANNE CORRECTION D'ASSIETTE ARRIERE GAUCHE
101D COMMANDE ELECTRO COA AVD
COMMANDE ELECTROVANNE CORRECTION D'ASSIETTE AVANT DROIT
101E COMMANDE ELECTRO COA ARD
COMMANDE ELECTROVANNE CORRECTION D'ASSIETTE ARRIERE DROIT
)
A := StrSplit(string, "`n`n") ; we split the string (`n`n = empty line)
SearchForCommandId(__ID) {
global A ; to retrieve A defined without the function since all variable are implicitly local within a function
Loop % A.MaxIndex() ; loops throught the splited string
{
if (SubStr(A[a_index], 1, 4) == __ID) { ; if the string starts with the ID
return A[a_index] ; returns the ID
}
}
return "La commande " . __ID . "n'a pas pu être trouvée." ; otherwise returns a message
} ; a function to search a command by its ID
MsgBox % SearchForCommandId("101D")
MsgBox % SearchForCommandId("101E")
MsgBox % SearchForCommandId("107D")
Re: Search text in a file and display result
Hallo,
this script search in C:\laison.txt
this script search in C:\laison.txt
Code: Select all
InputBox, SearchString, Search in laison.txt, Entering the string to search
Line := False
Loop, Read, C:\laison.txt
{
If InStr(A_LoopReadLine, SearchString)
If !Line
{
Line := A_LoopReadLine
Continue ;second concerned line
}
Else
{
Line .= "`r`n" . A_LoopReadLine
Break
}
}
MsgBox, % Line
Re: Search text in a file and display result
Hello A_AhkUser,A_AhkUser wrote:Hi Gildamac!
Il est en français ton fichier!
Hope it helps youCode: Select all
string = ( 101C COMMANDE ELECTRO COA ARG**** COMMANDE ELECTROVANNE CORRECTION D'ASSIETTE ARRIERE GAUCHE 101D COMMANDE ELECTRO COA AVD COMMANDE ELECTROVANNE CORRECTION D'ASSIETTE AVANT DROIT 101E COMMANDE ELECTRO COA ARD COMMANDE ELECTROVANNE CORRECTION D'ASSIETTE ARRIERE DROIT ) A := StrSplit(string, "`n`n") ; we split the string (`n`n = empty line)
thank you very much for your french answer
You solution work fine, but...
the file where to find the string is rather big (about 3Mo)...does the string commande (string =) will support this lenght ?
And if yes, how to convert the file in string ?
Thank you again
Re: Search text in a file and display result
Hallo,
Please try my version above
Please try my version above
Re: Search text in a file and display result
hello Rohwedder,Rohwedder wrote:Hallo,
Please try my version above
Your solution work fine for the first line, but don't write the second line.
In fact, if you search "101D"
the result must be
101D COMMANDE ELECTRO COA AVD
COMMANDE ELECTROVANNE CORRECTION D'ASSIETTE AVANT DROIT
that is to say the line which contains 101D and the next line.
Re: Search text in a file and display result
And now?
Code: Select all
InputBox, SearchString, Search in laison.txt, Entering the string to search
Line := False
Loop, Read, C:\laison.txt
{
If !Trim(A_LoopReadLine)
Continue
If InStr(A_LoopReadLine, SearchString)
{
Line := A_LoopReadLine
Continue ;second concerned line
}
If Line
{
Line .= "`r`n" . A_LoopReadLine
Break
}
}
MsgBox, % Line
Re: Search text in a file and display result
Rohwedder wrote:And now?
Code: Select all
InputBox, SearchString, Search in laison.txt, Entering the string to search Line := False Loop, Read, C:\laison.txt { If !Trim(A_LoopReadLine) Continue If InStr(A_LoopReadLine, SearchString) { Line := A_LoopReadLine Continue ;second concerned line } If Line { Line .= "`r`n" . A_LoopReadLine Break } } MsgBox, % Line
Perfect
Thank you very much again.
Re: Search text in a file and display result
I could use that! Tell me is it possible to search for a string within a string...I mean, the line in the text file doesn't start with the searched string?Rohwedder wrote:And now?
Code: Select all
InputBox, SearchString, Search in laison.txt, Entering the string to search Line := False Loop, Read, C:\laison.txt { If !Trim(A_LoopReadLine) Continue If InStr(A_LoopReadLine, SearchString) { Line := A_LoopReadLine Continue ;second concerned line } If Line { Line .= "`r`n" . A_LoopReadLine Break } } MsgBox, % Line
In fact, I have a .txt file with a bunch of file path in it. I would like to search with the filename within the filepath.
For instance:
I search "FileName1"
.txt file contain:
//Drive/FilePath/FileName0.txt
//Drive/FilePath/FileName0.pdf
//Drive/FilePath/FileName1.txt
//Drive/FilePath/FileName1.pdf
//Drive/FilePath/FileName2.txt
//Drive/FilePath/FileName2.pdf
//Drive/FilePath/FileName11.txt
//Drive/FilePath/FileName11.pdf
The script return (in a combobox or whatever):
FileName1.txt
FileName1.pdf
FileName11.txt
FileName11.pdf
The use can select a file to open...
But as the code is right now, it find line in the .txt file only if the line starts with the serched string.
Re: Search text in a file and display result
Hallo,
try:
try:
Code: Select all
InputBox, FullPath, Search File, Entering the Fullpath of File
InputBox, SearchString, Search in %FullPath%, Entering the String to search
Lines =
Loop, Read, %FullPath%
If InStr(A_LoopReadLine, SearchString)
Lines .= SubStr(A_LoopReadLine, 1+InStr(A_LoopReadLine,"/",,0)) "`r`n"
MsgBox, % Lines
Re: Search text in a file and display result
That's it! Thank you very much! It works perfectly!Rohwedder wrote:Hallo,
try:Code: Select all
InputBox, FullPath, Search File, Entering the Fullpath of File InputBox, SearchString, Search in %FullPath%, Entering the String to search Lines = Loop, Read, %FullPath% If InStr(A_LoopReadLine, SearchString) Lines .= SubStr(A_LoopReadLine, 1+InStr(A_LoopReadLine,"/",,0)) "`r`n" MsgBox, % Lines
Re: Search text in a file and display result
That's it! Thank you very much! It works perfectly!Rohwedder wrote:Hallo,
try:Code: Select all
InputBox, FullPath, Search File, Entering the Fullpath of File InputBox, SearchString, Search in %FullPath%, Entering the String to search Lines = Loop, Read, %FullPath% If InStr(A_LoopReadLine, SearchString) Lines .= SubStr(A_LoopReadLine, 1+InStr(A_LoopReadLine,"/",,0)) "`r`n" MsgBox, % Lines
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 191 guests