User:FunCode/sandbox

CMD is a program that executes commands whenever you want. This simple looking software may look harmless but can be used for hacking and creating viruses.

CMD has a kind of programming language to go on with it called batch, which is saved as a [.bat] file. You can use the same codes in batch but it runs as one executable file while CMD keeps running for more and more executable files.

CMD is very powerful but very basic, and I am here to teach you how to use this little language.

Now, lets start off with the basics of programming in batch. Every batch file has to start off with a kind of format type thing, whether it is on, or off. Most people prefer it to be off because it makes the program look a lot better.

@echo off

Now of course when you are typing, it won't be bold, that was just representing it was off, or on. Now lets make the program say Hello

@echo off echo Hello

As you can see, echo means to type something, now if you were to save this and run it, it would say hello... For only about a millisecond To fix this problem, you would type pause

@echo off echo Hello pause

This is how it would run.

Hello Press any key to continue...

It would say 'Press any key to continue', because of the pause. To get rid of that, add >nul to the end of the pause

@echo off echo Hello pause>nul

So this time, it would run

Hello

Then you would press any key then it would exit.

When this runs, you probably want it to look more professional, so that's where title and color comes in. To give the file a title, you simply type 'title TitleNameHere'

@echo off title This is a title

And now to the color part, the color thing can be confusing, but if you type color, then more than 2 numbers, you would get a list (my little cheat way). The color codes are arranged by the first num/char ( backgroundcolor ) and the second num/char ( foregroundcolor). This is just used for a little style. The default color arrangement is 09, which means black background, and white foreground.

Here is a neat little 'first' file

@echo off color 0a title Hello echo Hello pause>nul This is how that file would have worked, 1st, the color would have changed to a black background, and a green foreground, then the title would set to Hello, then it would say Hello, and finally it would make a pause that is invisible because of the >nul.

Now, lets set a location-type thing to go back to if you need to. To do this, you make a label, that MUST start with a colon, exmple :a Caps does matter so make sure you use the correct punctuation. Here is a way to use the label

@echo off :a echo ME

Now that you have a label, you need to know how to use it. To say, make it go back to the label, you would type 'goto labelName', here is an example

@echo off :a echo Label Test goto a

This would make tons and tons of words saying 'Label Test', to fix this, add a cls under the label. This would give it a weird flashy look

@echo off :a cls echo Label Test goto a

What cls does, is clears the screen. If you add a pause, then it won't be as constant. In the previous example, if I wanted to add a pause, I would add it above the 'goto a', so it still can echo Label test, clear the screen, and go to label a.

@echo off :a cls echo Label Test pause goto a

How would this work? Well first, it would turn the @echo, off. Then it would set the label a, go down and clear the screen. Then it would print 'Label Test', it would bring up pause, then it would go back to a to repeat the process again. This is pretty useless, but we'll find a use for it with the set command.

To use the set command, you would type 'set', but there are two ways it can be used. It can be used as an expression, or a string. We'll be using a string for this. An expression is /a, and a string is /p. So to make a string input, you would type 'set /p'. But we will need to name the string to make a variable, we would put that after /p, then add an equal sign.

@echo off set /p stringName=

This would make an input you can use, but maybe you want to add and extra thing before what you type, like say you were typing a username, it could say 'Username: ', then you can type. To do this, we would write what we want it to print before the input, after the equal. Its kindof confusing what I just said, but you can get a better understanding of it in this example.

@echo off set /p username=Username:

This would just do this

Username:

Then after the colon, you would type something, then the program would end.

Now we can use IF statements. Anytime you are specifying a variable outside of the set /p, you would add %'s after and before it. Example

@echo off set /p test= echo %test% pause

What this would do is setup an input, then write what I said. if statements are kindof weird sometimes, and takes a while to type, but is totally worth it if you want something like a login. To use an if statement, you will have to have the variable, in this case, a string.

Note: With IF statements, you must use 2 equal signs.

@echo off set /p username=Username: cls if %username%== username goto a    :a echo yay pause

Either way, it would print yay, but you get the point.