Jump to content

Recommended Posts

Posted

I have searched the forums but not exhaustively so if this has been answered before please accept my apologies....

I can get Notepad to open and do a search and replace, but it does not offer an info window or anything when it has finished -- how can I detect this?

  • Moderators
Posted

I'm not exactly sure what you are looking for, but the MS Word Automation Library has a _WordDocFindReplace() function with several parameters. Check my signature for the link.

Posted

OK thanks for those word functions bigdaddy, but I want to keep the text clean of any weird non-printing characters Word add becuse these are Oracle SQL statements and I don't want them corrupted... I know notepad is such a basic editor (and everyone has it so anyone can run this script) that it leaves the file unsullied.

Lar I tried your second solution first and came up with this:

RunWait("Notepad.exe C:\PVCS\PVCS-inserts.sql")

WinWaitActive("PVCS-inserts.sql - Notepad")

$SQLtext = ControlGetText("PVCS-inserts - Notepad", "", "Edit1")

$SQLtext = StringReplace($SQLtext, "{SHIFTDOWN}7{SHIFTUP}", "'{SHIFTDOWN}\\{SHIFTUP}CHR{SHIFTDOWN}9{SHIFTUP}38{SHIFTDOWN}0\\{SHIFTUP}'")

ControlSetText("PVCS-inserts.sql - Notepad", "", "Edit1", $SQLtext )

...which gives me a blank file! I suspect I have cocked up the last line.

Just to give a bit of background, these are INSERT statements generated from a version control system called PVCS but some of the comments have got ampersands in which an Oracle 'feature' (bug) interprets as an input parameter even within quotes so I am trying to replace them with '||CHR(38)||' which will concatenate the raw ASCII code in the middle of the string.

I am going to try Lar's method 1 now with a Repeat until WindowActive("Notepad") which is the title of the window that tells you it can't find the character.

Thanks for the help guys,

Regards,

Jim.

Posted (edited)

Hi,

does this do your trick?

; Search & Replace in Notepad
$title = "Unbenannt - Editor"
$text = "Hello World & Haha replace '&' by '||CHR(38)||'"
Run('Notepad.exe')
WinWaitActive($title)
ControlSend($title,"", 15, $text)
$newText = StringReplace(ControlGetText($title, "", 15), '&', '||CHR(38)||')
MsgBox(16, "Replace in Notepad", "OLD : " & $text & @CRLF & "NEW : " & $newText)
ControlSetText($title, "", 15, $newText)

Edit: Change title to your language

So long,

Mega

Edited by th.meger

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Posted (edited)

Wooo hooo....this works a treat!.....

WinMenuSelectItem("PVCS-inserts.sql - Notepad", "", "&Edit", "&Replace")

Send("{SHIFTDOWN}7{SHIFTUP}{TAB}'{SHIFTDOWN}\\{SHIFTUP}CHR{SHIFTDOWN}9{SHIFTUP}38{SHIFTDOWN}0\\{SHIFTUP}'{TAB}{TAB}{TAB}{ENTER}")

Do

Send("{SHIFTDOWN}{TAB}{SHIFTUP}{ENTER}{TAB}{ENTER}")

Until WinActive("Notepad")

AND it's faster than "Replace All"!!

YO!

P.S Just saw your reply Mega and thanks, but this is easier!! Also it's a fairly big file (1.3 Mb) so I am not convinced that a $var would cope with that.....

Edited by jimollerhead
Posted

Yeah I spoke too soon.... weird things have started to happen....this is my test script:

RunWait("Notepad.exe C:\PVCS\PVCS-inserts.sql")

WinWaitActive("PVCS-inserts.sql - Notepad")

WinMenuSelectItem("PVCS-inserts.sql - Notepad", "", "&Edit", "&Replace")

Send("{SHIFTDOWN}7{SHIFTUP}{TAB}'{SHIFTDOWN}\\{SHIFTUP}CHR{SHIFTDOWN}9{SHIFTUP}38{SHIFTDOWN}0\\{SHIFTUP}'{TAB}{TAB}{TAB}{ENTER}")

Do

Send("{SHIFTDOWN}{TAB}{SHIFTUP}{ENTER}{TAB}{ENTER}")

Until WinActive("Notepad")

First time through it just opens the file and sits there with an autoit icon in the system tray

Second time through it works and spawns a second autoit icon in the system tray

Third time through, still two icons, but when it exits, the "Notepad" window flickers a few times and I start to get carriage returns repeatedly entered into my file and have to use task manager to kill it.

The string replace sounds a good option but I have had enough and am gonna use vbscript

Thanks for all the replies,

Jim.

Posted

Well I was not too chuffed with the string functions in vbscript so decided to have one last go with StringReplace.... and it doesn't bloody work!!! Why is nothing working for me???

This is the code fragment:

$FileIn = FileOpen("PVCS-inserts.txt", 0)

$FileOut = FileOpen("PVCS-inserts.sql", 2)

;I cut and pasted the example form the help file -- this worked!

$SQLtext = StringReplace("this is a line of text", " ", "-")

$numreplacements = @extended

MsgBox(0, "New string is", $SQLtext)

MsgBox(0, "The number of replacements done was", $numreplacements)

;This is my code and the string is identical at the bottom as it is at the top.... (sigh)

While 1

$SQLtext = FileReadLine($FileIn)

msgbox(0, "top of the loop", $SQLtext)

If @error = -1 Then ExitLoop

StringReplace($SQLtext, "SolvCV", "SolvCT")

FileWriteLine($FileOut, $SQLtext)

msgbox(0, "bottom of the loop", $SQLtext)

Wend

Posted

Hi All,

I finally had to give up with the notepad thing, it was just too flakey... I think the problem is that autoIT works so hellishly fast that it presses the keys faster than any human could and notepad cannot cope -- I tried the OPTKey delays but it still was dodgy, sometimes it worked sometimes it didn't.

My final solution, as suggested by randallc, was this and it works a treat... and faster than in notepad (when it worked):

$FileIn = FileOpen("PVCS-inserts.txt", 0)

$FileOut = FileOpen("PVCS-inserts.sql", 2)

While 1

$SQLtext = FileReadLine($FileIn)

If @error = -1 Then ExitLoop

$SQLtext = StringReplace($SQLtext, "&", "'||CHR(38)||'")

$SQLtext = StringReplace($SQLtext, "SolvCV", "SolvCT");

FileWriteLine($FileOut, $SQLtext)

WEnd

I'm a stubborn sod and hate being beaten and this works consistently well (I had the syntax wrong before).

A more 'elegant' solution too.

Phew! -- onwards and upwards...

Thanks to all who contributed (and to Lar who had obviously been furiously running test stubs to find out the cause!)

Best,

Jim.

Posted

Hi,

would have been much easier, if you haven't named the thread "Search & replace in NOTEPAD". Instead of that, it should be replacing in data or in file ... :P

So long,

Mega :)

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Posted

Just in case anyone is interested, the project is now complete and has been successfully installed on someone else's machine.

I compiled the script that "does the work" and incorporated the exe file (along with several SQL scripts, a ReadMe and an ini file) into another compilation that does the install and creates a desktop shortcut.

Something I came across late were the INI file functions -- I had been opening and reading from the INI file manually and cursed when I found them -- it simplified the code immensely!!

Jim.

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...