Sunday November 16 2025

Bat scripting in windows: Practical guides & examples for beginners

BAT Scripting in Windows for Beginners
Learn how to create and run BAT files in Windows with simple steps and examples.

🚀 Automation in Windows with .BAT files

Simple examples and practical scenarios for beginners to automate everyday tasks

💡 What you will learn in this article

Discover how you can save time and simplify your daily computer work by creating simple files that automatically perform repetitive tasks. Without having to be a computer expert!

You'll learn step-by-step how to create and run batch files, automate processes like backups, file management, and run multiple commands with a single click. Plus, you'll see practical examples that you can immediately adapt to your needs.

By the end of this article, you will have the skills to create small tools that make your work more efficient, reducing errors and freeing up time for more important tasks.

🤔 What are BAT files? 

Imagine that every day you do the same tasks on your computer: opening specific programs, deleting old files, copying folders for safety. It would be great if all of this was done automatically with one click, right?

The files BAT (Batch files) is exactly that: small programs that automatically execute a series of commands in Windows. It's like writing a list of instructions and the computer follows them one by one.
✨ Why they are useful:
  • Save valuable time from repetitive tasks
  • They reduce the possibility of error (the computer doesn't forget steps!)
  • No special programming knowledge required
  • They are free and built into Windows

📝 How do I create a BAT file?

Creating a BAT file is very simple. Follow these steps:

1 Opening Notepad

Press the button Start Windows and type "Notepad". Open the program.

2 Write your commands

Write the commands you want the computer to execute (we will see examples below).

3 Save as .bat

Go to the menu File → Save As. In the file name write something like to_programma_mou.bat (important that it ends in '.bat'). In "Save as type" select "All Files".

4 Implementation

Double-click the .bat file you created and the commands will be executed automatically!

💡 Tip: Save the BAT files to a special folder on your Desktop for easy access.

🧹 Example 1: Automatic Cleaning of Temporary Files

Over time, Windows accumulates temporary files, logs, caches, and junk data that eat up space and often slow down the system. The program below automatically cleans up many categories of such files with one click.

📄 cleanup.bat
@echo off echo ============================== echo Starting system cleanup... echo ===================================== echo. REM 1) Delete Windows temporary files del /q /f /s %temp%\* echo - Temp folder cleaned (%temp%) REM 2) Delete system-wide temporary files del /q /f /s C:\Windows\Temp\* echo - Windows Temp folder cleaned REM 3) Clear Prefetch cache (Windows startup optimizations) del /q /f /s C:\Windows\Prefetch\* echo - Prefetch cache cleaned REM 4) Clear browser cache (Chrome) if exist "%localappdata%\Google\Chrome\User Data\Default\Cache" ( rd /s /q "%localappdata%\Google\Chrome\User Data\Default\Cache" echo - Chrome cache cleaned ) REM 5) Clear Edge cache if exist "%localappdata%\Microsoft\Edge\User Data\Default\Cache" ( rd /s /q "%localappdata%\Microsoft\Edge\User Data\Default\Cache" echo - Edge cache cleaned ) REM 6) Clear Windows update leftover files del /q /f /s C:\Windows\SoftwareDistribution\Download\* echo - Windows Update leftovers removed REM 7) Empty Recycle Bin rd /s /q %systemdrive%\$Recycle.bin echo - Recycle Bin emptied echo. echo ============================== echo Cleanup completed successfully! echo ============================== pause
🎯 What it cleans:
  • Temp folder: files that create programs and are no longer needed
  • C:\Windows\Temp: old Windows temporary files
  • Prefetch: startup optimization files (regenerated automatically)
  • Browser cache: Chrome & Edge cache taking up many MB/GB
  • Windows Update cache: useless downloader files from old upgrades
  • Recycle Bin: completely empties the Recycle Bin
⚠️ Important:

Do not run this script while Windows updates are being installed. Prefetch and cache files are safe to delete — they will be recreated automatically.

🎯 What does it do:
  • Deletes all temporary files from the Temp folder
  • Emptying the Recycle Bin
  • It informs you about every action it takes.
  • It is waiting for you to press a key to close (pause)
💡 When to use it: Run it once a week to keep your computer fast and clean.

💾 Example 2: Backing Up Your Files

Backing up is very important! The program below automatically copies your important files from a folder of your choice to a secure destination folder.

👉 You will only need to change two lines: the source folder and the destination folder.

📄 antigrafo_asfaleias.bat
@echo off echo ====================================== echo File Backup echo ====================================== echo. REM ============================ REM 1) Set source folder REM Example: REM set source=C:\Users\YourName\Documents REM set source=D:\Work\Projects REM ============================ set source=C:\Users\%username%\Documents REM =========================== REM 2) Set destination folder REM Examples: REM set destination=E:\Backup REM set destination=F:\MySafeCopy REM ==================== set destination=D:\Backup\%date:~-4%-%date:~-7,2%-%date:~-10,2% REM Create folder if it does not exist if not exist "%destination%" mkdir "%destination%" REM Copy files echo Copying files in progress... xcopy "%source%" "%destination%" /E /I /Y /H echo. echo ====================================== echo Backup completed! echo Files were saved to: %destination% echo ============================================= pause
🎯 What does it do:
  • Copies all files from the folder you specify in source
  • Automatically creates a folder with a date (e.g. 2025-11-16)
  • Keeps subfolders as is
  • Replaces old files only if they have changed
⚠️ Attention:

Change it set destination=... to your own destination folder.

Examples of safe options:
• External disk: E:\Backup
• USB Stick: F:\USB_Backup
• Second internal disk: D:\MyFiles\Backups.Make sure there is enough free space!

🔄 Example 3: Bulk Rename Files with Preview

This script does the same as before, but first displays what your files will be named so you can make sure they are correct before proceeding with the renaming.

📄 rename_files_preview.bat
@echo off echo ============================================= echo Bulk File Renaming - Preview echo ============================================= echo REM Navigate to the folder with your files cd /d "C:\Users\%username%\Pictures\Fotografia" REM Set up a counter for numbering set counter=1 echo The following files will be renamed: for %%f in (*.jpg) do ( echo %%f -> Vacation_Summer_2025_!counter!.jpg set /a counter+=1 ) echo. set /p proceed="Do you want to proceed with renaming? (Y/N): " if /i "%proceed%"=="Y" ( set counter=1 for %%f in (*.jpg) do ( ren "%%f" "Vacation_Summer_2025_!counter!.jpg" set /a counter+=1 ) echo Renaming completed! ) else ( echo Operation canceled. ) pause
🎯 What does it do:
  • Shows preview of new names before renaming them
  • Asks the user if they want to continue
  • Bulk renames files in numerical order only if confirmed
  • Safe for beginners, reduces the possibility of errors
💡 How the script selects the files:
  • The line cd /d "C:\Users\%username%\Pictures\Fotografia" determines which folder to look for files in. You can change the path to point to the folder containing your files.
  • The file mask *.jpg on for %%f in (*.jpg) selects all files with the extension .jpg in the folder. You can change it to *.png, *.mp3 ή *.* for all files.
  • The script **doesn't have to be in the same folder as the files**. You can keep it in a separate folder and just change the cd /d on the desired route.
  • The user first sees the list of files and new names before any changes are made, and decides whether to continue with the Y/N.
💡 Extra customizations:
  • You can add more file types, B.C. for %%f in (*.jpg *.png).
  • You can change the base name Vacation_Summer_2025 to any desired.
  • You can preview only specific files by creating a separate mask, e.g. IMG_*.jpg.

🌐 Example 4: Opening Multiple Web Pages with Preview

This script first shows the web pages it is about to open, so you can check that they are correct, and then asks you if you want to proceed.

📄 open_websites_preview.bat
@echo off echo ============================================= echo Favorite Websites Preview echo ====================================== echo. REM List of websites to open set websites=https://www.google.com https://www.youtube.com https://mail.google.com https://www.facebook.com echo The following websites will be opened: for %%w in (%websites%) do ( echo %%w ) echo. set /p proceed="Do you want to open all these websites? (Y/N): " if /i "%proceed%"=="Y" ( for %%w in (%websites%) do ( start %%w timeout /t 1 /nobreak >nul ) echo All websites opened! timeout /t 3 /nobreak >nul ) else ( echo Operation canceled. ) exit
🎯 What does it do:
  • It first shows the list of websites to open
  • Asks the user for confirmation before opening sites
  • Opens web pages with a 1 second delay between them
  • Safe for beginners and easy to adapt
💡 Customization:
  • You can add or remove websites from the list set websites=...
  • Change the delay time to timeout /t 1 if you want a longer interval between openings

📊 Example 5: Organizing Files by Type

The "Downloads" folder usually becomes a mess! This program automatically organizes all files into folders by type:

📄 organosi_arxeion.bat
@echo off echo ====================================== echo Organizing Downloads Folder echo ====================================== echo. REM Navigate to the Downloads folder cd /d "%userprofile%\Downloads" REM Create folders for each file type if not exist "Images" mkdir "Images" if not exist "Documents" mkdir "Documents" if not exist "Music" mkdir "Music" if not exist "Videos" mkdir "Videos" if not exist "Archives" mkdir "Archives" if not exist "Applications" mkdir "Applications" REM Move images echo Organizing images... move *.jpg "Images\" 2>nul move *.jpeg "Images\" 2>nul move *.png "Images\" 2>nul move *.gif "Images\" 2>nul REM Move documents echo Organizing documents... move *.pdf "Documents\" 2>nul move *.docx "Documents\" 2>nul move *.doc "Documents\" 2>nul move *.xlsx "Documents\" 2>nul move *.txt "Documents\" 2>nul REM Move music files echo Organizing music... move *.mp3 "Music\" 2>nul move *.wav "Music\" 2>nul move *.flac "Music\" 2>nul REM Move videos echo Organizing videos... move *.mp4 "Videos\" 2>nul move *.avi "Videos\" 2>nul move *.mkv "Videos\" 2>nul REM Move compressed files echo Organizing archives... move *.zip "Archives\" 2>nul move *.rar "Archives\" 2>nul REM Move applications echo Organizing applications... move *.exe "Applications\" 2>nul move *.msi "Applications\" 2>nul echo. echo ====================================== echo Organization completed! echo Check your new folders. echo ====================================== pause

🔍 How It Works:

  • cd /d "%userprofile%\Downloads" - Goes to the user's Downloads folder
  • if not exist "Folder" mkdir "Folder" - Creates folder only if it doesn't already exist
  • move *.extension "Folder\" - Moves all files with a specific extension to the corresponding folder
  • 2>nul - Hides error messages (e.g. if there are no .mp3 files)
💡 Customizations with Examples:
  • Add more extensions – If you want to organize other types of files, add new lines move.
    Example for graphics: move *.psd "Images\" 2>nul, move *.ai "Images\" 2>nul, move *.svg "Images\" 2>nulAll Photoshop, Illustrator and SVG files will go into the `Images` folder.
  • Create new folders – If you want folders by topic or project, first create the folder with: if not exist "School" mkdir "School" and if not exist "Work" mkdir "Work", and then moved the files there with move.
  • Change the folder path – To organize a folder other than Downloads, replace the command: cd /d "%userprofile%\Downloads" with the path of the folder you want, e.g. cd /d "D:\Work\Projects".
  • Add a date or timestamp to folders – Use the command %date% ή %date:~-4%-%date:~-7,2%-%date:~-10,2% to create type folders Images_2025-11-16 and keep files organized by date.
⚠️ Attention: Run the script on a test folder first to see how it works! Do not use it on a folder with important files without a backup.

🖥️ Example 6: Quick System Information

Want to quickly see information about your computer? This program creates a report with useful details, saves everything to a TXT file on the Desktop, and opens it automatically.

📄 plirofories_systimatos.bat
@echo off echo == echo. REM Create a report file on the Desktop set reportfile=%userprofile%\Desktop\System_Info_%date:~-4%%date:~-7,2%%date:~-10,2%.txt echo System Report - %date% %time% > "%reportfile%" echo ========================================================================= >> "%reportfile%" echo. >> "%reportfile%" echo Collecting system information... echo --- GENERAL INFORMATION --- >> "%reportfile%" systeminfo | findstr /C:"OS Name" /C:"OS Version" /C:"System Type" >> "%reportfile%" echo. >> "%reportfile%" echo --- CPU --- >> "%reportfile%" wmic cpu get name /format:list >> "%reportfile%" echo. >> "%reportfile%" echo --- RAM --- >> "%reportfile%" wmic memorychip get capacity /format:list >> "%reportfile%" echo. >> "%reportfile%" echo --- HARD DISKS --- >> "%reportfile%" wmic logicaldisk get deviceid,size,freespace /format:list >> "%reportfile%" echo. >> "%reportfile%" echo --- GPU --- >> "%reportfile%" wmic path win32_VideoController get name /format:list >> "%reportfile%" echo. >> "%reportfile%" echo --- NETWORK ADAPTERS --- >> "%reportfile%" ipconfig /all >> "%reportfile%" echo. >> "%reportfile%" echo --- INSTALLED PROGRAMS --- >> "%reportfile%" wmic product get name,version >> "%reportfile%" echo. >> "%reportfile%" echo --- ACTIVE USERS --- >> "%reportfile%" query user >> "%reportfile%" echo. >> "%reportfile%" echo. echo ======================================= echo Report created! echo Saved on Desktop as: System_Info_%date:~-4%%date:~-7,2%%date:~-10,2%.txt echo ============================================= pause REM Open the report automatically start notepad "%reportfile%"
🎯 What does it do:
  • Collects operating system details
  • Displays processor (CPU) and RAM information
  • Shows available space on hard drives
  • Provides information about GPU (graphics card)
  • Displays networks and connections (Network Adapters)
  • List of installed programs with versions
  • Shows active users in the system
  • Saves everything to a TXT file on the Desktop
  • Automatically opens the file for immediate viewing
💡 Useful for: When you want to send technical details for support, keep a record of your computer's specifications, or have a quick report on updates, installations, and active users.
⚙️ Recommended extensions:
  • You can add network information: netstat -an >> "%reportfile%"
  • You can collect details for services: wmic service get name,startmode,state >> "%reportfile%"
  • Add date and time to file name to keep history: we already use %date% and %time%

⏰ How to run automatically at a specific time?

You can set your programs to run automatically using the Task Scheduler Windows. This is useful for everyday tasks like cleaning, backing up, or organizing folders without having to do it manually.

1 Open Task Scheduler

Click Start and type "Task Scheduler". Open the program. On the home screen you will see the existing tasks and you can check which program is running automatically.

2 Create a new task

Click on "Create Basic Task" (Create Basic Task) in the right menu. Give it a descriptive name and a short description so you can remember what the task does later.

3 Schedule setting

Choose when you want the task to run: Casual, Weekly, Only once or when an event occurs. Set the exact time, e.g. 23:00 to clean temporary files or 08:00 to open work web pages.

4 Selecting the BAT file

In the "Action" field, select "Start a program" and press Browse to find the file .bat that you want to run automatically. You can select any BAT file you have created, e.g. for cleaning, backup or folder organization.

✨ Automation examples:
  • Every morning at 8:00: Opening work websites to start the day ready.
  • Every Sunday at 22:00: Back up important files.
  • Every day at 23:00: Clean up temporary files to free up space on your computer.
  • Every Friday at 18:00 PM: Organize Downloads folder and sort files by type.
  • Customized program: You can combine multiple tasks at different times or days, depending on your needs.

⚠️ Safety Tips

BAT files are powerful tools and can make changes to your system, so be careful. Follow these guidelines for safe use and to avoid problems.

🔒 Safety Rules:
  • Do not run BAT files from strangers: Only your own or from trusted sources.
  • Try first: Before using a new BAT file, test it on non-important files or folders.
  • Keep backups: Before you do anything that deletes or moves files, make a backup.
  • Read the code: Open the file with Notepad and see what it does before running it.
  • Be careful on the routes: Make sure the folders you are using exist to avoid errors.
💡 Best practices: Add the command to the beginning of each BAT file @echo on instead @echo off the first time you try it. This way you'll see every command being executed and understand exactly what the program does before you automate it.
💡 Extra tips:
  • You can add notification actions, such as msg %username% "Η εργασία ολοκληρώθηκε" to display a message after execution.
  • Use log files with >&gt log.txt to record what was executed.
  • For more complex scripts, you can combine multiple BAT tasks into one program and have Task Scheduler execute them in sequence.

🎯 Conclusion

BAT files are a simple and free way to save time and make your daily computer work easier and more efficient. Start with the simple examples you saw here, experiment, and soon you'll be creating your own automations that fit your needs exactly!

📚 Resources & Learning for Scripts & BAT Files

If you want to delve deeper into scripting in Windows, better understand commands, and learn safe practices, you can utilize the following resources:

🌐 Websites:

📖 Books:

  • "The Book of Batch Scripting" – Jack McLarney — Available as a paperback with ISBN 9781718503427.
  • "Batch Scripting Essentials: Definitive Reference" – Richard Johnson — Professional guide to scripting, available in e-book.

💡 Extra Tips:

  • Experiment first with simple scripts in a test folder to see how the commands work.
  • use it @echo on to monitor each command as it executes.
  • Create backups before running scripts that delete or move files.
  • Gradually add new commands and functions to automate everyday tasks.

Using these resources, you can learn to create safe and efficient scripts for any type of task, from simple folder cleanups to automated backups and bulk file renaming.

???? Do you have questions or ideas for new automations?
Leave a comment below and we'll be happy to help you!

📌 Useful Shortcuts:

BAT = Batch File | CMD = Command Prompt | REM = Comment in code


Evangelos
✍️ Evangelos
Its creator LoveForTechnology.net — an independent and trusted source for tech guides, tools, and practical solutions. Each article is based on personal testing, evidence-based research, and care for the average user. Here, technology is presented simply and clearly.



RELATED TOPICS


⭐ Important Posts

 How to create a network in windows NETWORK

How to create windows network: Connect 2 or more computers easily

 QLED vs Mini-LED = SCREENS

QLED vs Mini-LED: What are the differences between them?

💬 Comments

Share your thoughts

Loading comments...