Jump to content

find replace untill string does not exist


Recommended Posts

Good day,

I am trying to open a text file find some text, do a carriage return to new line and add some text. The string I am looking for may or not exist in the file. If it can't find the string I want it to still close and save the file go on to the next file.

The problem is that once it finds the last string and the message box cannot find wait 1 sec comes up. The window text shows when you use AU3info is: Cannot find "wait 1 sec"

I have tried it with and without the double quotes script continues to run still. Here is the script that I have.

Any help would be greatly appreciated.

Thank you.

; Shows the filenames of all files in the current directory

$search = FileFindFirstFile("F:\MACROS\private\test\test\*.mac")

$find = "wait 1 sec"

$replace = "wait 5 msec "

; Check if the search was successful

If $search = -1 Then

MsgBox(0, "Error", "No files/directories matched the search pattern")

Exit

EndIf

While 1

$file = FileFindNextFile($search)

If @error Then ExitLoop

run("notepad.exe F:\MACROS\private\test\test\" & $file)

do

Sleep (500)

send ("{CTRLDOWN}f{CTRLUP}")

WinWait("Find","Fi&nd what:")

sleep (500)

Send($find)

sleep (2500)

;this what the window text shows when you use AU3info

;Cannot find "wait 1 sec"

;I have tried it with and without the double quotes script continues to run

If winexists ("Notepad","Cannot """"find wait 1 """) Then

sleep (2500)

Send("{SPACE}")

sleep (2500)

Send("{ESC}")

sleep (2500)

Send("{CTRLDOWN}s{CTRLUP}{ALTDOWN}{F4}{ALTUP}")

EndIf

send ("{enter}")

sleep (500)

Send("{ESC}")

sleep (500)

Send("{END}{ENTER}")

send ($replace)

until WinExists("Notepad","")

MsgBox(0, "Error", "No files/directories matched the search pattern")

WEnd

Link to comment
Share on other sites

I see additional space before second quote and one unnecessary "" pair. Maybe this cause?

If winexists ("Notepad", 'Cannot "find wait 1"') Then

Anyway, isn't simpler and safer to load macro into string with FileRead and search with StringInStr?

<{POST_SNAPBACK}>

Hi Lazycat,

Thank you for your reply, however it did not work either.

As for your suggestion, it might be safer to do a fileread and do a search. However, I am not that knowledgable in writing scripts and I was happy to get where I am. I will give it a try though.

Thank you again for your help.

Link to comment
Share on other sites

As for your suggestion, it might be safer to do a fileread and do a search. However, I am not that knowledgable in writing scripts and I was happy to get where I am. I will give it a try though.

you might want to try this :-)

#include <File.au3>

$find = "wait 1 sec"
$replace = "wait 5 msec"


$filepath = "F:\MACROS\private\test\test\"
$filepattern = "*.mac"

$search = FileFindFirstFile($filepath & $filepattern)

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf


While 1
    $file = FileFindNextFile($search)
    if @error then exitloop
    $retval = ReplaceStringInFile($filepath & $file,$find,$replace,0,1)
    if $retval = -1 then
        msgbox(0, "ERROR", "The pattern could not be replaced in file: " & $file)
        exit
    else
        msgbox(0, "INFO", "Found " & $retval & " occurances of the pattern: " &$find & " in the file: " & $file)
    endif
wend

func ReplaceStringInFile($filename, $searchstring, $replacestring,$caseness = 0, $occurance = 0)
; $filename = name of the file to open. ! Need the FULL path, not just the name returned by FileFindNextFile !
; $searchstring = string to search 
; $replacestring = string to replace
; $caseness = should case matter? 0 = NO (default), 1 = YES
; $occurence = shall we find all strings in the file or just the first? 0 = first only (default), 1 = ALL strings

    local $retval = 0
    local $tempfile, $readhandle, $writehandle

    $tempfile = _TempFile()

    $readhandle = FileOpen($filename,0)
    if $readhandle = -1 then return -1

    $writehandle = FileOpen($tempfile,2)
    if $writehandle = -1 then return -1

    while 1 
        $line = FileReadLine($readhandle)
        if @error then exitloop
        if StringInStr($line,$searchstring,$caseness,$occurance) then
            $retval += 1
            $line = StringReplace($line,$searchstring,$replacestring,0,$caseness)
            FileWriteLine($writehandle,$line)
            if $occurance = 0 then
                $retval = 1
                exitloop
            endif
        else 
            FileWriteLine($writehandle,$line)
        endif 
    wend

    FileClose($readhandle)
    FileClose($writehandle)
    
    FileDelete($filename)
    FileMove($tempfile,$filename)

    return $retval
endfunc

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

you might want to try this :-)

#include <File.au3>

$find = "wait 1 sec"
$replace = "wait 5 msec"
$filepath = "F:\MACROS\private\test\test\"
$filepattern = "*.mac"

$search = FileFindFirstFile($filepath & $filepattern)

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf
While 1
    $file = FileFindNextFile($search)
    if @error then exitloop
    $retval = ReplaceStringInFile($filepath & $file,$find,$replace,0,1)
    if $retval = -1 then
        msgbox(0, "ERROR", "The pattern could not be replaced in file: " & $file)
        exit
    else
        msgbox(0, "INFO", "Found " & $retval & " occurances of the pattern: " &$find & " in the file: " & $file)
    endif
wend

func ReplaceStringInFile($filename, $searchstring, $replacestring,$caseness = 0, $occurance = 0)
; $filename = name of the file to open. ! Need the FULL path, not just the name returned by FileFindNextFile !
; $searchstring = string to search 
; $replacestring = string to replace
; $caseness = should case matter? 0 = NO (default), 1 = YES
; $occurence = shall we find all strings in the file or just the first? 0 = first only (default), 1 = ALL strings

    local $retval = 0
    local $tempfile, $readhandle, $writehandle

    $tempfile = _TempFile()

    $readhandle = FileOpen($filename,0)
    if $readhandle = -1 then return -1

    $writehandle = FileOpen($tempfile,2)
    if $writehandle = -1 then return -1

    while 1 
        $line = FileReadLine($readhandle)
        if @error then exitloop
        if StringInStr($line,$searchstring,$caseness,$occurance) then
            $retval += 1
            $line = StringReplace($line,$searchstring,$replacestring,0,$caseness)
            FileWriteLine($writehandle,$line)
            if $occurance = 0 then
                $retval = 1
                exitloop
            endif
        else 
            FileWriteLine($writehandle,$line)
        endif 
    wend

    FileClose($readhandle)
    FileClose($writehandle)
    
    FileDelete($filename)
    FileMove($tempfile,$filename)

    return $retval
endfunc

Cheers

Kurt

<{POST_SNAPBACK}>

Kurt thank you for your reply.

That worked great.

I must not have been clear on what I would like to do. I do not want to replace the "wait 1 sec". I want to find that line then goto the next line and write "wait 5 msec", until end of file and then save the file.

The code you were so kind to give me replaces wait 1 sec with wait 5 msec.

Thank you I appreciate it your time in trying to assist me.

John

Link to comment
Share on other sites

I must not have been clear on what I would like to do. I do not want to replace the "wait 1 sec". I want to find that line then goto the next line and write "wait 5 msec", until end of file and then save the file. 

can you post an example? Before and after?

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

can you post an example? Before and after?

Cheers

Kurt

<{POST_SNAPBACK}>

Sure Kurt,

Here is what a file might look like before and after.

Now I have no idea why I just can't change the wait 1 sec to wait 5 sec. The macros just will not work. However if I add the 5msec to the next line they work just fine. I have over 200 macros that I need to change, I thought this would be easier then to have edit each file manually.

05 b.mac is before change

05 a.mac is after change

Thank you again for your help.

John

oops I didn't get the fils to attach. hereis some code: before

[wait inp inh]

wait 1 sec until FieldAttribute 0000 at (9,76)

[enter]

[wait inp inh]

wait 1 sec until FieldAttribute 0000 at (4,62)

[enter]

[wait inp inh]

wait 1 sec until FieldAttribute 0000 at (7,1)

"1

[enter]

[wait inp inh]

wait 1 sec until FieldAttribute 0000 at (7,1)

[enter]

[wait inp inh]

wait 1 sec until FieldAttribute 0000 at (4,36)

[enter]

[wait inp inh]

wait 1 sec until FieldAttribute 0000 at (9,16)

and after :

wait 1 sec until FieldAttribute 0000 at (9,76)

wait 5 msec

[enter]

[wait inp inh]

wait 1 sec until FieldAttribute 0000 at (4,62)

wait 5 msec

[enter]

[wait inp inh]

wait 1 sec until FieldAttribute 0000 at (7,1)

wait 5 msec

"1

[enter]

[wait inp inh]

wait 1 sec until FieldAttribute 0000 at (7,1)

wait 5 msec

[enter]

[wait inp inh]

wait 1 sec until FieldAttribute 0000 at (4,36)

wait 5 msec

[enter]

[wait inp inh]

wait 1 sec until FieldAttribute 0000 at (9,16)

wait 5 msec

I am sorry that I wasn't able to attach the files.

John

Edited by caspermurphy
Link to comment
Share on other sites

and after :

wait 1 sec until FieldAttribute 0000 at (9,76)

wait 5 msec

[enter]

[wait inp inh]

wait 1 sec until FieldAttribute 0000 at (4,62)

wait 5 msec

O.K. this is not exactly what you described in your previous post: I do not want to replace the "wait 1 sec". I want to find that line then goto the next line and write "wait 5 msec", until end of file and then save the file.

I'm a bit consused now! Can you describe a bit more what you want to achieve? Is it like this?

File before:

line 1: kllfkjfsdljf

line 2: cbnmbx

line 3: do somethin "wait 1 sec"

line 4: wquiz239

line 5: 8012830

line 6: mnasdllk

File after:

line 1: kllfkjfsdljf

line 2: cbnmbx

line 3: do somethin "wait 1 sec"

line 4: wait 5 msec

line 5: wait 5 msec

line 6: wait 5 msec

This is at least what I thought when I read your reply to my first post !??!

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

and after :

wait 1 sec until FieldAttribute 0000 at (9,76)

wait 5 msec

[enter]

[wait inp inh]

wait 1 sec until FieldAttribute 0000 at (4,62)

wait 5 msec

[enter]

after I read your post the second time, I understood it.. There is only a minor change necessary in the code (marked with <==)

#include <File.au3>

$find = "wait 1 sec"
$replace = "wait 5 msec"
$filepath = "F:\MACROS\private\test\test\"
$filepattern = "*.mac"

$search = FileFindFirstFile($filepath & $filepattern)

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf
While 1
    $file = FileFindNextFile($search)
    if @error then exitloop
    $retval = ReplaceStringInFile($filepath & $file,$find,$replace,0,1)
    if $retval = -1 then
        msgbox(0, "ERROR", "The pattern could not be replaced in file: " & $file)
        exit
    else
        msgbox(0, "INFO", "Found " & $retval & " occurances of the pattern: " &$find & " in the file: " & $file)
    endif
wend

func ReplaceStringInFile($filename, $searchstring, $replacestring,$caseness = 0, $occurance = 0)
; $filename = name of the file to open. ! Need the FULL path, not just the name returned by FileFindNextFile !
; $searchstring = string to search
; $replacestring = string to replace
; $caseness = should case matter? 0 = NO (default), 1 = YES
; $occurence = shall we find all strings in the file or just the first? 0 = first only (default), 1 = ALL strings

    local $retval = 0
    local $tempfile, $readhandle, $writehandle

    $tempfile = _TempFile()

    $readhandle = FileOpen($filename,0)
    if $readhandle = -1 then return -1

    $writehandle = FileOpen($tempfile,2)
    if $writehandle = -1 then return -1

    while 1
        $line = FileReadLine($readhandle)
        if @error then exitloop
        if StringInStr($line,$searchstring,$caseness,$occurance) then
            $retval += 1
           ; <<== removed a line here
            FileWriteLine($writehandle,$line)
            FileWriteLine($writehandle,$replacestring & @CRLF); <== added this !
            if $occurance = 0 then
                $retval = 1
                exitloop
            endif
        else
            FileWriteLine($writehandle,$line)
        endif
    wend

    FileClose($readhandle)
    FileClose($writehandle)
    
    FileDelete($filename)
    FileMove($tempfile,$filename)

    return $retval
endfunc

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

Good morning Kurt,

thank you for helping me out. What was the reason for this line? $retval += 1

When I ran it came up with an error (49) : ==> Expected a "=" operator in assignment statement.: I just took out the + sign and worked great for me.

Thank you again for your help.

John

Link to comment
Share on other sites

+= aswell as -=, *=, /= and &= are the new operators included in the beta. They are used to simplify code such as $A = $A + 1 to just $A += 1, this also applies for multiplication, subtraction, division, and concatenation.

Edited by Burrup

qq

Link to comment
Share on other sites

+= aswell as -=, *=, /= and &= are the new operators included in the beta. They are used to simplify code such as $A = $A + 1 to just $A += 1, this also applies for multiplication, subtraction, division, and concatenation.

<{POST_SNAPBACK}>

are there ++ and -- yet?
Link to comment
Share on other sites

thank you for helping me out. What was the reason for this line?  $retval += 1

Hi John,

Burrup already explained +=. The reason I use the statement $retval += 1, is that I want to return the number of occurrences of the search string found in the file.

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

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