Skip to content

Latest commit

 

History

History
executable file
·
149 lines (95 loc) · 4.81 KB

File metadata and controls

executable file
·
149 lines (95 loc) · 4.81 KB
  1. Creating a .bat File

    Creating a batch script is as simple as writing a series of commands into a plain text file and saving it with the .bat or .cmd extension.

    📝 Steps to Create:

    1. Open a Text Editor:

      • Use Notepad (built-in to Windows) or any code editor (like VS Code, Notepad++, etc.).
    2. Write Commands:

      @echo off
      echo Welcome to Batch Scripting!
      pause
    3. Save the File:

      • File → Save As...

      • Choose “All Files” in the Save As type.

      • Name it like example.bat (not example.bat.txt).

      • Choose ANSI or UTF-8 without BOM as encoding (optional but safer for special characters).

    4. ✅ Your script is ready to execute!


  2. Running a Batch Script

    There are multiple ways to run a .bat file:

    1. Double-click from File Explorer:

      • Just double-click the .bat file, and it will open in a new Command Prompt window.

      • It executes line-by-line and then closes (unless you use pause).

    2. Via Command Prompt:

      • Open CMD (Win + R → type cmd → Enter).

      • Navigate to the script location using cd command.

      • Run the script by typing:

        example.bat
    3. From Run Dialog:

      • Press Win + R, then type:

        C:\Path\To\Your\Script.bat
      • Useful for launching tools quickly.

    4. From Task Scheduler:

      • Useful for automation.

      • Create a scheduled task and point it to your batch file.

      ⚠️ Notes:

      • If the batch file doesn’t run or flashes and closes instantly, add a pause at the end to diagnose the output.

      • Use echo statements for debugging inside the script.


  3. Basic Syntax and Rules

    Batch scripting follows a line-by-line execution model. It doesn't require indentation or brackets like high-level languages, but there are some basic rules to keep in mind.

    General Syntax Rules:

    Rule Description
    One command per line Each line is a command executed in order
    Case-insensitive ECHO, echo, or EcHo are treated the same
    Comments Use REM or :: to comment
    Variables Use %VAR_NAME% syntax for variables
    Execution flow Use IF, GOTO, and FOR for logic and loops
    Escape characters Use ^ to escape special characters like &, ` , >`

    🧾 Common Commands:

    @echo off           :: Suppresses command echoing
    echo Hello World    :: Prints text
    pause               :: Waits for a key press
    cls                 :: Clears the screen
    set VAR=value       :: Creates a variable
    echo %VAR%          :: Prints variable
    goto label          :: Jumps to a label
    if EXIST file.txt echo Found file!   :: Condition check

    🔍 Commenting in Batch:

    REM This is a comment
    :: This is also a comment (but safer to use REM in loops)

    🚫 Special Characters to Be Aware Of:

    Batch scripts have characters that must be escaped or used with care:

    Character Meaning
    > Output redirection
    < Input redirection
    ` ` Pipe
    & Command chaining
    ^ Escape character

    To print these characters as text, escape them:

    echo ^> This is not redirection

    💡 Best Practices:

    • Always use @echo off at the top to suppress command repetition.

    • Use pause for debugging to see what’s happening.

    • Always quote file paths with spaces:

      "C:\Program Files\App\run.exe"
    • Add comments to make scripts readable.

    • Use lowercase for readability, though it's not required.