Jump to content

Find and Replace with Notepad (ControlSend)


Recommended Posts

Hi everyone,

I have a rather simple task to do but I haven't been able to find out exactly how. Maybe one of you can help me out with this.

I have to open a text file invisibly (with the hidden status), find certain characters in that file, replace them with another set of characters then save and close the file.

Basically, I thought of using Notepad's "Find and Replace" functionality and I would run the script to do that.

I have over 1000 files to treat and it would be really tedious to do them by hand so thought of creating a script that does it for me.

I know that my best bet would be to use the Send or ControlSend function, but I tried different ways and it didnt work, and I looked up in the forum and couldn't find it.

The following code works but needs to have the window activated, and I need to run this in the background (hidden mode).

$txtFile = $CmdLine[1]
ShellExecute("Notepad.exe", $txtFile,@ScriptDir,"open");,@SW_HIDE)
WinActivate($txtFile & " - Notepad")
Send("!er")
Send(" -!p")
Send("L3 -!a")
Send("{ESC}")
Send("!fs!fx")
;MsgBox(0, "File", $txtFile)

Would be awesome if anyone can help, as I don't think this is a really complicated task.

Thank you.

the123punch

Edited by the123punch
Link to comment
Share on other sites

you can Use FileRead() and StringReplace() , Both are fast,consistant and act completely in the background. Much better than useing notepad imho

There is always a butthead in the crowd, no matter how hard one tries to keep them out.......Volly

Link to comment
Share on other sites

you can Use FileRead() and StringReplace() , Both are fast,consistant and act completely in the background. Much better than useing notepad imho

Hi ofLight,

Thank you for your prompt response. It all worked out really fine. Thanks a lot for your suggestion.

The only thing that did not work though, is that this script will have to run periodically (every day at a specific hour), where it will search for text files on the server, and replace some text in it with other text.

Now, when I run my script with a Windows session open, it works. It can go open files, change them, rewrite them, etc...

When I add it in the planned task scheduler, it does not work. As if the file I/O does not work when I am not logged in a session.

Here is the code that works when logged in:

;getting parameters from command line
$txtFile = $CmdLine[1]
$str2Find = $CmdLine[2]
$str2Replace = $CmdLine[3]
;opening file for reading
$file = FileOpen($txtFile, 0)
If $file <> -1 Then
    ;getting file contents
    $fc = FileRead($file)
    ;replacing the string to find with the string to replace
    $fc = StringReplace($fc, $str2Find, $str2Replace)
    FileClose($file)
    ;re-writing everything back to the file
    $file = FileOpen($txtFile, 2)
    FileWrite($file, $fc)
    FileClose($file)
Else
    Exit
EndIf

Anyone has any idea?

the123punch

Edited by the123punch
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...