Read Mode

The FOR Command - Part II - FOR Loop Switches | Learn Batch Programming - Part III

Important cmd Commands: THE FOR LOOP or we can say, THE FOR COMMAND IN CMD is a very important and one of the most annoying CMD commands that can cause Chaos in a simple batch code. There are many batch for loop files available on the internet, that will help you in understanding THE POWER OF FOR LOOP IN CMD and How to loop a command in CMD. and, in this Part-II of the Third Part of the series -  LEARN BATCH PROGRAMMING, We are going to Learn about Special Switches in FOR Command in CMD.
We all Know, Linux is Great... 
It Does INFINITE Loops in 5 Seconds!
-Linus Torvalds
The FOR Command - Part II - FOR Loop Switches | Learn Batch Programming - Part III

The FOR Command Switches | Learn Batch Programming - Part III

There are mainly Four Switches that you can associate with the FOR Command/FOR Loop. And, As we already know that the For Loop is used to Looping through Different commands, digits and another set of commands, numbers, digits to Repeat the same action multiple times. The Following are the List of All possible Switches that you will use with the FOR command in CMD.

Use of For /F Switch Examples

I have used this switch most of the time, And it is used to Execute the Commands within the For loop parameters and Use the Commands output in custom pieces to serve different purposes. IN SIMPLE WORDS, It is used to manipulate other commands output.
e.g. For /F %%A in ('tasklist') do (Echo.%%A && Timeout /t 1 >nul)

The FOR Command - Part II - FOR Loop Switches | Learn Batch Programming - Part III

This will Print the Currently running programs on the cmd console. As, We've used ECHO, it will print all of them one by one, with a delay of 1 second in each printing iteration.

Note: This Switch is little Complex with great Possibilities, So - I recommend to watch the Video Embedded at the End of the Article to get its complete working.

For Every FOR Command you use For you Batch Code,
is FOR the betterment of CODE.


Use of For /L Switch Examples

The For /L Switch is the second favorite switch, which I have used the most. It is mainly used for Simple repetition of the LOOP for a specified amount of numbers. IN OTHER WORDS, you can run the FOR command for 100 iterations using the FOR /L switch.
E.g. For /L %%A in (1,1,100) Do (Echo.%%A)

The Above Example will print, First 100 natural numbers on the CMD console, Starting from 1. Try this in your Batch Files to see the Working output.
Also Read: Make Awesome Calc with Notepad | Awesome Calc v.1.0 By Kabue

Use of For /D Switch Examples

FOR /D switch is another great switch, But honestly - I don't think you gonna use this one more often. Because, All it does is, just find the Folders in current/Given directory that has match letters passed to the FOR Command. (Watch the video for better understanding)
E.g. For /D %%A in (p*) do (Echo.%%A)

FOR /D Switch

The Above Example Code will Print all the folders Starting from the Letter P, From the current working Directory to the CMD console.

Use of For /R Switch Examples

The Last, But not the Least - FOR Command Switch, it is one of the least used CMD Commands. As, Personally - I have never used it. But, While making the Learn Batch Programming Series, I have to go through it. And, the Main use of this Switch is same as the FOR /D Switch command, the only difference is that it scans through files (instead of folders) in the current directory and sub-directories too.

E.g.: Just Change /D from the above example code to /R, And see the difference.
For /R %%A in (p*) do (Echo.%%A)

FOR /R Command usage
All Files in sub-folders, 'starting with the letter P'

Video Demo of FOR Command Switches

As, Always - A Video Demo is much more powerful than a Bulky Explanation. And, A Demo is a Great way to express the importance of this For Command and its switches. You'll know a few of my thoughts about this command in this video. So, be prepared to get affected by those crazy thoughts.

From the Editor's Desk

If you still have any doubt or You think, this article was too basic. Please, Go through other TheBATeam Projects. You'll definitely feel amazed. And, You are welcome to appreciate TheBATeam's efforts in the comments. We won't charge any money for true appreciation. ☺️

And, For a more Deep understanding of these Topics, we highly recommend to Read the official TheBATeam's Book on this Topic, named as "Batch Programming Decoded". You can also submit your own custom batch projects that you want to share with the world here. Thanks for your Faith and Time.

No comments:

Powered by Blogger.