To write a Windows batch script, follow these steps:
1. Create a New Text File:
Open Notepad or any text editor.
Save the file with a .bat extension, for example: script.bat.
2. Write Basic Commands:
Start by writing basic commands you want to automate. For example, to print “Hello, World!” on the screen:
@echo off
echo Hello, World!
pause
@echo off prevents the commands from being displayed while executing.
echo displays text.
pause keeps the window open so you can see the output.
3. Run the Script:
Double-click the .bat file to run it.
4. Use Conditional Statements:
You can also use if, for, goto, etc., for more complex logic. Example:
@echo off
if exist “C:\example\file.txt” (
echo File exists.
) else (
echo File does not exist.
)
Batch scripts are great for automating repetitive tasks on Windows.