- Iterate through files in a directory:
C:\> CycleFiles.bat
CycleFiles.bat@echo off title **** Cycle through files **** echo iterating files in a directory setlocal EnableDelayedExpansion for %%I in (filesdir\*.*) do ( rem Extracting file informations from path rem file name (without extension) : ~n rem file extension (dot included) : ~x rem file attribute : ~a rem file time : ~t rem drive letter : ~d rem path without drive : ~p rem absolute path : ~s rem file length (in bytes) : ~z rem Example: extract file name in FN set FN=%%~nI rem do some operation echo %%I has filename !FN! rem if NOT errorlevel 1 (echo !FN! : OK) else (echo !FN! : ERROR) ) - Access script arguments:
C:\> PassingArgs.bat arg1 arg2
PassingArgs.bat@echo off title **** Passing arguments **** echo. echo First argument: %1 echo Second argument: %2 - Delete files in the temporary directory:
C:\> CleanTempFiles.bat
CleanTempFiles.bat@echo off title *** Clean Windows temp folder *** rem Removing files echo Deleting files in "%TEMP%\*.*" del /S /Q /F "%TEMP%\*" rem This would remove all folders including temp dir!! rem rmdir /S /Q "%TEMP%" - Delete Borland C++ Builder temporary and backup files:
C:\> CleanBcbTemp.bat
CleanBCBTemp.battitle *** Clean BCB temp and backup files *** del /S "*.tds" del /S "*.obj" del /S "*.dcu" del /S "*.~h" del /S "*.~???" pause - Set some environmental variables (ex. MinGW environment):
C:\> SetMinGWEnv.bat
SetMinGWEnv.bat@echo off title Set Windows environment for msys/MinGW rem 'setx' is part of the 'Windows XP Service Pack 2 Support Tools' rem 'pathman' is part of 'Windows 2003 Resource Kit' rem *** Temporary set tmpMSYS=%SYSTEMDRIVE%\msys set tmpMINGW=%tmpMSYS%\mingw rem *** Set permanent changes in User environment setx MINGW "%tmpMINGW%" pathman /au %tmpMSYS%\bin pathman /au %tmpMINGW%\bin - List path:
C:\> ListPath.bat
ListPath.bat@echo off title Check Windows path entries rem See whole value echo PATH = %PATH% echo. rem List entries set _path="%PATH:;=" "%" for %%p in (%_path%) do if not "%%~p"=="" echo %%~p pause