Jump to content

not fin StringInStr o_O


faustf
 Share

Recommended Posts

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

Link to comment
Share on other sites

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 by AutoBert
2. script added
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...