I have made a program in notepad which when runs allows the user to choose one of a select amount of options. Each option is a time that essentially counts down until when it hits 0 the computer locks itself.
Problems:
This has been created to allocate a set amount of time for my little brother however it can easily be stopped by just manually closing the program.
I wanted to know if I could therefore make it so after choosing an option the program would hide itself by running in the background or so if it is manually closed it would reopen and carry on counting down thank you.
Provided Example Code:
#echo off
cls
title Shutdown timer
color 0a
:start
echo
echo Choose Your Time allocated
echo 1. 35 Minutes
echo 2. 65 Minutes
echo 3. 95 Minutes
echo 4. 125 Minutes
echo 5. 1 Minutes
set /p choice=Type which number for your choice
if not '%choice%'== set choice=%choice:~0,1%
if '%choice%'=='1' goto :Choice1
if '%choice%'=='2' goto :Choice2
if '%choice%'=='3' goto :Choice3
if '%choice%'=='4' goto :Choice4
If '%choice%'=='5' goto :Choice5
echo "%choice%" is not a valid option. Please try again
You're looking to baby-poof a batch script? A simple solution is to simply hide the command window. This can be done by using a .VBS command shell to hide the command prompt process. Original post here.
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False
Above is the script to hide command windows in VBS. You can use this script by calling wscript.exe "Script" "Batch File". Pretty simple and easy way to do it without 3'rd party tools.
I combined your script to allow to create, export, use, and delete these files. Your Main shutdown script will also need exported to a new batch file to be called to by the VBS script. This can be done using a code block bellow
Rem | Create Timer.bat
(
Echo TIMEOUT /T %Time% /NOBREAK
Echo rundll32.exe user32.dll,LockWorkStation
Echo DEL "%%~f0"
)>> %Temp%\Timer.bat
This will create a new batch file containing the shutdown or lock commands. In this case rundll32.exe user32.dll,LockWorkStation will lock the workstation after X minutes by TIMEOUT /T
StartTimmer.bat:
#echo off
title Shutdown timer
color 0a
:start
cls
echo Choose Your Time allocated:
echo(
echo 1. 35 Minutes
echo 2. 65 Minutes
echo 3. 95 Minutes
echo 4. 125 Minutes
echo 5. 1 Minutes
echo(
set /p "choice=Type which number for your choice: "
if "%choice%"=="1" set "choice=35" & goto ChoiceSellected
if "%choice%"=="2" set "choice=65" & goto ChoiceSellected
if "%choice%"=="3" set "choice=95" & goto ChoiceSellected
if "%choice%"=="4" set "choice=125" & goto ChoiceSellected
If "%choice%"=="5" set "choice=1" & goto ChoiceSellected
goto start
:ChoiceSellected
Echo Now Starting Timmer For %choice% Minutes
Rem | Do math - Covert Seconds To Minutes
Set /a "Time=%choice% * 60"
Rem | Create Timer.bat
(
Echo TIMEOUT /T %Time% /NOBREAK
Echo rundll32.exe user32.dll,LockWorkStation
Echo DEL "%%~f0"
)>> %Temp%\Timer.bat
Rem | Export Hide Script & Use it
Echo CreateObject^("Wscript.Shell"^).Run """" ^& WScript.Arguments^(0^) ^& """", 0, False>> Hide.vbs
wscript.exe "%~dp0Hide.vbs" "%Temp%\Timer.bat"
del "%~dp0Hide.vbs"
goto :EOF
For help on any of the commands do the following:
call /?
set /?
for /?
if /?
find /?
So on.
Related
#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 -------------------------------------------------------------------------------
#echo off
:load
rem imitation of loading the os
color 70
ver
title boot
echo please wait...
ping localhost -n 3 >nul
cls
systeminfo
rem in here user types name he wants to be his account name
:login
title login
cls
date
cls
echo welcome to windows 71
echo before we bigen please type your name
set /P_name=here:
if %name%=admin goto admin
if not goto ms
rem ms=menu start
:ms
echo %time%
echo hello %name%
echo type HELP for list to commands
set /P_command=here:
if %command%=help goto help
if %command%=exit goto exit
if %command%=calendar goto cal
if not goto wc
rem wc=wrong command
:admin
echo hello %name% to the admin panel
echo type HELP for list to commands
set /P_command=here:
if %command%=help goto help
if %command%=exit goto exit
if %command%=calendar goto cal
So the problem is that it crashes after the :LOGIN part and I don't know what to do!
I'm trying to make an OS batch (something like MS-DOS), but it crashes after the "login" part.
I tried everything I could think of and it didn't work, also I want to make a save file so users can set a password for their "account".
As mentioned in above comments, you need to correctly use your variables, you can however use choice instead of set /p for your commands.
#echo off
:load
rem imitation of loading the os
color 70
ver
title boot
echo please wait...
timeout /t 3 >nul
cls
systeminfo
rem in here user types name he wants to be his account name
:login
title login
cls
date /t
timeout /t 2>nul
cls
echo welcome to windows 71
echo Before we begin please type your name
set /P "_name=here:"
if /i "_%name%"=="admin" goto admin
rem ms=menu start
:ms
echo %time%
echo hello %_name%
echo type HELP for list to commands
CHOICE /C HEC /M "Press H for Help, E to exit C for Calender."
goto opt%errorlevel%
:admin
echo hello %_name% to the admin panel
echo type HELP for list to commands
CHOICE /C HEC /M "Press H for Help, E to exit C for Calender."
goto opt%errorlevel%
:opt1
echo Help stuff goes here
goto :eof
:opt2
exit
:opt3
echo Calenders stuff goes here
Some things to note. You do not require to goto ms is the user is not admin as the statement for not being admin will not be met, we will automatically faal through into the ms label.
Notice where the problems were in your code. i.e if %name%=admin should be if "%_name%"=="admin" with double equal sign and the underscore in the name. It is also double quoted to ensure that we do a match without unwanted whitespace. Lastly /I option to catch ADMIN in any case.
See if /?, choice /? from command line for more help around these functions.
Okay this code is quite wrong.
I fixed it.
#echo off
:load
rem imitation of loading the os
color 70
title boot
echo please wait...
ping localhost -n 3 >nul
cls
rem in here user types name he wants to be his account name
:login
title login
cls
echo Welcome to Microsoft Windows 7!
echo Before we begin, please type your name.
set /p name=here:
if "%name%"=="admin" goto admin
if not "%name%"=="admin" goto ms
rem ms=menu start
:ms
echo %time%
echo Hello %name%
echo Type HELP for list to commands
set /p command=here:
if "%command%"=="help" goto help
if "%command%"=="exit" goto exit
if "%command%"=="calendar" goto cal
goto ms
rem wc=wrong command
:admin
CLS
echo hello %name% to the admin panel
echo type HELP for list to commands
set /P command=here:
if "%command%"=="help" goto help
if "%command%"=="exit" goto exit
if "%command%"=="calendar" goto cal
GOTO :ADMIN
Okay but you don't need to start systeminfo and all that.
I am wondering if there is a nice and clean way to add a "select all choices" when building a choice menu in batch.
Currently I have the choice setup as clear item 1, clear item 2, clear item 3 (clear all), exit, and a timeout that the user cannot see.
If I have to I will just re-add all my code to the "clear all" area. I was hoping to see if there was a way to just have the "clear all" use the defined 1 and 2 and then go back to :start like everything else.
Answer: I took Aacini's idea of the "set option" and came up with an even simpler answer. I Changed "GOTO :start"to"GOTO :ClearCache" for the "Clear All Options". Then I added a "IF %ERRORLEVEL% neq 3 GOTO :start" and after that a "GOTO :ClearCredentials". This allowed me to keep less lines than the set option and I didn't have to move my code around to have it pass to the next process.
This should allow for multiple but different clear all options for future items.
#ECHO OFF
:start
ECHO 1. Clear IE, Chrome, Temp Cache
ECHO 2. Clear Credentials in IE, Chrome, and Windows
ECHO 3. Clear All Options
ECHO 4. Exit
CHOICE /N /C:12345 /T 15 /D 5 /M "Type the number to choose an option."
IF %ERRORLEVEL%==5 GOTO TIMEOUT
IF %ERRORLEVEL%==4 GOTO Exit
IF %ERRORLEVEL%==3 GOTO ClearAllOptions
IF %ERRORLEVEL%==2 GOTO ClearCredentials
IF %ERRORLEVEL%==1 GOTO ClearCache
GOTO :start
:ClearCache
ECHO Clearing Cache...
<code here>
pause
cls
IF %ERRORLEVEL% neq 3 GOTO :start
GOTO :ClearCredentials
REM ===-----------------------
:ClearCredentials
ECHO Clearing Credentials
<code here>
pause
cls
GOTO :start
REM ===-----------------------
:ClearAllOptions
ECHO Clearing All Options...
pause
cls
GOTO :ClearCache
pause
REM ===-----------------------
:Exit
ECHO Exiting...
<code here>
pause
EXIT
REM ===-----------------------
:TIMEOUT
cls
ECHO Exiting because no choices were made in 15 seconds
:END
timeout /t 5
This is a very simple way to do that:
#ECHO OFF
:start
set option=0
ECHO 1. Clear IE, Chrome, Temp Cache
ECHO 2. Clear Credentials in IE, Chrome, and Windows
ECHO 3. Clear All Options
ECHO 4. Exit
CHOICE /N /C:12345 /T 15 /D 5 /M "Type the number to choose an option."
GOTO Option-%ERRORLEVEL%
:Option-3 ClearAllOptions
ECHO Clearing All Options
set option=3
:Option-1 ClearCache
ECHO Clearing Cache...
<code here>
pause
cls
if %option% neq 3 GOTO :start
REM ===-----------------------
:Option-2 ClearCredentials
ECHO Clearing Credentials
<code here>
pause
cls
GOTO :start
REM ===-----------------------
:Option-4 Exit
ECHO Exiting...
<code here>
pause
EXIT
REM ===-----------------------
:Option-5 TIMEOUT
cls
ECHO Exiting because no choices were made in 15 seconds
:END
timeout /t 5
I've been reading how to avoid spaghetti code in batch files.
In the example of what spaghetti code is, I realized that the batch file that I use when I logon almost fits this example. Could someone please help me make my batch file not have spaghetti code?
#ECHO OFF
CLS
:MENU
echo Welcome %USERNAME%
echo 1 - Start KeePass
echo 2 - Backup
echo 3 - FireFox
echo 4 - Exit
SET /P M=Please Enter Selection, then Press Enter:
IF %M%==1 GOTO StarKeePass
IF %M%==2 GOTO Backup
IF %M%==3 GOTO FireFox
IF %M%==4 GOTO :EOF
GOTO MENU
:StarKeePass
SET keePass="%USERPROFILE%\KeePass\KeePass-2.30\KeePass.exe"
SET kdb="%USERPROFILE%\KeePass\PasswordDatabase\PasswordDatabase.kdbx"
echo I'll start KeePass for You
START "" %keePass% %kdb%
GOTO MENU
:Backup
SET backup="%USERPROFILE%\backup.bat"
call %backup%
GOTO MENU
:FireFox
cd "C:\Program Files (x86)\Mozilla Firefox\"
start firefox.exe
GOTO MENU
In this case, if you want to use subroutines you should do this:
#ECHO OFF
CLS
:MENU
echo Welcome %USERNAME%
echo 1 - Start KeePass
echo 2 - Backup
echo 3 - FireFox
echo 4 - Exit
SET /P M=Please Enter Selection, then Press Enter:
IF %M%==1 CALL :StartKeePass
IF %M%==2 CALL :Backup
IF %M%==3 CALL :FireFox
IF %M%==4 GOTO :EOF
GOTO MENU
:StartKeePass
SET "keePass=%USERPROFILE%\KeePass\KeePass-2.30\KeePass.exe"
SET "kdb=%USERPROFILE%\KeePass\PasswordDatabase\PasswordDatabase.kdbx"
echo I'll start KeePass for You
START "" %keePass% %kdb%
GOTO :EOF
:Backup
SET "backup=%USERPROFILE%\backup.bat"
call %backup%
GOTO :EOF
:FireFox
cd "C:\Program Files (x86)\Mozilla Firefox\"
start firefox.exe
GOTO :EOF
Note that I changed a few things. Instead of goto... goto menu, you should use call :label goto :eof/ exit /b. Besides that, you had a spelling error StartKeePass, and instead of set variable="value", it's better to use set "variable=value". This will also accept spaces in the value, but it won't add quotes to your variable
Next time you should probably post this to code review, because these things aren't really errors
If you wanted to remove gotos altogether, you can simply call the script again to keep using it. Also, look into the choice command if you're using a version of Windows later than XP, since it will eliminate the need to check if the user entered invalid input.
#echo off
cls
echo Welcome %USERNAME%
echo 1 - Start KeePass
echo 2 - Backup
echo 3 - FireFox
echo 4 - Exit
choice /C:1234 /M "Please enter your selection: " /N
:: The first option listed by choice's /C option will return an errorlevel value of 1, the second 2, and so on
if %errorlevel% equ 1 (
SET keePass="%USERPROFILE%\KeePass\KeePass-2.30\KeePass.exe"
SET kdb="%USERPROFILE%\KeePass\PasswordDatabase\PasswordDatabase.kdbx"
echo I'll start KeePass for You
START "" %keePass% %kdb%
)
:: I've converted these to one-liners simply for personal preference.
:: You can keep these the way you had them if you put them inside of parentheses like with option 1.
if %errorlevel% equ 2 call "%USERPROFILE%\backup.bat"
if %errorlevel% equ 3 start "" "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
if %errorlevel% equ 4 exit /b
:: Calls this script again, simulating a goto :MENU
:: Personally, I'd stick with a label and a goto in this instance,
:: but this is how you could do it if you don't want to use goto at all
call %0
If each choice the user can make is fairly simple (i.e. it can be simplified to one or two commands), you might want to code this way; otherwise, definitely use subroutines like Dennis suggested.
My take on organizing this, added a reset to m variable, allowed some accidental input to be dealt with, and made it all checked in one block of code.
Nothing wrong with 'Dennis van Gils' answer, figured i would show you a different approach.
#echo off
setlocal enableDelayedExpansion
:menu
set "m="
cls
echo/Welcome !username!
echo/
echo/1 - Start keepass
echo/2 - Backup
echo/3 - Firefox
echo/4 - Exit
echo/
set /p "m=Please enter selection, then press enter:"
if not defined m (
cls
echo/Error: Empty input.
pause
) else (
if "!m!" equ "1" (
set "keepass=!userprofile!\keepass\keepass-2.30\keepass.exe"
set "kdb=!userprofile!\keepass\passworddatabase\passworddatabase.kdbx"
echo/I'll start keepass for you
start "" !keepass! !kdb!
) else (
if "!m!" equ "2" (
set "backup=!userprofile!\backup.bat"
call !backup!
) else (
if "!m!" equ "3" (
cd "c:\program files (x86)\mozilla firefox\"
start firefox.exe
) else (
if "!m!" equ "4" (
goto :eof
) else (
cls
echo/Error: ["!m!"] not recognized.
pause
)
)
)
)
)
goto :menu
Note: echo/ is used as a habit, as echo: and echo\ i mistake for parts of a file path/url, and echo. is so painstakingly noted for its longer command time.
Also, i prefer using ! over % along with setlocal enableDelayedExpansion by pure preference, and ease of block coding.
How do I create a batch file timer to execute / call another batch through out the day Maybe on given times to run but not to run on weekends ? Must run on system times can also be .cmd to run on xp server 2003
For the timer part of your script i highly reccomend using:
echo.
echo Waiting For One Hour...
TIMEOUT /T 3600 /NOBREAK
echo.
echo (Put some Other Processes Here)
echo.
pause >nul
This script waits for 1 hour (3600 seconds) and then continues on with the script and the user cannot press any buttons to bypass the timer (besides CTRL+C).
You can use
Timeout /t 3600 /nobreak >nul
If you don't want to see a countdown on the screen.
I would use the scheduler (control panel) rather than a cmd line or other application.
Control Panel -> Scheduled tasks
Below is a batch file that will wait for 1 minute, check the day, and then perform an action. It uses PING.EXE, but requires no files that aren't included with Windows.
#ECHO OFF
:LOOP
ECHO Waiting for 1 minute...
PING -n 60 127.0.0.1>nul
IF %DATE:~0,3%==Mon CALL SomeOtherFile.cmd
IF %DATE:~0,3%==Tue CALL SomeOtherFile.cmd
IF %DATE:~0,3%==Wed CALL SomeOtherFile.cmd
IF %DATE:~0,3%==Thu CALL WootSomeOtherFile.cmd
IF %DATE:~0,3%==Fri CALL SomeOtherFile.cmd
IF %DATE:~0,3%==Sat ECHO Saturday...nothing to do.
IF %DATE:~0,3%==Sun ECHO Sunday...nothing to do.
GOTO LOOP
It could be improved upon in many ways, but it might get you started.
The AT command would do that but that's what the Scheduled Tasks gui is for. Enter "help at" in a cmd window for details.
I did it by writing a little C# app that just wakes up to launch periodic tasks -- don't know if it is doable from a batch file without downloading extensions to support a sleep command. (For my purposes the Windows scheduler didn't work because the apps launched had no graphics context available.)
#echo off
:Start
title timer
color EC
echo Type in an amount of time (Seconds)
set /p time=
color CE
:loop
cls
ping localhost -n 2 >nul
set /a time=%time%-1
echo %time%
if %time% EQU 0 goto Timesup
goto loop
:Timesup
title Time Is Up!
ping localhost -n 2 >nul
ping localhost -n 2 >nul
cls
echo The Time is up!
pause
cls
echo Thank you for using this software.
pause
goto Web
goto Exit
:Web
rem type ur command here
:Exit
Exit
goto Exit
#echo off
:Start # seting a ponter
title timer #name the cmd window to "Timer"
echo Type in an amount of time (Seconds)
set /p A= #wating for input from user
set B=1
cls
:loop
ping localhost -n 2 >nul #pinging your self for 1 second
set /A A=A-B #sets the value A - 1
echo %A% # printing A
if %A% EQU 0 goto Timesup #if A = 0 go to ponter Timesup eles loop it
goto loop
:Timesup #ponter Timesup
cls #clear the screen
MSG * /v "time Is Up!" #makes a pop up saying "time Is Up!"
goto Exit #go to exit
:Exit
better code that doesn't involve ping:
SET COUNTER=0
:loop
SET /a COUNTER=%COUNTER%+1
XCOPY "Server\*" "c:\minecraft\backups\server_backup_%COUNTER%" /i /s
timeout /t 600 /nobreak >nul
goto loop
600 seconds is 10 minutes, however you can set it whatever time you'd like
You could also do this>
#echo off
:loop
set a=60
set /a a-1
if a GTR 1 (
echo %a% minutes remaining...
timeout /t 60 /nobreak >nul
goto a
) else if a LSS 1 goto finished
:finished
::code
::code
::code
pause>nul
Or something like that.