Guide to the use of batch files and sample code.
Batch Scripts are stored in simple text files containing lines with commands that get executed in sequence, one after the other. Scripting is a way by which one can alleviate this necessity by automating these command sequences in order to make ones life at the shell easier and more productive.
A batch file consists of a series of commands to be executed by the command-line interpreter, stored in a plain text file. A batch file may contain any command the interpreter accepts interactively and use constructs that enable conditional branching and looping within the batch file, such as IF, FOR, and GOTO labels. The term “batch” is from batch processing, meaning “non-interactive execution”, though a batch file may not process a batch of multiple data.
Samples
Convert a file to lower case.
@echo off if {%1}=={} @echo Syntax: LwrCase FullyQualifiedFileName&goto :EOF if not exist %1 @echo LwrCase - %1 NOT found.&goto :EOF setlocal for /f "Tokens=*" %%a in ('@echo %~a1') do ( set file=%%a ) if /i "%file:~0,1%" EQU "d" @echo LwrCase - %1 is NOT a file.&endlocal&goto :EOF for /f "Tokens=*" %%f in ('dir %1 /L /b /a /a-d') do ( Rename %1 "%%f" ) endlocalConvert all files in a folder to lower case.
@echo off if {%1}=={} @echo Syntax: LwrCase_Folder FullyQualifiedDirectoryName&goto :EOF if not exist %1 @echo LwrCase_Folder - %1 NOT found.&goto :EOF setlocal for /f "Tokens=*" %%a in ('@echo %~a1') do ( set folder=%%a ) if /i "%folder:~0,1%" NEQ "d" @echo LwrCase_Folder - %1 is NOT a folder.&endlocal&goto :EOF pushd %1 set sw=/B /A /A-D if /i {%2}=={/S} set sw=%sw% %2 for /f "Tokens=*" %%f in ('dir %sw%') do ( call LwrCase "%%f" ) popd endlocal
Remove spaces from all files in a folder.
@echo off setlocal enabledelayedexpansion for %%j in (*.*) do ( set filename=%%~nj set filename=!filename:.=! set filename=!filename: =! if not "!filename!"=="%%~nj" ren "%%j" "!filename!%%~xj" )