Complete Guide to Batch Scripts in Windows: Examples and Practical Use

Last update: 21/03/2025
Author Isaac
  • Learn what batch scripts are and how they can automate tasks in Windows.
  • Explore the structure and syntax of batch files with commands key code.
  • Discover practical examples for managing files, running programs, and automating processes.
  • Enhance your scripts with variables, conditional structures, and loops.

batch .bat

Batch scripts are a powerful tool for automating tasks in Windows by executing commands in a text file. With them, you can streamline repetitive processes and improve system administration efficiency.

In this detailed guide, you'll learn what batch scripts are, how they work, and how you can use them for a variety of tasks, from file manipulation to system configuration management.

What is a batch script?

Un script batch It is a text file with the extension .bat or .cmd that contains a sequence of commands that are executed automatically in the Windows console (DCM). These scripts allow you to run tasks in batches, making it easier to automation of repetitive processes.

Batch Script Basics

Structure of a batch script

A batch script consists of a sequence of instructions that are executed in the order in which they appear. Each line represents an executable command in the Windows console.

Example:

@echo off
 echo Hola, este es un script batch.
 pause

@ Echo off: Disables the display of commands on the screen.
threw out: Displays a message on the screen.
pause: Stops execution until the user presses a key.

Variables in batch scripts

The variables allow you to store and reuse values ​​in a batch script. They are defined with the command SET and are used with the symbol %.

Example:

@echo off
 SET nombre=Juan
 echo Hola %nombre%
 pause

Using parameters

Batch scripts can accept parameters to make them more dynamic. They are used %1, %2, etc., to refer to past values.

Example:

@echo off
 echo Hola %1
 pause

If you run the script as script.bat Pedro, will print: Hello Pedro.

  How to equalize Windows 10 sound? - Audio settings

Essential commands in batch scripts

batch, batch script

Manipulating files and directories

  • DIR: Displays the contents of a directory.
    DIR C:\Users
  • COPY: Copy files from one place to another.
    COPY archivo.txt D:\Backup
  • TIME: Deletes specific files.
    DEL archivo.txt
  • MKDIR: Create a new directory.
    MKDIR C:\NuevoDirectorio

Program execution

To run a program from a batch script, you can use START o CALL.

START notepad.exe

This will open Notepad.

Control structures

Conditional IF

The command IF allows instructions to be executed based on a condition.

@echo off
 SET /P edad="Ingrese su edad: "
 IF %edad% GEQ 18 (
 echo Eres mayor de edad.
 ) ELSE (
 echo Eres menor de edad.
 )
 pause

Loops with FOR

The command FOR allows you to iterate over a collection of elements.

@echo off
 FOR %%i IN (1 2 3 4 5) DO echo Número: %%i
 pause

Goto and labels

The command GOTO allows you to jump to a specific section of the script.

@echo off
 echo Inicio del script
 GOTO SALTO
 echo Esta línea no se ejecutará.
 : SALTO
 echo Has saltado a esta línea.
 pause

Practical examples of batch scripts

Create a backup script

This script automatically copies files to a backup directory.

@echo off
 SET origen=C:\Documentos
 SET destino=D:\Respaldo
 XCOPY %origen% %destino% /E /H /C /I
 echo Copia de seguridad completada.
 pause

Delete temporary files

This script cleans the Temporary files of the System.

@echo off
 DEL /S /Q C:\Windows\Temp\*.*
 DEL /S /Q %USERPROFILE%\AppData\Local\Temp\*.*
 echo Archivos temporales eliminados.
 pause

Turn off the equipment with a countdown

This script shuts down the computer after a specified time.

@echo off
 SET /P tiempo="Ingrese el tiempo en segundos para apagar: "
 shutdown -s -t %tiempo%
 echo El equipo se apagará en %tiempo% segundos.
 pause

Tips to improve your batch scripts

  • Use comments REM o :: to explain the code.
  • Validates tickets user to avoid errors.
  • Avoid overwriting important files without confirmation.
  • Test the script in a safe environment before running it.
  Battery overheating while charging in Windows 11: Causes, prevention, and solutions

With this guide, you can start creating and improving batch scripts in Windows for automate tasks y streamline processes. Experiment with the examples and adapt them to your needs.

Ctg Files
Related article:
Ctg Files: What They Are, How to Use Them, Programs to Open Them

Leave a comment