I have a few scripts to help running a program in CMD. The first script that starts the sequence is run on startup. In the process, I'm running a VBScript with which I'm trying to focus on the CMD and make it fullscreen. However, sometimes it works and sometimes it doesn't and I'm not changing anything, just restarting the computer over and over again. It doesn't always focus on the cmd window so the problem should be with the AppActivate method.
Here is the code for the first script that runs on startup:
#echo off
TITLE masterBat
START "Master" /B /W myApp.bat
shutdown.exe /s /t 00
Here is the code for myApp.bat:
#echo off
cscript fullscreen.vbs
cd <path>
CMD /C <command>
exit
Here is the code for fullscreen.vbs:
Set ws = WScript.CreateObject("WScript.Shell")
ws.AppActivate "masterBat - myApp.bat"
ws.SendKeys "{F11}"
Set ws = Nothing
The title in the AppActivate call is the title on the cmd when I'm trying to make it fullscreen. I've also tried adding some WScript.Sleep calls in the script but it doesn't seem to help.
Wscript.sleep will make it worse. You don't want to sleep.
There are rules. Your program MUST obey one of these rules to be allowed to set the foreground window.
From https://learn.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setforegroundwindow
The system restricts which processes can set the foreground window. A process can set the foreground window only if one of the following conditions is true:
•The process is the foreground process.
•The process was started by the foreground process.
•The process received the last input event.
•There is no foreground process.
•The process is being debugged.
•The foreground process is not a Modern Application or the Start
Screen.
•The foreground is not locked (see LockSetForegroundWindow).
•The foreground lock time-out has expired (see
SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
•No menus are active.
An application cannot force a window to the foreground while the user
is working with another window. Instead, Windows flashes the taskbar
button of the window to notify the user.
What this means is you have two seconds (FOREGROUNDLOCKTIMEOUT) from when your script starts to set another window if your script doesn't have a user interface (as it can't be the foreground window). A message box as the first thing you do will comply (till something else happens). Also Wscript has a message box that times out - WshShell.Popup.
This is to prevent slow starting programs, when the user has got bored and is now working in another program, from then stealing the focus.
Preventive comment Normally people want to argue with me about this. Complain to Microsoft not me. And you can't argue with rules. You can just comply with them.
Related
Hope I'm in the right place here, wasn't sure where to post and this seemed the most likely
I use a cmd box to run a serial flasher program for repairing wifi boards, the way it works is this.
open a command prompt and change to the working directory
enter the command
sfh_DM36x.exe -nandflash -v -p "COM3"
this starts the flashing program, however, due to the way it works, the program when finishing the first time just loops with bootme bootme, this is normal behavior.
Normally I just hit CTRL+C and exit the batch file.
The same command is run again and this time the flashing process completes and you are returned to the C:\ prompt.
Now you have the information here is what I am trying to achieve.
Launch a batch file that runs the runflash.bat, kill the runflash.bat and then run runflash.bat again.
So far I have gotten as far as this (start.bat)
start c:\users\jud\desktop\runflash.bat
timeout /t 5
This runs a new CMD prompt and opens the runflash.bat
I've then entered a timeout of 5 seconds (long enough for the first attempt to finish)
at this point, I wanted to kill the currently open CMD prompt and then execute runflash.bat again.
at this point I'm stumped, any attempt to kill the second CMD prompt just kills the original, I've looked at taskkill but the PID changes each time I run the script, as well as other suggestions I've come across during google searches, most just relate to killing batch files manually or killing a CMD prompt where a batch file has finished.
Can this be done, the idea is to pass the file out to users who aren't familiar with CMD prompt workings as an all in one solution.
Provide the new process with a unique title (see start /? - the first quoted parameter is set as a title). Takskkill can then identify and close the process with its title:
start "JudRunFlash" "c:\users\jud\desktop\runflash.bat"
timeout /t 5 >nul
taskkill /fi "WindowTitle eq JudRunFlash"
I am running a coded ui script in infinite loop as per my requirement. I can start the coded ui test using the the mstest.exe.
I wanted to know is there any way using which I can stop the coded Ui test, any command that I can use in the batch file to stop the coded ui script?
You can try this little batch to kill any process :
#echo off
Title Process Killer
Mode con cols=72 lines=5 & Color 0C
set Process2Kill=mstest.exe
Taskkill /IM "%Process2Kill%" /F
pause
And if you want to see more features like how to start a process and how to kill one process or multiple processes at once that interact with user input with a dynamic menu you should take a look at this post ==> How to check and correct user input when he omit the extension .exe to kill the process?
I want to create a batch file to do the following:
Start selenium server(webdriver-manager start)
Run Protractor tests(protractor conf.js)
Stop Selenium server()
This needs 2 different command prompts since webdriver-manager start will keep running and simultaneously the tests need to be executed
I have achieved the following so far. I have created a .bat file with the following contents:
start runTests.cmd
webdriver-manager start
Ctrl-C(**DOES NOT WORK**)
However, I am not able to figure out a way to shutdown the Selenium server(which is achieved by pressing Ctrl+C on the same window on which the webdriver-manager start command is executed)
You can generate keystrokes using VB Script, which can be called from a batch file.
I followed this post: http://www.w7forums.com/threads/f5-key-via-batch-file.18046/, substituting {F5} with ^{C} for Ctrl+C. So my file looks like:
Set objShell = CreateObject("WScript.Shell")
objShell.SendKeys "^{C}"
You need to save this file with a ".vbs" extension.
I also found from this answer VBScript - switching focus to window using AppActivate how to set the focus to another window (which you know the title of). Doing this first, you can direct your keystroke to the appropriate window, so:
Set objShell = CreateObject("WScript.Shell")
objShell.AppActivate "Untitled - Notepad"
objShell.SendKeys "^{C}"
I experimented with an empty instance of Notepad, so you'll need to change the window title string appropriately. (Ctrl+C doesn't do anything in Notepad, but Alt+F4 closes it, so I used "%{F4}" to test it.)
Then you can simply call the VBS file from your batch file to run it.
I have a simple batch file in windows that I run on startup that presents the user with a menu to start certain applications. However by default, whenever I open one of these applications the focus goes to that window and off of the batch file. Is there a way to maintain, or re-divert focus onto the batch window?
Thanks
EDIT: Got it to do what I wanted. Used foxidrives suggestion to start them minimized but they were still taking focus. So I made a short VBScript to make the cmd window the active window after each call and the combination of the two worked. Thanks!
There is no command to steal the focus. As Bill_Stewart posted, that would be a dangerous feature that grants the program too much power over the user. You can however start the applications minimized (they will still be the active window), and then build and call a VBScript to make your batch window the active window:
start "" /MIN "application.exe"
cscript /nologo myVBScript.vbs
myVBScript.vbs
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "myBatchFile"
WScript.Sleep 2000
WshShell.AppActiavte "myBatchFile"
I've read that several people have had trouble with AppActivate on Windows 7. It was not functioning for me, and instead of bringing my target window to the foreground it just blinked in the task bar. Calling it a second time however, for some reason, brought it to the foreground and made it the active window, which is what I wanted. Hope this helps anybody else with a similar issue.
you can't lock the focus to your command prompt window. But what you could do is to set the TopMost flag of the command prompt window. There is a Win32 function called SetWindowPos which does that. Maybe there are some ready to use command line tools around which are doing this for you. Or, if you have visual studio installed, try to compile this one here: How to set a console application window to be the top most window (C#)?
If you use the start command with the /min switch then the application will be minimised and the batch file should remain visible:
#echo off
pause
echo launching notepad
start "" /min notepad
echo notepad is active
pause
I want to make a automated program in .bat. The program needs to run a command. However, the command must be run from a custom CMD.
If I open a regular CMD, the commands that I will do:
C:\Hardware\bin\StartCustomCMD.bat init (This is the first thing I will type. It starts the custom CMD.)
bb autobuild (This is the second thing that I will type. The command goes into the custom CMD)
You can probably tell that I did not write these scripts. I am trying to set this up in Windows Scheduler so that the script gets run automatically every day. Any help on how can I do that?
Thanks.
Make yourself a new batch file, and embed the other things into it, and then run that.
#echo off
call C:\Hardware\bin\StartCustomCMD.bat
bb autobuild
If bb it itself a batch file, then use call on it also. What call does is execute another batch file, and then continue processing. If you don't use call, when you run one batch file from another, the latter 'takes over' and the caller does not continue.
To do this you can use the timeout and goto command. Timeout waits a period of time in seconds but it can be skipped by pressing any key while cmd is open at the top layer. If you can see cmd press its icon on the desktop until you can not see it. Then using the goto command you can go to the top line. So here would be your script:
:start
C:\Hardware\bin\StartCustomCMD.bat init
bb autobuild
timeout 86400
goto start
You already know what the first two commands do, but timeout 86400 waits exactly one day then the goto start command goes to the first command so that gets repeated. If you need to add any more commands then put them above the timeout 86400 command.