Windows Commands: Conditionally perform a command several times


FOR-Windows Command

FOR

Conditionally perform a command several times.

syntax-FOR-Files>
      FOR %%parameter IN (set) DO command

syntax-FOR-Files-Rooted at Path
      FOR /R [[drive:]path] %%parameter IN (set) DO command

syntax-FOR-Folders
      FOR /D %%parameter IN (folder_set) DO command

syntax-FOR-List of numbers
      FOR /L %%parameter IN (start,step,end) DO command

syntax->FOR-File contents
      FOR /F ["options"] %%parameter IN (filenameset) DO command

      FOR /F ["options"] %%parameter IN ("Text string to process") DO command

syntax-FOR-Command Results
      FOR /F ["options"] %%parameter IN ('command to process') DO command

The operation of the FOR command can be summarised as...

If you are using the FOR command at the command line rather than in a batch program, specify %parameter instead of %%parameter.

FOR Parameters

The first parameter has to be defined using a single character, I tend to use the letter G.

e.g. FOR %%G IN ...

In each iteration of a FOR loop, the IN ( ....) clause is evaluated and %%G set to a different value

If this results in a single value then %%G is set = to that value and the command is performed.

If this results in a multiple values then extra parameters are implicitly defined to hold each. These are automatically assigned in alphabetical order %%H %%I %%J ...(implicit parameter definition)

For example

FOR /F %%G IN ("This is a long sentence") DO @echo %%G %%H %%J

will result in the output

This is long

You can of course pick any letter of the alphabet other than %%G.

%%G is a good choice because it does not conflict with any of the pathname format letters (a, d, f, n, p, s, t, x) and provides the longest run of non-conflicting letters for use as implicit parameters.
G > H > I > J > K > L > M

Other Environment variables
Environment variables within a FOR loop are expanded at the beginning of the loop and won't change until AFTER the end of the DO section.
The following example counts the files in the current folder, but %count% always returns 1:

@echo off
SET count=1
 FOR /f "tokens=*" %%G IN ('dir /b') DO (
 echo %count%:%%G
 set /a count+=1)

To make this work correctly we must force the variable %count% to be evaluated during each iteration, using the CALL :subroutine mechanism:

@echo off
SET count=1
FOR /f "tokens=*" %%G IN ('dir /b') DO (call :s_do_sums "%%G")
GOTO :eof

:s_do_sums
 echo %count%:%1
 set /a count+=1
 GOTO :eof

Nested FOR commands

FOR commands can be nested FOR %%G... DO (for %%U... do ...)
when nesting commands choose a different letter for each part. you can then refer to both parameters in the final DO command.

If Command Extensions are disabled, the FOR command will only support the basic syntax with no enhanced variables:
FOR %%parameter IN (set) DO command [command-parameters]

Other Windows 98, Windows XP Commands:
addusers , arp , assoc , associate , at , attrib , browstat , cacls , call , cd , change , chkdsk , chkntfs , choice , cipher , cleanmgr , clip , cmd , color , comp , con2prt , copy , date , defrag , del , delprof , dir , diruse , diskcomp , diskcopy , doskey , dsadd , dsmod , echo , endlocal , exit , expand , expand , fc , fdisk , find , findstr , for , forfiles , format , fsutil , ftp , ftype , global , goto , help , hfnetchk , if , ifmember , ipconfig , kill , label , ldifde , local , logevent , logoff , logtime , mapisend , md , mem , mode , monitor , more , mountvol , move , moveuser , msg , msiexec , msinfo32 , mstsc , munge , mv , nbtstat , net , netdom , netsh , netstat , netsvc , now , nslookup , ntbackup , ntrights , path , pathping , pause , perms , ping , popd , portqry , print , prncnfg , prnmngr , prompt , pushd , qgrep , rasdial , rasphone , rd , rdisk , recover , reg , regedit , regsvr32 , rem , ren , replace , rmtshare , robocopy , route , runas , rundll32 , sc , schtasks , sclist , scriptit , set , setlocal , setx , share , shift , shortcut , showgrps , showmbrs , shutdown , sleep , soon , sort , start , su , subinacl , subst , tasklist , time , timeout , title , touch , tracert , type , ver , verify , vol , where , whoami , windiff , winmsd , winmsdp , wmic , xcacls , xcopy ,
Live2Support.com :
Product - Benefits - Features - How Does it Works - Pricing - SSL Certificates - Signup - Contactus - Login - Submit Resources