best command prompt script examples popular batch files explained clearly

Popular batch files solve practical Windows automation tasks. Below are concise examples and explanations.

Automated File Backup

Purpose: Daily backup of critical folders.

  • @echo off hides command output
  • set source= and set target= define paths
  • xcopy mirrors directories

Script:

best command prompt script examples popular batch files explained clearly

@echo off

set source="C:ProjectFiles"

set target="D:BackupProject_%date:~10,4%%date:~4,2%%date:~7,2%"

xcopy %source% %target% /E /H /C /I /Y

Silent Software Installer

Purpose: Unattended application deployment.

best command prompt script examples popular batch files explained clearly
  • /quiet suppresses setup UI
  • /norestart postpones reboots
  • Error handling via %errorlevel%

Script:

@echo off

start /wait * /quiet /norestart

if %errorlevel% NEQ 0 (

echo Installation failed with code %errorlevel%

best command prompt script examples popular batch files explained clearly

exit /b 1

Network Device Ping Check

Purpose: Monitor server/device availability.

  • >nul suppresses ping output
  • Loop using :retry label
  • Logs status changes

Script:

@echo off

:retry

best command prompt script examples popular batch files explained clearly

ping 192.168.1.1 -n 1 >nul

if %errorlevel% EQU 0 (

echo Device active - %time% >> *

) else (

echo OFFLINE detected! >> *

best command prompt script examples popular batch files explained clearly

timeout /t 60 >nul

goto retry

Batch File Variables & Paths

Critical Techniques:

  • Dynamic paths: %~dp0 references script's directory
  • User input: set /p username=Enter name:
  • Arguments: %1, %2 for passed parameters

Timestamped Logging

Purpose: Create execution logs with timestamps.

Script:

best command prompt script examples popular batch files explained clearly

@echo off

call :logentry "Task started"

REM Your commands here

call :logentry "Operation complete"

exit /b

best command prompt script examples popular batch files explained clearly

:logentry

echo [%date% %time%] %~1 >> *

exit /b

Related News