Sign in to follow this  
Followers 0
craigholohan

Need help with StringInStr and _ReplaceStringInFile

2 posts in this topic

Hi All,

Please see below code, can anyone help please......

I am trying to first find the word, if it is in the file I want to be prompted if I want to replace it or not, if yes selected it mus replace the word, if no selected it must exit the loop.

I think I got the first part working but when I select yes to replace it does not actually replace the word.

Thanks in advance for any help.

#include <File.au3>

Global $readmsg
$fileRead = FileOpen("C:\test.txt", 0)
$filename = FileRead("C:\test.txt", 0)
$word=InputBox("Question", "What word?", "", "", -1, -1)
$replaceword = InputBox("Question", "With what word must I replace?", "", "", -1, -1)

While 1
$line = FileReadLine($fileRead)
If @error = -1 Then ExitLoop
if StringInStr(" "&$line,$word)>0 then $readmsg = 1 
    
if $readmsg = 1 Then
    If Not IsDeclared("readmsg") Then Local $readmsg
    $readmsg = MsgBox(36, "Found - ", $word& " in line " &$line& @CRLF&"Do you wast to replace it with " &$replaceword)
    Select
        Case $readmsg = 6;Yes
            MsgBox(0,"you clicked Yes","Ok here is where _ReplaceStringInFile should run")
            $retval = _ReplaceStringInFile($filename,$word,$replaceword)
            ExitLoop
                
        Case $readmsg = 7;No
            MsgBox(0,"you clicked No","I dont wat to change it")
            ExitLoop
    EndSelect
EndIf

Wend

FileClose($fileRead)

Share this post


Link to post
Share on other sites



The first error is opening the text file:

$fileRead = FileOpen("C:\test.txt", 0)

it is not an obvious error ... it took me some time to figure it - reading the code of _ReplaceStringInFile I've noticed that the function is reading the file, creating a temporary one, make the modifications, delete the original and rename the temporary file; once the original is open it can't be deleted so the function failed to work.

The line:

$filename = FileRead("C:\test.txt", 0) is obsolete

Another error was to place the remaining on the while loop and not to make it all the lines - I've made a couple of modifications and the result is:

#include <File.au3>

Global $readmsg
$fileRead = "C:\test.txt"
$word=InputBox("Question", "What word?", "", "", -1, -1)
$replaceword = InputBox("Question", "With what word must I replace?", "", "", -1, -1)

For $i=1 To FileGetSize($fileRead)
$line = FileReadLine($fileRead, $i)
If @error = -1 Then ExitLoop
if StringInStr(" "&$line,$word)>0 Then
    If Not IsDeclared("readmsg") Then Local $readmsg
    $readmsg = MsgBox(36, "Found - ", $word& " in line " &$line& @CRLF&"Do you wast to replace it with " &$replaceword)
    Select
        Case $readmsg = 6;Yes
            MsgBox(0,"you clicked Yes","Ok here is where _ReplaceStringInFile should run")
            $retval = _ReplaceStringInFile($fileRead,$word,$replaceword)
            ExitLoop
                
        Case $readmsg = 7;No
            MsgBox(0,"you clicked No","I dont wat to change it")
            ExitLoop
    EndSelect
EndIf
Next
Exit

The code could be "refined" further but sadly ... I can't afford it right now..

Good luck


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
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
Sign in to follow this  
Followers 0