Introduction
Batch scripting can automate repetitive tasks like backups, file cleanup, scheduled tasks, app launching, system maintenance, and more. This tutorial shows how to automate workflows using core Batch tools.
1. Automating File Backups
@echo off
set src=C:\Data
set dest=D:\Backup
xcopy "%src%" "%dest%" /E /I /Y
echo Backup complete!
2. Auto-Deleting Old Files
Delete files older than 7 days:
forfiles /p "C:\Logs" /s /m *.* /d -7 /c "cmd /c del @file"
3. Launch Multiple Programs at Once
start chrome.exe
start notepad.exe
start steam.exe
4. Automate System Cleaning
del /q C:\Windows\Temp\*
del /q %temp%\*
ipconfig /flushdns
5. Running Commands Silently
start "" /min cmd /c "script.bat"
6. Creating a Log File
echo Starting backup... >> backup.log
xcopy C:\Data D:\Backup /E >> backup.log
echo Finished at %time% >> backup.log
7. Auto-Compress Files with PowerShell
powershell -command "Compress-Archive -Path C:\Folder -Destination D:\Archive.zip"
8. Auto-Restart a Program if It Crashes
:loop
tasklist | find "myapp.exe" >nul
if errorlevel 1 start myapp.exe
timeout /t 5 >nul
goto loop
9. Automate Scheduled Tasks (schtasks)
Create a scheduled task to run daily at 6 AM:
schtasks /create /tn "DailyBackup" /tr C:\backup.bat /sc daily /st 06:00
Delete it:
schtasks /delete /tn "DailyBackup" /f
10. Automate Network Tasks
net use Z: \\Server\Share /user:Admin password
robocopy C:\Files Z:\Files /E
net use Z: /delete
Summary
- Automate backups using xcopy/robocopy.
- Use
forfilesto clean old files. - Launch multiple apps with
start. - Log automation results for debugging.
- Create scheduled tasks with
schtasks. - Use loops to restart apps automatically.
- Mix PowerShell for advanced automation.