How to Use the Microsoft Batch File Language

Open your text editor., Start writing the file., Hit enter., Welcome the user to the program., Hit enter again?, Leave a blank line for neat spacing then continue typing yet another line.,Press Enter one more time., Program what options you want the...

13 Steps 3 min read Advanced

Step-by-Step Guide

  1. Step 1: Open your text editor.

    Expect to use keys A-Z/0-9/, the symbols (!$| etc), and Enter.

    Most commands do not check entry for upper/lowercase, so for the moment don't worry about CAPS (or cApS).

    Each command (and its parameters) goes on one line.

    Open a command line window (cmd.exe) to test the commands you want to run.

    Arrange your windows so you can see them both.
  2. Step 2: Start writing the file.

    To start writing the file, most people start with @echo off as it stops each command being printed as it is processed.

    It reduces clutter on the user's screen.(This section does some hand-holding.

    If you want the finished script, jump to the next section.) : @echo OFF , Remember to press enter after each command., Type: : echo Welcome TO the Backup Script! , Good., : echo. ,, This code below gives the user a choice.

    Either they press F, or N, or they press Q or CTRL-Z which cancels the whole script. : choice /C:
    FNQ /N SELECT ull Backup OR ew FILES only.

    Press OR TO EXIT. , If the user presses Q the program returns a "3"

    and goes to section "end".

    If they press N the program returns a "2"

    and goes to section "small_backup".

    If they press F, the program returns a "1"

    and goes to "full_backup". "Errorlevel" is not a error message as such, just the only way to set output from the CHOICE command. :
    IF errorlevel 3 GOTO END :
    IF errorlevel 2 GOTO small_backup :
    IF errorlevel 1 GOTO full_backup , Type: : :small_backup<br> : echo. : echo. : echo You chose TO backup NEW FILES.

    Hit ANY KEY TO start OR ctrl-z TO cancel. pause >nul xcopy c:\mydirectory d:\mybackup /s/m/e GOTO END : :full_backup<br> : echo. : echo. : echo You chose TO backup ALL FILES.

    Hit ANY KEY TO start OR ctrl-z TO cancel. pause >nul xcopy c:\mydirectory d:\mybackup /s/e goto end  :end exit </source> Create the directories referred to above, and copy a few small test files into the source directory ready for testing.

    Later you can change those directory names to suit your real <my documents="">. ,,, @echo OFF echo Welcome TO the Backup Script! echo. choice /C:
    FN /N SELECT ull Backup OR ew FILES Backup, OR ctrl-z TO EXIT.

    IF errorlevel 3 GOTO END IF errorlevel 2 GOTO small_backup IF errorlevel 1 GOTO full_backup :small_backup echo. echo. echo You chose TO backup NEW FILES.

    Hit ANY KEY TO start OR ctrl-z TO EXIT. pause >nul xcopy c:\mydirectory d:\mybackup /s/m/e GOTO END :full_backup echo. echo. echo You chose TO backup ALL FILES.

    Hit ANY KEY TO start OR ctrl-z TO EXIT. pause >nul xcopy c:\mydirectory d:\mybackup /s/e GOTO END :
    END EXIT
  3. Step 3: Hit enter.

  4. Step 4: Welcome the user to the program.

  5. Step 5: Hit enter again?

  6. Step 6: Leave a blank line for neat spacing then continue typing yet another line.

  7. Step 7: Press Enter one more time.

  8. Step 8: Program what options you want the person who runs your program to see.

  9. Step 9: Create commands for each choice.

  10. Step 10: Create those sections referred to above.

  11. Step 11: Save the file in Notepad as "mybackup.bat".

  12. Step 12: Double-click the file to run it.

  13. Step 13: Practice your copy & pasting skills on the following text.

Detailed Guide

Expect to use keys A-Z/0-9/, the symbols (!$| etc), and Enter.

Most commands do not check entry for upper/lowercase, so for the moment don't worry about CAPS (or cApS).

Each command (and its parameters) goes on one line.

Open a command line window (cmd.exe) to test the commands you want to run.

Arrange your windows so you can see them both.

To start writing the file, most people start with @echo off as it stops each command being printed as it is processed.

It reduces clutter on the user's screen.(This section does some hand-holding.

If you want the finished script, jump to the next section.) : @echo OFF , Remember to press enter after each command., Type: : echo Welcome TO the Backup Script! , Good., : echo. ,, This code below gives the user a choice.

Either they press F, or N, or they press Q or CTRL-Z which cancels the whole script. : choice /C:
FNQ /N SELECT ull Backup OR ew FILES only.

Press OR TO EXIT. , If the user presses Q the program returns a "3"

and goes to section "end".

If they press N the program returns a "2"

and goes to section "small_backup".

If they press F, the program returns a "1"

and goes to "full_backup". "Errorlevel" is not a error message as such, just the only way to set output from the CHOICE command. :
IF errorlevel 3 GOTO END :
IF errorlevel 2 GOTO small_backup :
IF errorlevel 1 GOTO full_backup , Type: : :small_backup<br> : echo. : echo. : echo You chose TO backup NEW FILES.

Hit ANY KEY TO start OR ctrl-z TO cancel. pause >nul xcopy c:\mydirectory d:\mybackup /s/m/e GOTO END : :full_backup<br> : echo. : echo. : echo You chose TO backup ALL FILES.

Hit ANY KEY TO start OR ctrl-z TO cancel. pause >nul xcopy c:\mydirectory d:\mybackup /s/e goto end  :end exit </source> Create the directories referred to above, and copy a few small test files into the source directory ready for testing.

Later you can change those directory names to suit your real <my documents="">. ,,, @echo OFF echo Welcome TO the Backup Script! echo. choice /C:
FN /N SELECT ull Backup OR ew FILES Backup, OR ctrl-z TO EXIT.

IF errorlevel 3 GOTO END IF errorlevel 2 GOTO small_backup IF errorlevel 1 GOTO full_backup :small_backup echo. echo. echo You chose TO backup NEW FILES.

Hit ANY KEY TO start OR ctrl-z TO EXIT. pause >nul xcopy c:\mydirectory d:\mybackup /s/m/e GOTO END :full_backup echo. echo. echo You chose TO backup ALL FILES.

Hit ANY KEY TO start OR ctrl-z TO EXIT. pause >nul xcopy c:\mydirectory d:\mybackup /s/e GOTO END :
END EXIT

About the Author

J

Janet Gray

Enthusiastic about teaching DIY projects techniques through clear, step-by-step guides.

149 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: