I am trying to get into windows batch files and programming in general, I messed around with an example I found online and made a batch file that creates another batch file (for the sake of learning)
But I now want to enable typing into a command prompt, I have a crude solution to this where I make a batch file, then create another batch file and start that file using the orignal .bat (don't worry, code is below, will make sense.)
Basically if you type 1, I want to do something in the batch, I know that I need to first do an if, but it's trying to get the text entered in the command line that I'm struggling with.... Any help would be great!
#echo off
if exist CommandTest.bat (
echo File already exists...
pause
) else (
echo CommandTest.bat does not exist. Creating the file...
timeout /t 3 /NOBREAK>nul
echo Please wait while your file loads...
echo #echo off >CommandTest.bat
echo color 0a >>CommandTest.bat
echo echo Line number 1 >>CommandTest.bat
echo timeout /t 3 /NOBREAK>nul >>CommandTest.bat
echo echo Line number 2 >>CommandTest.bat
echo timeout /t 3 /NOBREAK>nul >>CommandTest.bat
echo echo Line number 3 >>CommandTest.bat
echo timeout /t 3 /NOBREAK>nul >>CommandTest.bat
echo echo Line number 4 >>CommandTest.bat
echo timeout /t 3 /NOBREAK>nul >>CommandTest.bat
timeout /t 2 /NOBREAK>nul
start CommandTest.bat
timeout /t 5 /NOBREAK>nul
)
I am basically trying to redesign it to be user interactive...
According to your comment, choice is the right choice for you:
#echo off
echo 1 - do something
echo 2 - do something else
echo 3 - do nothing
choice /c 123 /m "take your choice "
if %errorlevel% == 1 echo let's do something
if %errorlevel% == 2 goto :other
if %errorlevel% == 3 echo let's do nothing
pause
goto :eof
:other
echo let's do something else
echo something else ...
pause
Related
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.
okay, so I'm trying to make a batch file that just echos the text inside a file called "test.txt" and my code is
#echo off
cls
(
echo %0%
echo %1%
echo %2%
echo %3%
echo %4%
echo %5%
echo %6%
echo %7%
echo %8%
echo %9%
echo %10%
echo %11%
echo %12%
echo %13%
echo %14%
echo %15%
echo %16%
) <test.txt
pause >nul
but for some very strange reason I couldn't find any answers for anywhere, my output is
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
0
1
2
3
4
5
6
and I really don't understand why.
If you want to echo the text that's inside your test.txt file, then you need to add this line to your batch file as already said by #Blorgbeard.
type C:\..\test.txt
#pause
Adding this line to your batch file will display the text/contents of a file in the command prompt window.
Copy the numbers below into test.txt on your desktop.
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
.
Now copy and paste the code below into print.bat on your desktop.
#ECHO OFF
type "%UserProfile%\Desktop\test.txt"
#pause
now run print.bat
REM:: Big thanks to Rakitić, Blorgbeard & Hackoo.
With this batch file you can echo your test.txt file :
#echo off
Set "MyFile=test.txt"
for /f "delims=" %%a in ('Type "%MyFile%"') Do (
echo %%a )
pause
I've been using batch for a while now and I just recently ran into a problem I never encountered before involving ERRORLEVELS.
Here is a short program I made to show off the error.
#echo off
title Choices
CMD /C EXIT 0
echo [1] Choice 1
echo [2] Choice 2
echo [3] Choice 3
choice /c 123 /n
IF ERRORLEVEL 1 GOTO ONE
IF ERRORLEVEL 2 GOTO TWO
IF ERRORLEVEL 3 GOTO THREE
echo Nice you broke it
pause
exit
:ONE
echo CONGRATS YOU CHOSE 1
pause
exit
:TWO
echo NICE YOU CHOSE 2
pause
exit
:THREE
echo OOH YOU CHOSE 3
pause
exit
Its very simple and all you do is press a number and it says what number you pressed. The problem is no matter what i press it always outputs what would happen when I press 1. I used to use %errorlevel% and that worked fine but then it stopped working so I switched to the new method (IF ERRORLEVEL WHATEVER) and now it wont work either.
Please read the Microsoft support article Testing for a Specific Error Level in Batch Files.
And open a command prompt window, run if /? and read the output help, especially the paragraph about errorlevel.
The solution for your batch file is very simple, reverse the order of the lines testing on errorlevel:
#echo off
title Choices
CMD /C EXIT 0
echo [1] Choice 1
echo [2] Choice 2
echo [3] Choice 3
choice /c 123 /n
IF ERRORLEVEL 3 GOTO THREE
IF ERRORLEVEL 2 GOTO TWO
IF ERRORLEVEL 1 GOTO ONE
echo Nice you broke it
pause
exit
:ONE
echo CONGRATS YOU CHOSE 1
pause
exit
:TWO
echo NICE YOU CHOSE 2
pause
exit
:THREE
echo OOH YOU CHOSE 3
pause
exit
I would like to add a time limit to a batch file to enter an input for example:
#echo off
echo.
echo.
set /p m=press 1:
if %m%==1 (
goto start
)
:start
echo hello
pause >nul
:fail
you have failed!
pause >nul
Could you put any code near the set /p m=press 1: so that if no input is entered within 10 seconds then it would go to fail?
thank you :)
With the CHOICE command you can do that :
CHOICE /T 5 /C yn /D y
This will automatically answer yafter 5 secondes without user input
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.