2

I've got a text file that I'd like to open using a batch script. Once it's open, I'd like to simulate pressing CTRL + END on the keyboard so that the cursor is placed at the very bottom of the file.

I can open the file in Notepad by using start /max myFile.txt, but I'm struggling on how to navigate to the bottom. I've read about a SendKeys function, but I can't seem to get this to work with sending ^{END} to my file (I've read mixed information about whether it's available in batch scripting or not).

Any help is greatly appreciated!

helencrump
  • 1,351
  • 1
  • 18
  • 27
  • Add `WScript.sleep 1000` prior to sending the key. – wOxxOm Nov 25 '15 at 11:54
  • There is no native command for sending key-strokes in `cmd.exe`. You might be interested in AutoIt or AutoHotKey...? – aschipfl Nov 25 '15 at 12:48
  • @wOxxOm After writing `stdout` and `stderr` to a log file, it appears that `WScript` isn't a recognised command. @aschipfl Do you recommend either of the two tools that you mentioned? – helencrump Nov 25 '15 at 13:01

1 Answers1

1

Here it is a way with sendKeys.bat

start /max notepad myFile.txt
call sendkeys.bat "myFile.txt" "^{END}"
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • 1
    I'm getting a `Microsoft JScript runtime error: Invalid procedure call or argument` error at the part where `sh.SendKeys(keys)` is called. Is there something that I'm missing? – helencrump Nov 25 '15 at 13:58
  • @helencrump - is there anything else in the error message? like the line where the problem occurs? – npocmaka Nov 25 '15 at 14:03
  • 1
    @helencrump - ah.It works from command line but fails if called from bat. Something need to be escaped .Will update soon. – npocmaka Nov 25 '15 at 14:09
  • 1
    @helencrump - sendKeys updated on github. The problem is that `^` is a metasymbol in javascript regular expressions and needed extra care (at least I think this was the reason). – npocmaka Nov 25 '15 at 14:19
  • It worked once, but subsequent runs of my batch script result in `Failed to find application with title myFile.txt` - do I need to wait before sending the keys, or is there something else wrong? – helencrump Nov 25 '15 at 14:36
  • @helencrump - if the file is minimized the `AppActivate` activate function wont work . BTW - are you using windows 10 , because seems the first issue is related to a windows 10 bug? – npocmaka Nov 25 '15 at 15:05
  • No, I'm closing the file before I run the script each time. I'm on Windows Server 2012, so no. :) – helencrump Nov 25 '15 at 15:08
  • 1
    @helencrump - the second reason that `AppActivate` could fail is if the active window is already `myFile` . So for the second calls you can use `call sendkeys.bat "" "^{END}"` which will preventing changing the active window. – npocmaka Nov 25 '15 at 15:17
  • and `call` [doubles the caret](http://stackoverflow.com/questions/4094699/how-does-the-windows-command-interpreter-cmd-exe-parse-scripts/4095133#4095133) which was the problem at first. – npocmaka Nov 25 '15 at 15:18
  • 1
    Great! Thanks for all of the help! :) – helencrump Nov 25 '15 at 15:22