for loop bat code example

Example 1: batch loops

set LoopVar=0
:Loop
echo %LoopVar%
set /a LoopVar=%LoopVar%+1
if not %LoopVar%==100 goto Loop
echo Done!
pause

------------------------------------
            HOW IT WORKS
------------------------------------
set LoopVar=0 <--- Sets the counter to 0
:Loop
echo %LoopVar% <--- Echos Status
set /a LoopVar=%LoopVar%+1 <--- Adds +1 to the Var "LoopVar"
if not %LoopVar%==100 goto Loop <-- Until "LoopVar" equals 100 it repeats the job. (You can change the 100 to any number you would like.)
echo Done!
pause

Example 2: cmd for loop

REM # | EXAMPLE: For loop to copy all the files in the current directory to the C:\User\ folder
For %F in (*) do copy %F C:\User\

REM # | SYNTAX
REM # | For %F in (<Files to loop through with space delimiters>) do <Command to do with %F>


REM # | NOTE: you can use the following to extract specific parts of a filepath
REM # | take note that this syntax only works when run from a batch file
@echo off
setlocal
set file=C:\Users\l72rugschiri\Desktop\fs.cfg
for %%N IN ("%file%") do (
   	echo filedrive=%%~dN
	echo filepath=%%~pN
	echo filename=%%~nN
	echo fileextension=%%~xN
    echo default=%%N
)

Tags:

Misc Example