Maintainers
Pricing
Enterprise
More
More
Documentation
Try Live
Rankings
Install
Blog
Docs7
Ask
New
Add Docs
Batch Script Guide
https://github.com/subhodip1307/batch-script-guide
Admin
It's a Simple Guide for Batch Script or Shell Scripting
Tokens:
1,504
Snippets:
18
Trust Score:
5.5
License:
MIT
Update:
1 year ago
Context
Chat
Benchmark
11.38
Latest
Show doc for...
Code
Info
Show Results
Tokens:
Raw
Copy
Link
Tokens:
Raw
Copy
Link
### Example: Opening Notepad for 'myfile.bat' - Bash Source: https://github.com/subhodip1307/batch-script-guide/blob/main/Readme.md This is a concrete example of opening Notepad to create or edit a batch file named 'myfile.bat'. It demonstrates the correct syntax for the 'notepad' command followed by the specific file name and the essential '.bat' extension. ```Bash notepad myfile.bat ``` -------------------------------- ### Printing 'Hello world' in Batch Script Source: https://github.com/subhodip1307/batch-script-guide/blob/main/Readme.md This batch script command uses 'Echo' to print the string 'Hello world' to the command prompt. It's a fundamental command for displaying output and is often used as a first example in scripting to confirm basic functionality. ```Batch Script Echo Hello world ``` -------------------------------- ### Example String Variable Declaration (Batch) Source: https://github.com/subhodip1307/batch-script-guide/blob/main/Readme.md This example provides concrete instances of declaring string variables, 'name' and 'HOME', and assigning them string values. It also includes a comment reinforcing that variable letter case does not matter, though uppercase is preferred. ```batch Set name= Subho Set HOME=India Ren in case of variable letter case doesn’t matters but preferred to use Upper case ``` -------------------------------- ### Prompting for User Input and Displaying Values (Batch Script) Source: https://github.com/subhodip1307/batch-script-guide/blob/main/Readme.md This Batch Script example demonstrates how to prompt the user for their name and age using 'set /p', store the inputs in 'name' and 'age' variables, and then display these captured values back to the user using the 'echo' command. It highlights the process of interactive input and output. ```bash set /p name= What is your name set /p age= what is your age Ren in case of variable letter case doesn’t matters but preferred to use Upper case echo Your name is %name% echo your age is %age% ``` -------------------------------- ### Adding Comments with Rem (Batch) Source: https://github.com/subhodip1307/batch-script-guide/blob/main/Readme.md This example demonstrates the use of `rem` (or `Rem`) to add comments within a batch script. Comments are ignored during script execution and are used to provide explanations or notes for human readers, improving code readability. ```batch echo Hello world rem Hi, my name is Subho pause ``` -------------------------------- ### Printing Integer Variable Values (Batch) Source: https://github.com/subhodip1307/batch-script-guide/blob/main/Readme.md This example demonstrates how to print the value of an integer variable. Similar to string variables, integer variable values are accessed by enclosing the variable name within percent signs (e.g., `%AGE%`). ```batch echo your age is %AGE% ``` -------------------------------- ### Pausing Script Execution with Pause (Batch) Source: https://github.com/subhodip1307/batch-script-guide/blob/main/Readme.md This example shows how to use the `pause` command to halt script execution until the user presses any key. It is commonly placed at the end of scripts to keep the console window open, allowing users to view output before the script closes. ```batch echo Hello world pause ``` -------------------------------- ### Syntax for Taking User Input (Batch Script) Source: https://github.com/subhodip1307/batch-script-guide/blob/main/Readme.md This snippet provides the general syntax for taking user input in Batch Script using the 'Set /p' command. It shows how to define a variable name and a placeholder prompt that will be displayed to the user. ```bash Set /p <variable name> = <placeholder> Set /p <variable name> = <placeholder> ``` -------------------------------- ### Setting Static Variables (Batch Script) Source: https://github.com/subhodip1307/batch-script-guide/blob/main/Readme.md This example shows how to declare and assign static string values to variables in Batch Script using the 'Set' command. It illustrates basic variable assignment, noting that variable names are case-insensitive but uppercase is preferred. ```bash Set name= Subho Set HOME=India Ren in case of variable letter case doesn’t matters but preferred to use Upper case ``` -------------------------------- ### Printing Output with Echo (Batch) Source: https://github.com/subhodip1307/batch-script-guide/blob/main/Readme.md This snippet demonstrates the basic usage of the `echo` command in Batch Script to print a specified string to the terminal. It's a fundamental command for displaying messages or variable values. ```batch echo Hello world ``` -------------------------------- ### Displaying Predefined and Environment Variables (Batch Script) Source: https://github.com/subhodip1307/batch-script-guide/blob/main/Readme.md This snippet demonstrates how to display the values of several predefined and environment variables in Batch Script, such as %RANDOM%, %USERNAME%, and %WINDIR%. It uses the 'Echo' command to print their current values to the console. ```bash Echo %RANDOM% Echo %USERNAME% Echo %WINDIR% Ren there is more variables like this, I will al explain in next update Ren please wait for upcoming update pause ``` -------------------------------- ### Opening Notepad to Create a Batch File - Bash Source: https://github.com/subhodip1307/batch-script-guide/blob/main/Readme.md This command opens Notepad to create a new batch file. Users should replace '<give a file name>' with their desired file name, ensuring the '.bat' extension is included for proper recognition as a batch script. This is the initial step for writing batch commands. ```Bash notepad <give a file name>.bat ``` -------------------------------- ### Setting Console Window Title with Title (Batch) Source: https://github.com/subhodip1307/batch-script-guide/blob/main/Readme.md The `Title` command allows you to set a custom title for the command prompt window where the batch script is running. This can be useful for identifying different script windows or providing context to the user. ```batch @echo off Title Subho1307 echo Hello world pause rem using pause to see the changes ``` -------------------------------- ### Suppressing Command Output with @echo off (Batch) Source: https://github.com/subhodip1307/batch-script-guide/blob/main/Readme.md The `@echo off` command, typically placed at the beginning of a batch script, prevents commands themselves from being displayed in the console, resulting in a cleaner output. It only shows the output generated by commands like `echo`. ```batch @echo off echo Hello world echo don’t forget to give a start ``` -------------------------------- ### Printing String Variable Values (Batch) Source: https://github.com/subhodip1307/batch-script-guide/blob/main/Readme.md This snippet demonstrates how to retrieve and print the values of string variables. To access a variable's value, enclose its name within percent signs (e.g., `%name%`). The `pause` command is included to keep the output visible. ```batch Set name= Subho Set HOME=India Echo my name is %name% Echo my home is %HOME% Echo %HOME% pause ``` -------------------------------- ### Timed Pause with Ping (Batch) Source: https://github.com/subhodip1307/batch-script-guide/blob/main/Readme.md This snippet illustrates an alternative method for pausing script execution for a specified duration using the `ping` command. The `-n 3` argument makes the script wait for 3 seconds. Note that this method may require running the script as an administrator. ```batch echo Hello world ping 127.0.0.1 -n 3 > nul ``` -------------------------------- ### Declaring Integer Variables (Batch) Source: https://github.com/subhodip1307/batch-script-guide/blob/main/Readme.md This snippet shows the syntax for declaring integer (numeric) variables in Batch Script. The `/a` switch after `Set` is crucial for performing arithmetic operations or storing numeric values. Replace `<variable name>` and `<number>` with your desired values. ```batch Set /a <variable name> = <number> Ren change the ‘<variable name>’ with any name and change the ‘<number>’ with any number Ren as example Set /a AGE= 20 Ren in case of variable letter case doesn’t matters but preferred to use Upper case ``` -------------------------------- ### Declaring String Variables (Batch) Source: https://github.com/subhodip1307/batch-script-guide/blob/main/Readme.md This snippet illustrates the syntax for declaring string variables in Batch Script using the `Set` command. Replace `<variables name>` with your desired variable name and `<put the value>` with the string you wish to store. Variable names are case-insensitive, but uppercase is preferred. ```batch Set <variables name> = <put the value> ```