Unlock Windows Automation: Powerful Batch Scripts vs Python
This article explores the advantages of Windows batch scripting for automation, detailing basic commands, practical use cases such as file organization, build folder cleanup, scheduled Python tasks, Git operations, and system junk removal, while comparing its simplicity and efficiency to Python scripts.
1. Introduction
When people think of automation solutions they often reach for Python scripts, but Windows batch scripting remains a lightweight, environment‑free alternative that can be more concise, convenient, and efficient for certain tasks.
2. Batch Basics
Creating a batch file is simple: create a text file, set its encoding to ANSI, write the script logic, and save it with a .bat extension.
Common batch commands include: echo – output logs to the console. :: or rem – comment lines. title / color – set window title and background color. cd – change directory. md – create a directory. dir – list directory contents. rd – remove a directory. del – delete files (with options to remove hidden, read‑only, or system files). copy – copy files. pause – pause execution, usually placed at the end of a script. goto – jump to a label, often used for loops. for – loop over files or directories, similar to Python's for. if – conditional execution. set – define variables. start – launch external programs.
3. Practical Scenarios
3.1 Organize files by extension
@echo off
for %%i in (*) do (
md %%~xi
move *%%~xi %%~xi
)
pauseThis four‑line script creates a folder for each file extension in the current directory and moves the corresponding files into it.
3.2 Delete all build folders
@echo off
:: open to current directory
cd /d "%~dp0"
echo Starting deletion
:: recursive delete of folders named *build*
for /r /D %%i in (*build*) do rd /s /q "%%i"
echo Deletion complete
pauseThe script navigates to the project root and removes every build directory, useful after Android Studio builds multiple modules.
3.3 Schedule a Python script every 5 minutes
@echo off
title Loop Run Python Script
:: interval in seconds (5 minutes = 300)
set INTERVAL=300
echo Starting execution - %time%
python C:/test.py
:Task
echo Starting execution - %time%
python C:/test.py
timeout %INTERVAL%
goto TaskThe script runs the Python script once, then enters an infinite loop that waits %INTERVAL% seconds before running it again.
3.4 One‑click Git commit
@echo off
title Commit Code
echo Commit code, simplify operation
:: show status
git status
:: prompt for commit message
set /p commit_msg=Enter commit message:
:: four git commands
git add .
git commit -m %commit_msg%
git pull
git push
echo Commit successful
pauseDouble‑clicking this batch file prompts for a commit message and executes the full Git workflow.
3.5 Clean system junk files
@echo off
:: configuration
title Clean System Junk
color 03
mode con cols=42 lines=20
echo Executing cleaning, please wait...
:: delete temporary and log files
del /f /s /q %systemdrive%\*.tmp >nul 2>&1
del /f /s /q %systemdrive%\*._mp >nul 2>&1
del /f /s /q %systemdrive%\*.log >nul 2>&1
... (additional del commands omitted for brevity) ...
echo System junk cleaning completed!
pauseThe script removes temporary files, logs, caches, and other unnecessary files across system drives and user profiles.
4. Conclusion
The examples above demonstrate how batch scripts can quickly automate routine tasks on Windows. Python offers elegant syntax and a rich ecosystem, while batch excels in scenarios where no additional runtime is required. They can be combined—for instance, converting a batch script to an executable that Python calls—to achieve complex automation workflows.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
