loading

Useful Batch Commands

Hey guys, I wanted to post a quick forum topic about useful batch commands that are seldom used or not well-known, or maybe just useful tidbits. Note: These do not have to be used, they are just extremely helpful and are just "extra," meaning you could probably find easier-to-understand but not as useful commands.

1. The CHOICE command
The reason this command is so cool is that unlike other Y/N menus, when you enter the desired number, you don't have to hit enter, it goes straight to the next screen. You can also program it to execute a command after a set amount of time. Here's how to write it out:

choice /c:12345 /t:5,10
if errorlevel 5 goto ha
if errorlevel 4 goto joke
if errorlevel 3 goto tired
if errorlevel 2 goto fried
if errorlevel 1 goto chiken

To time it, you type /t and then a colon. You write the choice that you want to be timed, e.g. 5, and the amount of time in seconds you want it to wait for seperated by a comma.

2. The Random Variable
The reason I deceided to put this up is because it is one of the only if not the only variable that is already programmed into the system. By using it correctly, you can generate a number between 1 and 32767. WHAT?! Of course, you can narrow it down, to, say about 1 and 100.

:dim
set rnd=%random%
if %rnd% GEQ 101 goto dim
if %rnd% LEQ 0 goto dim
set number=%rnd%

So as you can see, %random% is a set variable and generates a random number for you. Please keep in mind that this can take one second, or one eternity (literally) since it doesn't narrow down the choices after finding that a number doesn;t work. So, you may keep on generating 101 forever and never get to 100!

3. The ^| Command.
Ever get frustrated because you are making an animation with, for example, stick figures and are getting driven nuts because the minute it gets to a certain part, it exits on you? The reason is probably because you used the | symbol, which is actually knd of like a command by itself. So, if you're doing something like this:

echo [0]
echo /|
echo /

The reason it is not working is because you didin't use a ^ before the |. Here's what it should be:

echo [0]
echo /^|
echo /

Yes, it looks wierd in the editing stages, but looks much better in the real thing. Just remember, you will have to remember that the ^ will be invisible, so make sure everything else in front of it is like this:

echo [0]   [
echo /^|    [
echo /     [

So that the [ or whatever it is is one space ahead from the others.

4. The %TIME:~0,5% Command.
What time is it? Oh, it's 3:16:52.29! Ever been frustrated with the TIME command? For one, who ( and when I mean who, I mean the average batcher who doesn't use the TIME command to synchronize seven different computers around the house which I could never hope to do) needs to be that exact? When making a batch alarm clock, for example, the user wouldn't really want to do all of that, would they? So, for those of you who would want to make such a clock, here's an example:

@echo off
title Alarm Clock
:start
cls
color f0
echo.
echo Use 24 hour clock
echo.
set /p a=Set Alarm:
cls
echo.
echo Alarm set to %a%.
:alarm
if %time:~0,5% EQU %a% goto alarmset
goto alarm
:alarmset
color fc
echo
echo
echo
cls
echo.
echo It's %time:~0,5%!
echo.
echo Press 1 to acknowledge.
choice /n /c:12 /t:2,1
if errorlevel 2 goto alarmset
if errorlevel 1 goto sleep
goto alarmset
:sleep
goto start

So there's one example. %TIME:~0,5% sets the time from 3:15:00.00 to just 15:15. This doesn't work just with the TIME command, you can do this with any variable. If I had a variable %x% and it said "crayon" and wanted it to say "cray," I would type in %x:~0,-2%. If I wanted to make %x% say "on," I would type in %x:~-4,0%.

5. The AT Command

The AT command is exactly what it appears to be. You tell it AT (time) /every:[day(s) of the week] (command to be executed). So, it I wanted an alarm to go off whenever the Simpsons are on, I'd type

AT 19:30 /every:sumtwthfs echo ALARM!!!

Now, I know that the Simpsons are on at a different time on Sundays, but that's not important. Please not that you HAVE to specify what day you want it to go off on (it can be dd/mm/yyyy to be really specific). Default is set to "tomorrow."

This was probably too confusing. For one, I talk too much. Thank you for even getting this far, anyways. Thanks a bunch!


Laserman5957 years ago
you never specified they had to be used in the text you said

Hey guys, I wanted to post a quick forum topic about useful batch commands that are seldom used or not well-known, or maybe just useful tidbits.
 Emphasis 
on that last part.   
lemonie7 years ago
www.dostips.com
Lot o'stuff there.

L
Laserman5957 years ago
type help in comand prompt and there is al list of all the batch commands avalible to you.  But some of them only work in the command prompt its self.
TheBatchies (author)  Laserman5957 years ago
Uhh... Thanks, but these are commands or other useful tips that aren't found there. Thanks anyways though! The help is very much appreciated.