Pause a running Batch script and then restart after second key stroke - batch-file

I have a batch file that will run a macro. The macro will automatically run all day but I want to be able to start and stop it with a simple keyboard character. Is there a way in the code that I can add a "Press S to stop, and R to Resume"? The resume would start my vbs file again.
powershell -window minimized -command ""
#echo off
pause
cls
:start
cscript DisplayBoard2.vbs "S:\filepath\macro.xlsm"
goto start

You can use the choice command to do what you want.
Note: the script window has to be active for it to work. Cmd has no possibility to "intercept" keypresses if the window is not active.
#echo OFF
:start
echo running. Press 'S' to suspend.
REM your payload here
choice /c SX /n /t 1 /d X >nul
if errorlevel 2 goto :start
:pause
echo paused. Press 'R' to resume.
choice /c RX /n /t 1 /d X >nul
if errorlevel 2 goto :pause
goto :start
What (I guess) you really want, is called a keylogger (to catch keyboard input regardless of what application has the focus). See this site for how it basically works.

Related

Vbscript ran from batch file not sending keystrokes to batch file

#echo off
timeout /t 2 >NUL
cls
cd %temp%
set "var1=%random%%random%"
echo >%var1%.vbs set shell = CreateObject("WScript.Shell"):shell.SendKeys "%{ENTER}" & %var1%.vbs
pause
This is my code. What I basically want the batch file to do is to open itself up in fully, fully fullscreen (im talking f11 fullscreen mode). You can fullscreen a batch file on windows 10 with ALT+ENTER. So I write vbs send keys to do that... The % and ENTER is to send ALT+ENTER... when ran, I don't get an error with the vbs... just no fullscreen... why?
I created this batch and i tested it on my Windows 10 and it works 5/5
#echo off
Set "MyTitle=FullScreen"
Title %MyTitle% & color 0A & cls & cd %temp%
set "vbsfile=%~n0.vbs"
echo >"%vbsfile%" set shell = CreateObject("WScript.Shell"):shell.SendKeys "{F11}"
REM Timeout /t 1 /nobreak>NUL
Wscript.exe "%vbsfile%"
Ipconfig /all
pause
And here is another batch that can use this as sub to be called when you want to fullscreen or come to the back to normal screen :
#echo off
REM Here we call the sub :FullScreen to make it fullscreen
Call :FullScreen
Ping www.google.com
Tracert www.google.com
Call :FullScreen REM You come to the normal when we call it again the sub :FullScreen
ipconfig /all
Pause & Exit
REM -------------------------------------------------------------------------------
:FullScreen
Set "MyTitle=FullScreen"
Title %MyTitle% & color 0A & cls & cd %temp%
set "vbsfile=%~n0.vbs"
echo >"%vbsfile%" set shell = CreateObject("WScript.Shell"):shell.SendKeys "{F11}"
REM Timeout /t 1 /nobreak>NUL
Wscript.exe "%vbsfile%"
Exit /b
REM -------------------------------------------------------------------------------

Batch code does not work with case-insensitive input for a variable

I have created the below code to execute multiple commands depending on the user input but the input is case-sensitive.
I have tried the /I switch with the if statement but that does not work either.
Below is the code:
#Echo off
SET /P uname=Launch Outlook in safe mode:
IF "%uname%"==Yes GOTO Error
IF "%uname%"==No GOTO start
:Error
taskkill /IM Outlook.exe /f
cd c:\Program Files (x86)\Microsoft Office\root\Office16
start Outlook.exe /safe
#echo The Script is running please wait && timeout /t 20
taskkill /IM Outlook.exe /f
cd %Homepath%
cd Appdata\Local\Temp
echo.>myfile.txt && #echo Thanks for using the script,You may close this window.and continue. > myfile.txt
start outlook.exe
timeout /t 8
start notepad myfile.txt
:End
:start
echo "No valid options"
echo "This window would close in 10 seconds" && timeout /t 10
:End
I would use the choice command instead
echo Y] Yes
echo N] No
choice /c yn /n
if %errorlevel%==1 goto yes
if %errorlevel%==2 goto yes

Why is character A not assigned to environment variable using set %Variable%=A in batch file?

I have a batch file that detects if the user presses key A and if so should change the char variable to A instead of . But for some unknown reason that does not work. I don't know why.
Here is the code:
#echo off
set char=.
:start
cls
batbox.exe /k
if %errorlevel%==97 set %char%=A && goto next
goto start
:next
echo %char%
pause
If you need the batbox command info, here is the link.
Looks like your trying to do something like this:
#Echo off
Set Char=.
:Start
Cls
Choice /C an /T 10 /D n /N /M "Press a or wait"
Echo %Char%
Pause
Goto :start
You only have to throw in some of your own code to make it work like you want.
Quick run down from here /C is choices /T is time /D is what answer it does when the time runs out /N is for not showing the available answers and /M is message for the user.
Source: Batch Choice Information

BAT commands to recognize if process is running and display prompt

I need to create a bat that recognizes if a process is running, and if the process is running I need to add a prompt asking if the user wants to end the process to continue with some other tasks.
I have the command to recognize if a process is running(this is the one that worked for me):
#echo off
tasklist /nh /fi "imagename eq vmware-view.exe" | find /i "vmware-view.exe" >nul && (
echo VMWare Horizon is running
) || (
echo VMWare Horizon is not running
)
pause>nul
and I have the command to prompt for an imput:
#echo off
setlocal
:PROMPT
SET /P AREYOUSURE=Are you sure (Y/[N])?
IF /I "%AREYOUSURE%" NEQ "Y" GOTO END
But I don't know how to put those 2 together to say that, if the process is running, display prompt asking user if bat can end process to continue with an update, and if not running just go on with update.
could you please assist?
thanks in advance
I remember from the old DOS days that you could use GOTOs.
for instance a bat file containing the following:
#ECHO OFF
ECHO something
GOTO LABEL0
ECHO something else
GOTO LABEL1
:LABEL0
ECHO some label
:LABEL1
ECHO some other label
will print:
something
some label
some othe label
you will not see
something else
because the first GOTO skipped to LABEL0
You can use a label like :QUIT or :END at the end of your bat and skip at the end if you have nothing to do.
Use ERRORLEVEL to see if TASKLIST and FIND got the process. Afterwards use CHOICE.
#echo off
set task=vmware-view.exe
tasklist | find /i "%task%" > nul
if %errorlevel%==0 (
echo task is running.
choice /c yn /t 10 /d n /m "should we kill it?"
if errorlevel 2 goto end
taskkill /f /im "%task%"
) else (
echo task is not running.
)
:update
echo updating...
:end

Automatically make a selection after 20 seconds

I made a custom start script for my Minecraft server so i could use more RAM.
When you stop the server, it asks if you want to restart or if you want to stop.
I can't get it to restart it automatically if you don't answer after 20sec
(in case of a crash). Please help.
#echo off
title minecraft-server-1.8.3
color 0A
prompt [server]:
cls
:start //starts the server
echo loading server...
java -Xms3G -Xmx3G -jar minecraft_server.1.8.3.jar nogui
cls
[code to restart the server when it didn't get an anwser in 20sec]
:choice //what to do when
set /P a=do you want to restart[Y/N]? the server stops
if /I "%a%" EQU "Y" goto :restart
if /I "%a%" EQU "N" goto :stop
goto :choice
:restart
cls
echo server will restart
TIMEOUT /T 5
cls
goto :start
:stop
cls
echo closing server
TIMEOUT /T 5
exit
Instead of set /p use choice:
choice /c YN /t 20 /d Y /m "Do you want to restart "
/c YN states the answer set (Y or N, case insensitive by the way).
/t 20 states a timeout of 20 seconds.
/d Y states the default answer Y once the timeout expires.
/m "..." states a prompt.
choice sets errorlevel to the index of the input (Y:1, N:2) so to check the user input use:
if %errorlevel% equ 1 goto :restart
if %errorlevel% equ 2 goto :stop
You can find more information about choice at http://ss64.com/nt/choice.html or type choice/?

Resources