Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch script for/if not looping  (Read 7320 times)

0 Members and 1 Guest are viewing this topic.

Valerie

    Topic Starter


    Rookie

    Batch script for/if not looping
    « on: May 15, 2010, 08:00:06 PM »
    Win XP SP.3

    Why does the For loop in the following script not loop please?

    Code: [Select]
    @echo off
    setlocal enabledelayedexpansion
    cls

    set year=9
    set month=8
    set day=25
    set cnt=1

    for /f "tokens=%cnt%" %%1 in ("year month day") do (
        if !%%1! lss 10 set %%1=0!%%1!
            set /a cnt+=1
    )
    echo !year!:!month!:!day!
    exit /b

    Helpmeh



      Guru

    • Roar.
    • Thanked: 123
      • Yes
      • Yes
    • Computer: Specs
    • Experience: Familiar
    • OS: Windows 8
    Re: Batch script for/if not looping
    « Reply #1 on: May 15, 2010, 08:05:29 PM »
    Because there is only one line being fed into the for loop. It does the action(s) for each line it is fed.
    Where's MagicSpeed?
    Quote from: 'matt'
    He's playing a game called IRL. Great graphics, *censored* gameplay.

    Salmon Trout

    • Guest
    Re: Batch script for/if not looping
    « Reply #2 on: May 16, 2010, 12:46:04 AM »
    See the lines in red

    @echo off
    setlocal enabledelayedexpansion
    cls

    set year=9
    set month=8
    set day=25
    set cnt=1

    :loop
    for /f "tokens=%cnt%" %%1 in ("year month day") do (
       if !%%1! lss 10 set %%1=0!%%1!
       set /a cnt+=1
    )
    if %cnt% LSS 4 goto loop
    echo !year!:!month!:!day!







    Valerie

      Topic Starter


      Rookie

      Re: Batch script for/if not looping
      « Reply #3 on: May 16, 2010, 01:53:12 AM »
      Thank you both.

      V.