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

Author Topic: [SOLVED] Question about FOR  (Read 12103 times)

0 Members and 1 Guest are viewing this topic.

Two-eyes

    Topic Starter


    Intermediate
  • Thanked: 4
    [SOLVED] Question about FOR
    « on: September 12, 2009, 02:31:19 PM »
    Man, I've never seen a complicated for command...

    See if I understood it correctly, please:

    1)
    Code: [Select]
    FOR /F %%A IN (somefile) DO command
    Does this:
    - take a LINE from somefile and put it in the variable %%A
    -perform commands on %%A

    2)
    Code: [Select]
    FOR /F %%A IN ("somestring") DO command
    Does this:
    - take somestring and put it in %%A
    -perform commands on %%A

    3)
    Code: [Select]
    FOR /F %%A IN(aCommand) DO command
    Does this:
    - take a line from the output of the command and put it in %%A
    -perform commands on %%A

    4)
    Code: [Select]
    FOR /F "delims=[char/s]" %%A IN (somestring or somefile or aCommand) DO command
    Does this:
    -break up somestring or the line from somefile or the output from aCommand according to [char/s]
    - take a broken up part and put it in %%A
    -perform commands on %%A
    QUESTION does a broken up part continue past the end of a line?

    I understand eol, skip and usebackq, but
    QUESTION What does the tokens option do?

    5)
    Code: [Select]
    FOR /L %%A IN (x y z) DO command
    Does this:
    -assign x to %%A
    -do the command
    -increment %%A by y
    -if %%A equals z stop, else do the command
    -in simple works, a loop

    6)
    Code: [Select]
    FOR /R [path] %%A IN (set) DO command
    Sorry, I don't understand this at all

    7)
    Code: [Select]
    FOR /D IN (set) DO command
    If I write /? it says: If set contains wildcards, then specifies to match against directory names instead of file names.  So what happens if it doesn't?

    //can't write 8 ) cos it is 8)...so in this forum, lists should be less than 8 or using letters/roman number :P
    9)
    Code: [Select]
    FOR %%A IN (set) DO command
    Don't understand it because:
    ALSO, I don't understand what could be in 'set' for /R, /D and this last one.

    Please correct any mistakes, and answer QUESTIONs, ALSOs and PSs

    Thank you very VERY much
    Two-Eyes

    PS. Can FORs be nested?
    « Last Edit: September 13, 2009, 05:34:43 AM by Two-eyes »
    Quote
    I believe the bushes in my yard will BURN before God picks up a PC to send a message


    Salmon Trout

    • Guest
    Re: Question about FOR
    « Reply #1 on: September 13, 2009, 03:23:54 AM »
    1) Yes, note that the /F switch means "every line" not "a line".
    2) Yes
    3) Every line of the command output if there are more than one e.g. DIR, TYPE, whatever
    4)

    treat line as a series of tokens split by delim chars (default are space and TAB)
    specify others in delims= (delims block) e.g. "delims=,.:"

    tokens=[specify tokens]

    The first token (the one specified in the FOR line) is the EXPLICIT token.

    There may be up to 25 more IMPLICIT tokens in one FOR structure. (Because there are 26 letters of the alphabet)
    These follow in sequence and are specified by the tokens= part.

    e.g.
    Code: [Select]

    FOR /F "tokens=1-3* delims=," %%A in ("cat,dog,horse,the whole zoo") do (
        echo  token 1: %%A
        echo  token 2: %%B
        echo  token 3: %%C
        echo the rest: %%D
        )
       

    %%A is cat, %%B is dog, %%C is horse, %%D [created because of the asterisk which means "the rest"] is the whole zoo   
       
    Code: [Select]
    token 1: cat
     token 2: dog
     token 3: horse
    the rest: the whole zoo

    5) Yes

    6) Walks the directory tree starting at a top folder, i.e. successively repeats the command for every file specified in (set) in that folder and every sub(and sub-sub, sub-sub-sub, etc) folder in turn

    This code echoes the path and filename of every mp3 file in C:\Myfolder and all subdirectories

    Code: [Select]
    FOR /R "C:\MyFolder" %%A in (*.mp3) do (
        echo %%A
        )

    7) lists directories.

    for /D %%A in (C:\) do echo %%A echoes C:\
    for /D %%A in (C:\*) do echo %%A echoes  all next level sub directories
    for /D %%A in (C:\B*) do echo %%A echoes  all next level sub directories whose name starts with B

    can't write 8 )

    use different symbol like 8: or 8] or 8. 

    9) set can be a wildcard or a filename

    Note quotes; optional if %%A never has spaces, compulsory if there are spaces

    Code: [Select]
    for %%A in (*.bat) do (
        copy "%%A" D:\NewFolder
        )

    Quote
    PS. Can FORs be nested?

    Yes, but you cannot have more than a certain number of variables active. FOR /? help says max is 52 (A-Z, a-z) but in fact you can use 1-9 and a few other characters as well. I think it is 63 or 64 but beyond 52 is undocumented.
       
       












    Two-eyes

      Topic Starter


      Intermediate
    • Thanked: 4
      Re: Question about FOR
      « Reply #2 on: September 13, 2009, 04:16:18 AM »
      Great, thanks VERY much :).

      For number 4): [for other people searching]
      if I said
      Code: [Select]
      FOR /F "tokens=1,3 delims=," %%A IN ("cat,dog,horse,the rest") DO(
       ECHO %%A
       ECHO %%B

      Output is:
      Code: [Select]
      cat
      horse

      For number 7)
      So it does actions on directories instead of files.

      Thank you VERY much

      Two-eyes

      PS:
      Quote
      PS. Can FORs be nested?

      Yes, but you cannot have more than a certain number of variables active. FOR /? help says max is 52 (A-Z, a-z) but in fact you can use 1-9 and a few other characters as well. I think it is 63 or 64 but beyond 52 is undocumented.

      I wouldnt want to debug that program

      Quote
      can't write 8 )

      use different symbol like 8: or 8] or 8. 
      Wow, you are so............. thorough :P
      It just shows you care   :-* ;)

      Thanks again,
      Quote
      I believe the bushes in my yard will BURN before God picks up a PC to send a message


      Salmon Trout

      • Guest
      Re: Question about FOR
      « Reply #3 on: September 13, 2009, 04:43:43 AM »
      Quote
      Can FORs be nested?

      Code: [Select]
      @echo off
      for /f "tokens=1-3 delims=:" %%A in ("big:red:wet") do (
      for /f "tokens=1-3 delims=," %%D in ("cat,dog,cow") do (
      for /f "tokens=1-3 delims=-" %%G in ("hot-hit-hat") do (
          echo %%A %%D %%G
          echo %%A %%E %%G
          echo %%A %%F %%G
          echo %%B %%D %%G
          echo %%B %%E %%G
          echo %%B %%F %%G
          echo %%C %%D %%G
          echo %%C %%E %%G
          echo %%C %%F %%G
              echo %%A %%D %%H
          echo %%A %%E %%H
          echo %%A %%F %%H
          echo %%B %%D %%H
          echo %%B %%E %%H
          echo %%B %%F %%H
          echo %%C %%D %%H
          echo %%C %%E %%H
          echo %%C %%F %%H
          echo %%A %%D %%I
          echo %%A %%E %%I
          echo %%A %%F %%I
          echo %%B %%D %%I
          echo %%B %%E %%I
          echo %%B %%F %%I
          echo %%C %%D %%I
          echo %%C %%E %%I
          echo %%C %%F %%I
          )
          )
          )
         

      Code: [Select]
      big cat hot
      big dog hot
      big cow hot
      red cat hot
      red dog hot
      red cow hot
      wet cat hot
      wet dog hot
      wet cow hot
      big cat hit
      big dog hit
      big cow hit
      red cat hit
      red dog hit
      red cow hit
      wet cat hit
      wet dog hit
      wet cow hit
      big cat hat
      big dog hat
      big cow hat
      red cat hat
      red dog hat
      red cow hat
      wet cat hat
      wet dog hat
      wet cow hat

      Hours of fun...


      Salmon Trout

      • Guest
      Re: Question about FOR
      « Reply #4 on: September 13, 2009, 04:51:47 AM »
      Quote
      For number 4): [for other people searching]
      if I said

      FOR /F "tokens=1,3 delims=," %%A IN ("cat,dog,horse,the rest") DO(
       ECHO %%A
       ECHO %%B


      Output is:

      cat
      horse

      No, output is

      cat
      dog

      token 1 cat becomes contents of %%A
      token 2 dog becomes contents of %%B (the next letter of the alphabet after A)
      token 3 horse becomes contents of %%C (the next letter of the alphabet after B)

      Two-eyes

        Topic Starter


        Intermediate
      • Thanked: 4
        Re: Question about FOR
        « Reply #5 on: September 13, 2009, 04:57:38 AM »
        Weeeell, that loop didn't have a bug :P.

        No, the answer is cat and horse.  Notice that the tokens are 1 AND 3 not 1 TO 3.

        T-E
        Quote
        I believe the bushes in my yard will BURN before God picks up a PC to send a message


        Salmon Trout

        • Guest
        Re: Question about FOR
        « Reply #6 on: September 13, 2009, 05:01:46 AM »
        Weeeell, that loop didn't have a bug :P.

        No, the answer is cat and horse.  Notice that the tokens are 1 AND 3 not 1 TO 3.

        T-E

        You're right. I should clean my spectacles!

        tokens=1,3 means tokens 1 AND 3.
        tokens=1,2,3 and tokens 1-3 mean the same thing
        tokens=1-3,5 mean tokens 1 to 3 and token 5

        Two-eyes

          Topic Starter


          Intermediate
        • Thanked: 4
          Re: Question about FOR
          « Reply #7 on: September 13, 2009, 05:09:27 AM »
          Quote
          I should clean my spectacles!
          I am familiar to that problem, bro.

          Now, I know this is off-topic but,
          [caps on] Why isn't it described that easily in Computer hope's MS-DOS section[/caps on]
          I had to experiment and search a lot before I got it.

          I'm simply suggesting an improvement.

          er...where can I do this, ie is there a forum or e-mail or something like that?

          Two-eyes
          Quote
          I believe the bushes in my yard will BURN before God picks up a PC to send a message


          Salmon Trout

          • Guest
          Re: Question about FOR
          « Reply #8 on: September 13, 2009, 05:29:45 AM »
          Computer Hope is not intended to be the only source of help and information for people. There are many many web sites and forums where this topic is already covered.

          Here are a few links

          http://technet.microsoft.com/en-us/library/bb490909.aspx

          http://www.robvanderwoude.com/variableexpansion.php

          http://groups.google.co.uk/group/alt.msdos.batch.nt/browse_frm/thread/c05f88bc3fe14be6/7a467eebaca21f99?hl=en#7a467eebaca21f99

          A good place to look is here

          http://groups.google.co.uk/group/alt.msdos.batch.nt/topics?hl=en


          Two-eyes

            Topic Starter


            Intermediate
          • Thanked: 4
            Re: Question about FOR
            « Reply #9 on: September 13, 2009, 05:34:23 AM »
            Of course.  I'm just saying that it is a little complex to understand at first.  Just a side thought.
            Quote
            I believe the bushes in my yard will BURN before God picks up a PC to send a message


            Salmon Trout

            • Guest
            Re: Question about FOR
            « Reply #10 on: September 13, 2009, 05:47:16 AM »
            Of course.  I'm just saying that it is a little complex to understand at first.  Just a side thought.

            I really do agree that since FOR is the engine of so many batch scripts, that a section of Computerhope dealing with its syntax and some of the things that can (and cannot!) be done with it would be a very good idea. In particular,  the topic of delayed variable expansion is one that keeps appearing in the forum. ("Why can't I set variables in a loop?")

            ValerieMay



              Beginner

              Thanked: 5
              Re: [SOLVED] Question about FOR
              « Reply #11 on: September 13, 2009, 04:37:27 PM »
              Quote from: Salmon Trout
              There may be up to 25 more IMPLICIT tokens in one FOR structure. (Because there are 26 letters of the alphabet)

              Although not documented by MS, 31 tokens can be used and a total in excess of 65 may be open at any time. 

              Most of the printable ASCII chars can be used although some must be Escaped (e.g. ^<) when used.  I've not been able to get % or ~ to perform escaped or not.

              Short example:
              Code: [Select]
              @echo off
              cls
              setlocal

              set data= Able Baker Charlie

              for /f "tokens=1-3" %%: in ("%data%") do (
                  echo %%: %%; %%^<
                  echo.
                  echo %%^< %%; %%:
                  echo.
                  echo %%: %%^< %%;
              )


              Salmon Trout

              • Guest
              Re: [SOLVED] Question about FOR
              « Reply #12 on: September 14, 2009, 12:24:06 AM »
              Although not documented by MS, 31 tokens can be used and a total in excess of 65 may be open at any time. 

              As I noted,

              Quote from: Me
              FOR /? help says max is 52 (A-Z, a-z) but in fact you can use 1-9 and a few other characters as well. I think it is 63 or 64 but beyond 52 is undocumented.   

              However, (Opinions may differ) I'd be wary of using these "features" in anything approaching production code, partly out of long habit, and partly out of fear that MS might do something to break code that relies on undocumented behaviour of cmd.exe.



              Salmon Trout

              • Guest
              Re: [SOLVED] Question about FOR
              « Reply #13 on: September 16, 2009, 01:14:41 PM »
              Quote from: Two-eyes
              can't write 8 )

              When replying,

              1. Click "Additional Options..." (blue link, below "Notify me of replies")
              2. Check "Don't use smileys"
              3. Now you can type 8)

              Two-eyes

                Topic Starter


                Intermediate
              • Thanked: 4
                Re: [SOLVED] Question about FOR
                « Reply #14 on: September 16, 2009, 01:19:41 PM »
                Thanks....you're very efficient :) :P ...but also a little of an  ::)

                Two-Eyes %
                Quote
                I believe the bushes in my yard will BURN before God picks up a PC to send a message