Read Mode
Default

The FOR Command - Part III | Advanced CMD Command Options & Usage | Learn Batch Programming - Part III

The for loop in CMD is considered as one of the most complicated and complex among cmd commands. it's not my personal view but, I have faced this issue for a long time. and finally, when I understood how it works, I created a custom tutorial especially to help other batch programmers with for command. and, in this article - we are going to learn about the special options and parameters that you can use with for command in CMD. I hope these cmd trick will be useful for you, even if you are not a beginner in batch.
GUI's are made FOR THE IGNORANT PEOPLE,
BY THE IGNORANT PEOPLE to do IGNORANT TASKS with Computer.

-kvc

The FOR Command - Part III | Advanced CMD Command Options & Usage | Learn Batch Programming - Part III

Advanced CMD Command Options & Usage | The FOR Command - Part III

In the PART-I & PART-II about the For Command/LOOP in CMD, we understood the basic nature of CMD commands. and, we learned about the special parameters related to the for loop/command in CMD. but, learning all that was not enough because the for command in CMD has some special options available for the users/ batch programmers. so, without knowing them and understanding the special options of for command in CMD - no one can become an advanced programmer in batch.

Special "For /F" Switch - Command Prompt secrets

So, in this Part-III of For Loop - We will mainly focus on the amazing For /F switch, which allows incredible power in the hands of a batch programmer. There are many ways in which you can use this switch to do the impossible task with CMD.  understanding /f switch of for command is the first step towards being an advanced batch programmer. Let's Have a Look at some FOR /f Examples.

Without Special Options

In The FOR Command - Part II - FOR Loop Switches | Learn Batch Programming - Part III, We talked about Using For Command Switches in CMD. So, I am going to use the same example again. So, it will save a lot of my time and your time too.
 For /F %%A in ('tasklist') do (Echo.%%A && Timeout /t 1 >nul)

For /f Examples - Batch for Loop Example

For /F is very special as it allows you to use Extra options to enhance the command's Effect. The general syntax is as Follows:

For /F "OPTIONS" %%A in ('tasklist') do (Echo.%%A && Timeout /t 1 >nul)

In the OPTIONS, we can use various keywords and they affect the output very effectively. Here, I am only going to talk about these keywords. And, You can watch a Working batch file examples demo of them in the Video Embedded Just below the explanation. Hope, You'll like it.
  1. EOL=C:
    It Stands for End Of Line, and this option is very useful when you are reading files using For Loop in CMD. And, you want to comment out some special lines in the files. You can select a Character, And, if CMD sees this character in any line of the file - it will not read that specific line after that Specially Selected Character.
    I personally use, # or ; to Make comments. And, this option helps in reading the custom script files, that you can read using this special option. (Replace C with the character you want)

    For /F "EOL=;" %%A in ('tasklist') do (Echo.%%A && Timeout /t 1 >nul)

  2. Skip=N
    This option is used to Skip a Specific amount of initial lines, while reading a Text file/Commands output. And, again it is very useful if you are making your own custom scripts and reading specific files using the For Command in CMD.

    For /F "EOL=; skip=5" %%A in ('tasklist') do (Echo.%%A && Timeout /t 1 >nul)

    The above command will not read any line starting with the character ; (Semi-colon) and Won't read first 5 lines from the Output.

  3. Tokens=x,y,m-n
    One of the important to understand For Command option. It helps the Batch programmer to break the Output String into various parts, depending on the Requirement of the Program. And, later on you can use these parts of the String to do various Tasks and actions using the program.

    For /F "EOL=; skip=5 tokens=1,2,3" %%A in ('tasklist') do (Echo.%%A && Timeout /t 1 >nul)

    The Above command will help in breaking the output of the command in 3 parts, and as we are only using %%A, only the first part of the string will be printed on the console. To access the other parts - you can use the next alphabets in the character-set as %%B, %%C. Please watch the video tutorial to understand more about it. I do most of my cmd tricks using this functionality in Batch.

  4. Delims=xxx
    This is mostly combined with the Tokens option (explained above). By default, CMD will break the given string output from The Space or TABs. But, there can be scenarios when we need it to break the srting from a special character. Then, we need to use this option combined with the Tokens.

    For /F "EOL=; skip=5 tokens=1,2,3 delims=," %%A in ('tasklist') do (Echo.%%A && Timeout /t 1 >nul)

    In the above example, CMD will not read any line starting from a Semi-colon (;), It will skip reading the first 5 lines, and it will break the output string into 3 parts. But, this time it won't break it from spaces, it will break it from Commas (,). But only prints the first part of the string on console.

  5. usebackq
    I think, this can be understood better from the video tutorial. I don't want to confuse you by telling you something that I can't completely explain myself in words. So, Jump to the next section of the article. The video will be better in explaining that.

Command Prompt Secrets - The Video Demo

There are many other Secret aspects of cmd hacks, that you can perform with the For Command of CMD. What is more better than a Self-Speaking video Demo. So, without wasting any time - let's have a look at it.


From The Editor's Desk

So, I think, those were enough batch file examples for you to understand about the FOR LOOP in cmd. If you still have your doubts or suggestions, you are free to contact us from the comments field below. or you can join the Discord Chat to discuss more. If you have built your own batch project, and you want o to share it with the world with your name, You can submit it via this link.

Thanks for your Faith and Time in TheBATeam. We hope to keep the flow of information flowing. See you soon with another article.

No comments:

Powered by Blogger.