-->

  • FOR Loop in Batch Scripting with Examples

     

    FOR Loop in Batch Scripting with Examples


    FOR Loop in Batch Scripting with Examples


    When working with Batch Scripts, you may need to repeat the same task multiple times. Instead of writing the same command again and again, you can use a **FOR Loop**.

    The FOR Loop helps automate repetitive tasks such as displaying values, creating folders, processing files, and running commands multiple times.

    In this tutorial, we'll learn the basics of the FOR Loop using simple examples.



    What is a FOR Loop?


    A FOR Loop executes a block of code repeatedly for each item in a list.

    Syntax


    for %%variable in (list) do (
        command
    )


    Where:
     

    • `%%variable` stores the current value.
    • `list` contains the items to process.
    • `do` specifies the command to execute.



    Example 1: Display Numbers Using FOR Loop


    @echo off
    setlocal
    for %%i in (1 2 3 4 5) do (
        echo %%i
    )


    Output

    1
    2
    3
    4
    5



    Explanation


    In this example:

    • `%%i` acts as the loop variable.
    • The loop processes each value one by one.
    • The `echo` command displays the current value.



    Execution Flow:


    Iteration 1 → 1
    Iteration 2 → 2
    Iteration 3 → 3
    Iteration 4 → 4
    Iteration 5 → 5



    Example 2: Create Multiple Folders


    @echo off
    setlocal
    for %%i in (1 2 3 4 5) do (
        mkdir Folder-atul-%%i
    )

     

    Result


    The script creates the following folders:


    Folder-atul-1
    Folder-atul-2
    Folder-atul-3
    Folder-atul-4
    Folder-atul-5


     

    FOR Loop in Batch Scripting with Examples

     

     

     

    Explanation

    For each number:

    * The loop runs once.
    * `mkdir` creates a new folder.
    * `%%i` is appended to the folder name.

    This saves time compared to manually creating multiple folders.



    Why Use FOR Loops?


    FOR Loops are useful when:

    • * Creating multiple folders
    • * Renaming files
    • * Processing log files
    • * Running repetitive commands
    • * Automating administrative tasks




    Instead of writing:


    mkdir Folder1
    mkdir Folder2
    mkdir Folder3
    mkdir Folder4
    mkdir Folder5


    you can use a single FOR Loop.


    Real-World Uses for System Engineers

    System Engineers commonly use FOR Loops for:

    • Bulk folder creation
    • File management
    • Log collection
    • Backup automation
    • Software deployment tasks
    • User account processing



    FOR Loops are one of the most frequently used automation tools in Batch Scripting.





    Interview Questions


    What is a FOR Loop?
    A FOR Loop is used to repeat a command for each item in a list.


    What does %%i represent?
    It is the loop variable that stores the current value during each iteration.


    Why are FOR Loops useful?
    They reduce repetitive code and automate repetitive tasks.


    Which command is used to create folders?
    mkdir


    Can FOR Loops be used in automation scripts?
    Yes. They are widely used in system administration and automation tasks.



    Conclusion


    The FOR Loop is one of the most powerful features of Batch Scripting. It allows you to repeat tasks efficiently and automate routine operations. Whether you're displaying values, creating folders, or processing files, mastering FOR Loops will make your scripts cleaner and more effective.

    As a future System Engineer or SecDevOps professional, understanding loops is an important step toward automation and scripting mastery.
     

     

     

     

  • 0 comments:

    Post a Comment

    For Any Tech Updates, Hacking News, Internet, Computer, Technology and related to IT Field Articles Follow Our Blog.