faustf Posted August 22, 2016 Posted August 22, 2016 hi guys i create a little script for find a word inside a txt , the strange is if insert in StringInStr a classical word like in example work good but when i insert a strig by variable , not work i attach also a list of word #include <MsgBoxConstants.au3> #include <File.au3> Local $sWordFind = "firenze" ;Local $sFilePath = "E:\_GESTIONALE_NEW\SPEDIZIONIERI\SPEDIAMO_IT\PROVINCIE\prov.txt" Local $sFilePath = "E:\_GESTIONALE_NEW\SPEDIZIONIERI\SPEDIAMO_IT\PROVINCIE\de.txt" ; Retrieve the character position of where the string 'white' first occurs in the sentence. Local $iTot_line = _FileCountLines($sFilePath) Local $hFileOpen = FileOpen($sFilePath, $FO_READ) Do Local $sFileRead = FileReadLine($hFileOpen, $iTot_line) ;MsgBox (0,'',$sFileRead &" "& $sWordFind ) Local $iPosition = StringInStr($sFileRead, $sWordFind) $iTot_line -= 1 Until $iTot_line = 0 FileClose($hFileOpen) MsgBox(0, '', $iPosition) de.txt
AutoBert Posted August 22, 2016 Posted August 22, 2016 (edited) The real problem is: You read txt line by line and if the word isn't in the last tested line (in your case first line of file) you get no position of the founded word in the lines before. Test this script: #include <MsgBoxConstants.au3> #include <File.au3> Local $sWordFind = "firenze" ;Local $sFilePath = "E:\_GESTIONALE_NEW\SPEDIZIONIERI\SPEDIAMO_IT\PROVINCIE\prov.txt" Local $sFilePath = "de.txt" ;< ; Retrieve the character position of where the string 'white' first occurs in the sentence. Local $iTot_line = _FileCountLines($sFilePath) Local $hFileOpen = FileOpen($sFilePath, $FO_READ) Do Local $sFileRead = FileReadLine($hFileOpen, $iTot_line) ;MsgBox (0,'',$sFileRead &" "& $sWordFind ) Local $iPosition = StringInStr($sFileRead, $sWordFind) If $iPosition Then ExitLoop $iTot_line -= 1 Until $iTot_line = 0 If $iTot_line>0 Then Local $sKey = StringLeft($sFileRead, 2) MsgBox(0, '', 'found in line: ' & $iTot_line & @CRLF & 'Key: ' & $sKey) Else MsgBox(0,'','Value not found') EndIf FileClose($hFileOpen) or try this: #include <MsgBoxConstants.au3> #include <File.au3> Local $sWordFind = "firenze" ;Local $sFilePath = "E:\_GESTIONALE_NEW\SPEDIZIONIERI\SPEDIAMO_IT\PROVINCIE\prov.txt" Local $sFilePath = "de.txt" ; ; Retrieve the character position of where the string 'white' first occurs in the sentence. ;Local $iTot_line = _FileCountLines($sFilePath) Local $hFileOpen = FileOpen($sFilePath, $FO_READ) Local $sFileRead = FileRead($hFileOpen) Local $iPosition = StringInStr($sFileRead, $sWordFind) FileClose($hFileOpen) If $iPosition>0 Then Local $sKey = StringMid($sFileRead, $iPosition-5,2) MsgBox(0, '', 'Value found at pos: ' & $iPosition & @CRLF & 'Key: ' & $sKey) Else MsgBox(0,'','Value not found') EndIf it's a little bit faster. Edited August 22, 2016 by AutoBert 2. script added
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now