I have an existing MATLAB program which after a keystroke displays an image on the left or right of the screen. The user then indicates via arrow key if the image is on the left or right. The program actually does a lot of other things, but that's the gist. This is for a WinXP computer using MATLAB 2008A.
I would like to do a large number (thousands) of iterations for this program in an attempt to determine why some computers running this program have dying graphics cards. I've already made a number of improvements to simplify existing code and reduce computational time, but I need a to test and show the improvement. GPU-Z records all the hardware variables I'm interested in for now, so I need a keypress script capable of supplying input to the MATLAB program automatically. Ideally, this would be a stand alone macro capable of being run completely independently.
After reading this question, I attempted to modify the batch script as follows:
#if (#CodeSection == #Batch) #then
#echo off
rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=CScript //nologo //E:JScript "%~F0"
rem Start the other program in the same Window
start "" /B cmd
%SendKeys% "echo off{ENTER}"
set /P "=Wait and send a command: " < NUL
ping -n 100 -w 1 127.0.0.1 > NUL
%SendKeys% "2"
set /P "=Wait and send a command: " < NUL
%SendKeys% "3"
ping -n 30 -w 1 127.0.0.1 > NUL
%SendKeys% "2"
set /P "=Wait and send a command: " < NUL
%SendKeys% "1"
goto :EOF
#end
// JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
In testing, the batch code seems to appropriately enter keystrokes into the active window (cmd, text files, browsers, etc). However, if I attempt to run my MATLAB program simultaneously, the batch execution stalls until the MATLAB program ends. Obviously this is useless to me in the current form.
I'd appreciate any suggestions on where to go from here. Is batch scripting even a viable solution for this kind of automation? If not, any suggestions? I'd prefer to stay away from 3rd party programs as much as possible, but if it's the easiest solution I'll consider it.
Related
I am trying to write a batch script that is as universal across Windows versions as possible (or at least from XP to 10). Everything is compatible so far (just some echoes and variable setting), except it uses the TIMEOUT.EXE command, which isn't available in XP or below.
I tried copying the exe over to no success. I was wondering if, through some clever coding, if this is possible. I basically need it to wait X amount of seconds before continuing, or allow a keypress to continue.
I tried using sleep.exe from the server 2003 utilities pack while piping it to set /p "=" and vice versa, but that didn't work either.
Any help is appreciated.
There is the choice command command that offers a default option together with a timeout.
For instance:
rem /* Wait for 10 seconds and take the default choice of `0`;
rem you can interrupt waiting with any of the keys `0` to `9` and `A` to `Z`;
rem you cannot use punctuation characters or white-spaces as choices: */
choice /C 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ /D 0 /T 10
rem // The `ErrorLevel` value is going to be set to a non-zero value.
Not the greatest of tools, but using choice with a custom message and a timeout of (5 seconds in this demo), with keystroke interrupt (any key, besides Enter esc etc.)
#echo off
choice /c qwertyuiopasdfghjklzxcvbnm1234567890 /cs /n /M "Timeout is 5 seconds.. press any key to Continue." /D c /T 5
echo 1 > null
there are a lot of ways. PING seems to be the most popular. You can try also
with w32tm
w32tm /stripchart /computer:localhost /period:5 /dataonly /samples:2 1>nul
or wtih typeperf:
typeperf "\System\Processor Queue Length" -si 5 -sc 1 >nul
with mshta:
start "" /w /b /min mshta "javascript:setTimeout(function(){close();},5000);"
I am probably missing the right vocabulary to talk about this problem more succinctly so excuse me if I'm a little wordy here. Under Windows 10 I have a program that runs inside a CMD command prompt It's an executable called OpenSim and it has it's own extensive command set, including 'shutdown', which initiates a graceful termination of the processes therein, closes SQL connections etc, then finally closes the CMD command window. I also have a CMD .bat file that is activated by my UPS when the power goes down that will of course open it's own window, and then does some housekeeping before closing down the hardware. One thing I want the .bat file to do is to somehow insert a 'shutdown'command into the other window's process. Is that possible? If so, how? Please assume I am a total newbie at this and you won't go far wrong. Thank you.
EDIT It looks like creating a file to flag the closedown event taking place is the only (and I guess rather primitive) way to do this. So, building on what others have said in stackoverflow, I have the following now. When I run it to test it waits - it doesn't. It runs right through to the end, running 'shutdown', even though the UPSFLAG.TXT file does not exist. What's going wrong?
echo Waiting for UPS Power Down Signal.
echo =================================
#ECHO OFF
SET LookForFile="C:\Opensim OSGrid\UPSFLAG.TXT"
:CheckForFile
IF EXIST %LookForFile% GOTO FoundIt
REM If we get here, the file is not found.
REM Wait 10 seconds and then recheck.
REM If no delay is needed, comment/remove the timeout line.
TIMEOUT /T 10 >nul
GOTO CheckForFile
:FoundIt
ECHO Found: %LookForFile%
rem Tidy up
del "C:\Opensim OSGrid\UPSFLAG.TXT"
shutdown
Adding double quote after the = will save your variable as that "C:\Opensim OSGrid\UPSFLAG.TXT" which you do not want. rather you want to store it as C:\Opensim OSGrid\UPSFLAG.TXT so move the quote to before lookforfile.
Also, you created a variable for the file, so you might as well use it in the delete.
Finally, as a safety measure, always put an exit after a goto. That will ensure the system exists should there be a problem in the script and you can make sure you do not delete files or shutdown the system when it was not planned for.
echo Waiting for UPS Power Down Signal.
echo =================================
#ECHO OFF
SET "LookForFile=C:\Opensim OSGrid\UPSFLAG.TXT"
:CheckForFile
IF EXIST "%LookForFile%" GOTO FoundIt
REM If we get here, the file is not found.
REM Wait 10 seconds and then recheck.
REM If no delay is needed, comment/remove the timeout line.
TIMEOUT /T 10 >nul
GOTO CheckForFile
exit
:FoundIt
ECHO Found: %LookForFile%
rem Tidy up
del "%LookForFile"
shutdown
I used the sample project
https://developer.chrome.com/extensions/samples
I am able to run the python native app.
Is there any way to get the message within native-messaging-example-host.bat
I don't want to load python script
What I want to do here is
send message from chrome {text: "xyz.bat"}
and the batch file should run START xyz.bat
You should not approach this problem from the batch file standpoint, as in lieu of my solution, it requires the program to be run upfront, which in most applications is depreciated in favor of running it in the background. However if you still want to know how you could potentially do it in batch...
If you can pass the message to a blank html page (not currently sure how you can or want to do it this way), where the only thing on that html page is your runme.bat we can run a program that would copy the page, open a text file and paste it inside, close the text file, and run the batch with input from it. So code wise,
#if (#CodeSection == #Batch) #then
#echo off
set SendKeys=CScript //nologo //E:JScript "%~F0"
rem below copys everything on the page, and closes it
%SendKeys% "{TAB}"
%SendKeys% "^{A}"
%SendKeys% "^{C}"
%SendKeys% "^{W}"
rem open text file,wait for it load, paste clipboard, save and exit
start newreadforme.txt
timeout /nobreak /t 5
%SendKeys% "^{V}"
%SendKeys% "^{S}"
timeout /nobreak /t 2
%SendKeys% "^{W}"
start program.bat
goto :EOF
#end
// JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
then in your batch file
#echo off
set /p x=<newreadforme.txt
start %x%
This code will run simulated keystrokes on the opened page to copy its contents and relay to a text file to be referenced from another batchfile. But you should only use this method as a last resort as this approach is a TERRIBLE way to solve your problem. My code requires you to keep the webpages open and upfront and make sure no one interferes with the program during its execution. So if a user is using the computer the time its running, then they may accidentally mess with the inputs.
On top of that, already you need to be modifying webpages to achieve your end result so you probably should use a language that supports html to filesystem operations. Nodejs can provide a nice interface between the file system and html pages that you may decide to pass. How you handle the webpage filled with the message i am not sure of but you should most certainly avoid using batch to do what you ask in favor of more html friendly languages
I have written a batch script in an interactive mode, for making some tasks.
Sometimes, These tasks takes a long time to be finished, and then the batch asks if the user wants to go on to the next task, or back to the Batch's Main Menu or... etc
Now, what I want to do, is to add an "Interactive Alarm" command, that sounds a small short beep (Ex: Like the one when we turn on our PCs), to alert the batch user for new questions .
I don't know if this is possible or not, but the most important thing for me, NOT to use a GUI application like WMP or so..
I just want to do this from the Background, even If that beep has to be made from the free speaker, or by using a Third-Party CLI Application (Btw, I've Cygwin installed on my Win7-x64) .
Please note that, I will add that alarm command exactly before the interactive questions, waiting for user's answer to get to the next stage, so I can't just finish the batch, by making a real error beep !
So, would somebody please tell me how to do this ?
Appreciate your help :)
WARNING: rundll32.exe Kernel32.dll,Beep 750,300 no longer works well from the command line on modern windows systems as rundll32 no longer accepts integer values (again, through the command line) and this will play the beep with the default values which is too long (and frequency is irritating):
REM Again, with warnings about running this from the command line...
rundll32.exe Kernel32.dll,Beep 750,300
or
rundll32.exe cmdext.dll,MessageBeepStub
or
rundll32 user32.dll,MessageBeep
With rundll functions you won't need special symbols like ^G. With the first method you can also set the frequency and the time you want to beep, though see the warning that those parameters no longer work on modern systems from the command line and will instead play the annoying defaults.
UPDATE
other options are:
powershell "[console]::beep(500,300)"
or using systemSounds.bat
call systemsounds.bat beep
The capability of beeping depends on the mainboard and if the mainboard has a system speaker - which has increasingly become a rarity as systems tend to depend solely on "normal" speakers instead. An alternative is to play sound through those speakers. Here are some options:
Using the speaking capabilities of the SAPI.SpVoice:
mshta "javascript:code(close((V=(v=new ActiveXObject('SAPI.SpVoice')).GetVoices()).count&&v.Speak('beep')))"
Here this is wrapped in a batch file and the words can be passed as an argument.
SAPI.SpVoice can be used for playing wav files and you have some packaged with the default Windows installation. You can use this script:
spplayer.bat "C:\Windows\Media\Windows Navigation Start.wav"
Another option: Using the windows media player active-x objects to play a sound. On Windows XP it was not installed by default but I think for the newer Windows versions it is. It also can play mp3 files:
call mediarunner.bat "C:\Windows\Media\Ring03.wav"
And one that is a little bit obscure - using the <bgsound> tag from internet explorer (which also can play mp3 files). Here's the script:
call soundplayer.bat "C:\Windows\Media\tada.wav"
And here's a way to use the BEL character to produce sound with easy to copy-paste code (I've called it a beeper.bat):
#echo off
setlocal
::Define a Linefeed variable
(set LF=^
%=-=%
)
for /f eol^=^%LF%%LF%^ delims^= %%A in (
'forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(0x07"'
) do echo(%%A
It's not possible to type the BEL directly in (for example) notepad.
To get it, type echo ^G>>yourbatch.bat on the command line (don't type ^ G, but <Control>-G, which will be shown as ^G on the screen). That puts a strange looking character to the end of your file. That's the BELcharacter 0x007 ("control-G"). Just copy/move it to any echo command, you like. Also
set /p "input=^Ggive value: "
is possible (where the ^G represents that strange char)
The following can be used to issue a beep
without pausing the script
without creating a new line.
without requiring the use of a non-printable character
Echo/| CHOICE /N 2> nul & rem BEL
It is a deliberate misuse of the choice command, that Echo's nothing via a pipe to Choice, causing a non-breaking error. STDERR is redirected to nul, and the default choice prompt is suppressed via the /N switch, meaning no new line is output.
If for some reason you wanted to reuse this annoying tone throughout a script, you could define it as a macro
Set "BEL=Echo/| CHOICE /N 2> nul"
%BEL%
#echo off
echo BEEP.BAT by CSS---
echo PRESS ANY KEY TO HEAR A BEEP...
PAUSE>NUL
ECHO
echo I BEEPED
PAUSE
there is an ASCII control code ^G after the echo. Just copy this code, and save it as ASCII/ANSI using a text editor.
use ECHO command to echo a CTRL G
I think the better solution is echoing a ^G to a file from the cmd prompt and then type that file from within the script, that way you don't need to include control characteres in the batch file itself:
C:\> echo ^G>beep.snd
Now there's an ASCII 007 char in the "beep.snd" file, then from your .bat file all you have to do is type it or copy to the screen:
type beep.snd
or
copy beep.snd con > nul
I tried all the options above in Win 10. I settled with this
powershell.exe [console]::beep(500,600)
So programmatically in node.js it would look like this (python or C would be similar)
require("child_process").exec("powershell.exe [console]::beep(500,600)");
A bit late to the party, but I find this variation on #npocmaka version works for me with Windows 10:
REM This captures the Bell as a variable.
for /f %%g in ('%__APPDIR__%forfiles.exe /p "%~dp0." /m "%~nx0" /c "cmd /c echo 0x07"') do set "bel=%%g"
REM This produces the Bell sound.
set /P "=%bel%"<NUL
This works for me..
there was a special character in line 4 which stackoverflow was omitting,
code's pasted here:
hashb.in/long
and line 5 and 6 can be used interchangeably of course.
I've recently learned that I can control the size of the CMD window running my program with mode x,y. However I just as recently noticed that this sets the buffer size, and the window will adjust to match or max out at the screen size.
I would like to use mode 100,50 for the window size, but I also want to keep a arger buffer - for development at least I want mode 100,9999.
Is there any way to do this?
I don't think there is a native batch command that gives independent control of buffer and window sizes. .NET can control both, but I don't think VBScript or JScript can access that functionality. But powershell can :-) See How Can I Expand the Width of the Windows PowerShell Console?
Thankfully, the new settings are preserved in the CMD window when PowerShell exits.
It is important that the window size is always less than or equal to the buffer size. To simplify things, I first use MODE to set both the window and buffer to the desired window size, and then I use powershell to set the buffer size.
Here is a simple conSize.bat hybrid batch/powershell script that takes the sizes as parameters:
#echo off
:conSize winWidth winHeight bufWidth bufHeight
mode con: cols=%1 lines=%2
powershell -command "&{$H=get-host;$W=$H.ui.rawui;$B=$W.buffersize;$B.width=%3;$B.height=%4;$W.buffersize=$B;}"
To get your desired size, you would simply use
call conSize 100 50 100 9999
There's a 'ConSetBuffer' binary available that does specifically this, and I've found it to work reliably. It and related console utilities are available at the 'conutils.zip' link on this page.
I have written a tiny application for Windows that allows to "maximize" the window and buffer. It could be easily extended to allow passing parameters with custom values.
here another variant:
/*
#echo off & mode 100,50
set "cscfile="
set Pathfile="%WinDir%\Microsoft.NET\Framework\csc.exe"
for /f "delims=" %%a in ('dir /b /a-d /s %PathFile%') do set "cscfile=%%a"
if defined cscfile (
%cscfile% /nologo /out:"%~0.exe" %0
) else exit /b
"%~0.exe"
del "%~0.exe"
cmd /k
*/
using System;
class Program
{
static void Main()
{
Console.SetBufferSize(100, 9999);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Current Logged UserName: " + Environment.UserName);
Console.ResetColor();
Console.WriteLine("[Enter] to continue");
Console.ReadLine();
}
}
The answer to this question didn't work as I got an error regarding variables sent to the PowerShell command, so I modified to work.
The PowerShell command simply adds uiHeightBuffer to the uiHeight variable to show the scroll bar.
Edit: Added the height buffer as the 3rd parameter and TRUE or FALSE as a 4th parameter to enable or disable the scrollbar.
I found this useful when exiting a script with a small message when there's no need for the scrollbar in this case.
Also to enable UTF-8 encoding you have to set the default at the start of the `:CMDSIZE' call and set it at the end, otherwise, the font size goes a bit funny.
Omitting chcp 850 >NUL & and chcp 65001 >NUL & are what you'd do if you didn't want to affect this.
This works in Version: 1909 of Windows 10.
#echo off
title %~nx0
rem Based on this thread here: https://stackoverflow.com/a/13351373
goto MYROUTINE
:CMDSIZE
chcp 850 >NUL & set "uiWidth=%1" & set "uiHeight=%2"
mode %uiWidth%,%uiHeight%
if %4==TRUE (set /a "uiHeightBuffer=uiHeight+%3")
if %4==TRUE (powershell.exe -ExecutionPolicy Bypass -Command "&{$H=get-host;$W=$H.ui.rawui;$B=$W.buffersize;$B.width=%uiWidth%;$B.height=%uiHeightBuffer%;$W.buffersize=$B}")
if %4==FALSE (powershell.exe -ExecutionPolicy Bypass -Command "&{$H=get-host;$W=$H.ui.rawui;$B=$W.buffersize;$B.width=%uiWidth%;$B.height=%uiHeight%;$W.buffersize=$B}")
chcp 65001 >NUL & goto :EOF
:MYROUTINE
call :CMDSIZE 255 44 222 TRUE
title Do your routine here...
echo Do your routine here...
echo/ & pause
goto :EXITROUTINE
:EXITROUTINE
call :CMDSIZE 40 2 0 FALSE
title Exiting routine message...
echo Exiting routine message...
set /p "=" <NUL
ping localhost -n 3 >NUL & exit
I have found a way to resize the buffer size without influencing the window size. It works thanks to a flaw in how batch works but it gets the job done.
mode 648 78 >nul 2>nul
How does it work? There is a syntax error in this command, it should be "mode 648, 78". Because of how batch works, the buffer size will first be resized to 648 and then the window resize will come but it will never finish, because of the syntax error. Voila, buffer size is adjusted and the window size stays the same. This produces an ugly error so to get rid of it just add the ">nul 2>nul" and you're done.