julien-ref-batch
Best practices for Windows Batch scripts (.bat). Covers encoding, path handling, variables, error management, and common pitfalls.
When & Why to Use This Skill
This Claude skill serves as a comprehensive technical reference and boilerplate generator for creating robust Windows Batch (.bat) scripts. It enforces industry best practices such as UTF-8 encoding (chcp 65001), secure path handling with quotes, delayed variable expansion, and standardized error management. It is designed to help developers and system administrators build reliable, maintainable automation scripts while avoiding common pitfalls like encoding conflicts and path-related failures in Windows environments.
Use Cases
- Developing robust automation scripts for routine system maintenance, backups, and file management tasks.
- Refactoring and debugging legacy .bat files that fail due to spaces in file paths or incorrect character encoding.
- Standardizing script headers and error-trapping logic across IT teams to ensure consistent execution and observability.
- Creating interactive command-line tools that require validated user input and clear visual status feedback.
- Setting up automated environment configurations and software deployment scripts with proper prerequisite checks.
| name | julien-ref-batch |
|---|---|
| description | Best practices for Windows Batch scripts (.bat). Covers encoding, path handling, variables, error management, and common pitfalls. |
Best Practices - Scripts Batch (.bat)
STRUCTURE ET ENCODAGE
Observability
First: At the start of execution, display:
🔧 Skill "julien-ref-batch" activated
Encodage et Affichage
@echo off
chcp 65001 > nul ; UTF-8 pour caractères spéciaux
setlocal enabledelayedexpansion ; Variables dynamiques
En-tête Standard
@echo off
chcp 65001 > nul
setlocal enabledelayedexpansion
echo ========================================
echo NOM DU SCRIPT - DESCRIPTION
echo ========================================
echo.
GESTION DES CHEMINS
Variables de Chemin Robustes
set "SCRIPT_DIR=%~dp0" ; Répertoire du script
set "ROOT_DIR=%SCRIPT_DIR%.." ; Répertoire parent
set "TARGET_EXE=%ROOT_DIR%\app.exe" ; Exécutable cible
set "WORK_DIR=%SCRIPT_DIR%" ; Répertoire de travail
Validation des Chemins
if not exist "%TARGET_EXE%" (
echo ERREUR: app.exe non trouve dans %ROOT_DIR%
echo.
pause
exit /b 1
)
VARIABLES ET CONDITIONS
Syntaxe Variables Correcte
; CORRECT - Utiliser %VAR% au lieu de !VAR!
set "RESULT=%ERRORLEVEL%"
if %RESULT% equ 0 (
echo SUCCES
) else (
echo ECHEC - Code: %RESULT%
)
; ÉVITER - !VAR! seulement si nécessaire
if !RESULT! equ 0 (...)
Tests de Conditions
; Tests d'existence
if exist "%FILE%" (echo Fichier trouve)
if not exist "%FILE%" (echo Fichier manquant)
; Tests de variables
if defined VAR (echo Variable definie)
if /i "%CHOICE%"=="y" (echo Oui choisi)
; Tests numériques
if %CODE% equ 0 (echo Code zero)
if %CODE% neq 0 (echo Code non-zero)
BOUCLES ET LABELS
Labels et Goto
:main_test
echo Test en cours...
goto summary
:summary
echo Fin des tests
Boucles For
; Boucle sur fichiers
for %%f in (*.txt) do (
echo Traitement: %%f
)
; Boucle sur liste
for %%i in (test1 test2 test3) do (
echo Test: %%i
)
ENTRÉES UTILISATEUR
Input Simple
set /p "CHOICE=Continuer (y/n)? "
if /i not "%CHOICE%"=="y" goto end
Choice avec Options
choice /c yn /m "Voulez-vous continuer (Y/N)?"
if errorlevel 2 goto no
if errorlevel 1 goto yes
GESTION DES ERREURS
Codes de Sortie
"%EXECUTABLE%" "%SCRIPT%"
set "RESULT=%ERRORLEVEL%"
if %RESULT% equ 0 (
echo SUCCES - Code: %RESULT%
) else (
echo ECHEC - Code: %RESULT%
exit /b %RESULT%
)
Validation des Prérequis
; Vérifier exécutable
if not exist "%EXE_PATH%" (
echo ERREUR: Exécutable manquant
pause
exit /b 1
)
; Vérifier script
if not exist "%SCRIPT_PATH%" (
echo ERREUR: Script manquant
goto next_test
)
AFFICHAGE ET FEEDBACK
Messages Propres (Sans Emojis)
echo SUCCES: Operation terminee
echo ERREUR: Fichier non trouve
echo ATTENTION: Script non lance automatiquement
Séparateurs Visuels
echo ========================================
echo RESUME DES TESTS
echo ========================================
Pause et Attente
pause >nul ; Pause silencieuse
timeout /t 3 >nul ; Attente 3 secondes
SÉCURITÉ ET ROBUSTESSE
Variables Sécurisées
; Guillemets pour espaces
set "PATH_WITH_SPACES=%USERPROFILE%\My Documents"
; Validation avant usage
if not defined REQUIRED_VAR (
echo Variable requise manquante
exit /b 1
)
Nettoyage Final
:cleanup
; Nettoyer variables temporaires
set "TEMP_VAR="
set "RESULT1="
set "RESULT2="
echo Nettoyage termine
exit /b 0
EXEMPLE COMPLET - SCRIPT DE TEST
@echo off
chcp 65001 > nul
setlocal enabledelayedexpansion
echo ========================================
echo SCRIPT DE TEST AUTOMATISE
echo ========================================
echo.
set "SCRIPT_DIR=%~dp0"
set "ROOT_DIR=%SCRIPT_DIR%.."
set "TARGET_EXE=%ROOT_DIR%\app.exe"
; Validation prérequis
if not exist "%TARGET_EXE%" (
echo ERREUR: app.exe non trouve
pause
exit /b 1
)
; Test 1
echo Test 1: Compilation...
"%TARGET_EXE%" "test1.ahk"
set "RESULT1=%ERRORLEVEL%"
if %RESULT1% equ 0 (
echo SUCCES: Test 1 passe
) else (
echo ECHEC: Test 1 - Code %RESULT1%
)
; Résumé
echo.
echo ========================================
echo RESUME
echo ========================================
if %RESULT1% equ 0 (
echo Test 1: SUCCES
) else (
echo Test 1: ECHEC
)
pause >nul
ERREURS COMMUNES À ÉVITER
- Encodage - Toujours utiliser
chcp 65001pour UTF-8 - Variables - Préférer
%VAR%à!VAR!sauf cas spéciaux - Chemins - Toujours utiliser des guillemets pour les espaces
- Emojis - Éviter dans les scripts batch (problèmes d'encodage)
- PowerShell - Utiliser
cmd /cpour forcer CMD au lieu de PS - Caractères spéciaux - Attention aux accents dans les echo