How can I write batch output to my text cursor?
Asked Answered
T

2

6

In Windows 7, ultimately I want to bind a shortcut key (Ctrl+Alt+D) to spitting out a date-time stamp of the form 20120913 1232.

The step I'm getting hung up on is having a batch file write anything to my text cursor. Currently I'm piping it to clip.exe, then I paste, but I'd like to eliminate the middle step of the clipboard. If this isn't possible, is there another way around my problem?

echo %date:~-4%%date:~4,2%%date:~7,2% %time:~0,2%%time:~3,2% | clip
Tobytobye answered 13/9, 2012 at 16:42 Comment(0)
P
0

You can't do it from cmd. You need to use VBS, Js or PowerShell. Here's a simple VBS script that does what you want. Just create a shortcut on the desktop that runs this VBS file and assign a shortcut key to it

Set WshShell = WScript.CreateObject("WScript.Shell")
' Switch back to the previous window. Use Alt+Esc because Alt+Tab sometimes doesn't work
' WshShell.SendKeys "%{TAB}", True
WshShell.SendKeys "%{ESC}", True

' Get the time string
Dim dt, datetime_str
dt =  now
datetime_str = Year(dt) & Right("0" & Month(dt), 2) & Right("0" & Day(dt), 2)
datetime_str = datetime_str & " " & Right("0" & Hour(dt), 2) & Right("0" & Minute(dt), 2)

' Send the date string to the target window
WshShell.SendKeys datetime_str

However Autohotkey (AHK) would be a much better solution and works much faster. Here are 2 example functions that will type the datetime string to the current app

; Map Ctrl+Alt+T to send the date string
^!t::
FormatTime, currentdate,, yyyyMMdd HHmm
SendInput %currentdate%
Return

; Map Ctrl+Alt+D to send the date string
^!D::
SendInput %A_YYYY%%A_MM%%A_DD% %A_Hour%%A_Min%
Return

Save the above script as *.ahk and run it. AHK will run in the background and listen to the key sequence to do the desired action

Prytaneum answered 20/4, 2021 at 14:54 Comment(0)
A
-1

For OP/anyone interested: http://ahkscript.org

Auto Hotkey might be what you were after. Either a vanilla complementary cmd .bat or a standalone AHK script should be able to pull data from STDOUT and SET your date syntax up, AND set the hotkey all from 1 script.

The "hotkey" section of this question might be why it hasn't been answered before, not something that can be done in Windows CLI afaik, you need "Shortcut key remapping software" which is exactly what Auto Hotkey is designed for :)

It has been a long time since I researched Shortcut key remapping in Windows, but I do remember AHK being only a handful of programs able to remap hard-coded shortcut combinations like WinKey + R, unless you code one yourself (in C++ or similar)

I also recommend a thorough dig into Rob van der Woude's site, specifically the 2 articles about redirection:

http://www.robvanderwoude.com/battech_redirection.php

and down the bottom is a link to the "Redirection overview page" which is a particularly good "cheat sheet" for redirection commands/syntax.

Absolutely answered 29/9, 2015 at 14:3 Comment(1)
I believe the OP has created a desktop shortcut to a batch file, and then assigned it a Shortcut key. No need for additional software, but I don't think the OP's approach will permit inserting text into the active application.Hubing

© 2022 - 2024 — McMap. All rights reserved.