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

Author Topic: How to modify this for loop:  (Read 7733 times)

0 Members and 1 Guest are viewing this topic.

PPowerHouseK

  • Guest
How to modify this for loop:
« on: October 12, 2009, 08:24:49 PM »
Hello all,

I am working on a batch file. I need to read the contents of a text file and read every other line and set it as a different variable. For instance if the text file read:
Code: [Select]
house.map
house.map.zip
house2.map
house2.map.zip

then it would set the variable %map% = house.map, %map2% = house2.map

Here is what i have so far:
Code: [Select]
for /f "delims=" %%a in (list.txt) do (
  echo %%a
  set map=%%a
)
This only sets the last line as the variable which is not what I need to accomplish. Any help is much appreciated.

gh0std0g74



    Apprentice

    Thanked: 37
    Re: How to modify this for loop:
    « Reply #1 on: October 12, 2009, 08:50:29 PM »
    in the end , what are you going to do with the input file? describe properly your output.

    PPowerHouseK

    • Guest
    Re: How to modify this for loop:
    « Reply #2 on: October 12, 2009, 09:01:00 PM »
    I'm not real sure what your asking, but what I plan to do is read lines 1,3,5,7,9 from the text, and set them to variables , map map2 map3 map4 and map5 . Then I plan to delete the the text file or input. Once that is complete I will probably move the files. The moving and deleting is not the issue tho, its reading every other line and setting the variables, any ideas?

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: How to modify this for loop:
    « Reply #3 on: October 12, 2009, 09:11:23 PM »
    PPowerHouseK,
    You should provide some sample input text and some desired output,

    The FOR /F can read up to 26 lines and assign then A thru Z if you wish

    There is a practical limit on how many variables you can have at one time.
    You may need to think this over a bit.

    Helpmeh



      Guru

    • Roar.
    • Thanked: 123
      • Yes
      • Yes
    • Computer: Specs
    • Experience: Familiar
    • OS: Windows 8
    Re: How to modify this for loop:
    « Reply #4 on: October 12, 2009, 09:19:00 PM »
    PPowerHouseK,
    You should provide some sample input text and some desired output,

    The FOR /F can read up to 26 lines and assign then A thru Z if you wish

    There is a practical limit on how many variables you can have at one time.
    You may need to think this over a bit.
    Can it not read 52? Upper case and lower case
    Where's MagicSpeed?
    Quote from: 'matt'
    He's playing a game called IRL. Great graphics, *censored* gameplay.

    PPowerHouseK

    • Guest
    Re: How to modify this for loop:
    « Reply #5 on: October 12, 2009, 09:27:10 PM »
    You should provide some sample input text and some desired output.
    Here is a sample of the input text:
    Code: [Select]
    house.map
    house.map.zip
    house2.map
    house2.map.zip
    The folder its read from is a map repository that I and others will dump either 1 or more maps into.
    Lets say for times sake the that Ill cap it off at 10 variables, or 10 maps.
    The output should be  all the odd numbered files loaded into corresponding variables.
    That's All it has to do for now. Nothing else.

    Salmon Trout

    • Guest
    Re: How to modify this for loop:
    « Reply #6 on: October 13, 2009, 09:56:39 AM »
    Can it not read 52? Upper case and lower case

    Officially a single FOR /F loop can have up to 26 variables at once (either a-z or A-Z). In fact there are some other undocumented symbols bringing the total to about 32 I think. One loop cannot have a mixture of upper and lower case.


    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: How to modify this for loop:
    « Reply #7 on: October 13, 2009, 11:34:23 AM »
    Officially a single FOR /F loop can have up to 26 variables at once (either a-z or A-Z). In fact there are some other undocumented symbols bringing the total to about 32 I think. One loop cannot have a mixture of upper and lower case.
    We would thank you  if you would tell use the secret symbols.

    As to the original topic, Can you read lines from a ZIP file?

    BatchFileCommand



      Hopeful
    • Thanked: 1
      Re: How to modify this for loop:
      « Reply #8 on: October 13, 2009, 04:27:02 PM »
      Well, if you can unzip it, yes. But I'm not sure how command line would, or could do that.
      οτη άβγαλτος μεταφ βαθμολογία

      PPowerHouseK

      • Guest
      Re: How to modify this for loop:
      « Reply #9 on: October 13, 2009, 05:52:36 PM »
      I'm not quite sure how to approach the responses. I have a function that already reads the presence of the zipped file and outputs the file names to a text file named list.txt. I also have another set of functions that completes the necessary distribution of the zipped files. I just need to read from the list.txt where all the listed files have been output by a previous function. This current for loop does just that and even sets the variables, but not every other line and not to different variables. So please if someone knows how to tweak that loop, I would be very glad.

      gh0std0g74



        Apprentice

        Thanked: 37
        Re: How to modify this for loop:
        « Reply #10 on: October 13, 2009, 06:39:14 PM »
        basically to set each line to a different variable, i recommend arrays,BUT DOS cmd.exe lacks this feature so people normally do those delayexpansion stuff which i will leave it to somebody else to tell you how to do. Judging from your input file, if all you need to do is get those *.map files, then another way in batch (cmd.exe) is to use findstr
        Code: [Select]
        type file | findstr /I "*.map"
        get the results using for loop, and do your stuff. Note the above is not tested...

        ValerieMay



          Beginner

          Thanked: 5
          Re: How to modify this for loop:
          « Reply #11 on: October 15, 2009, 08:23:28 PM »
          We would thank you  if you would tell use the secret symbols.

          There are no secrets, all of the printable codes in the ASCII can be used.  These include the lower & upper case letters, numbers and characters such as ! " # $ etc in the decimal range 33 thru' 126 although some must be Escaped.  The restriction on how many can be used in a For loop seems to be caused by the maximum of 31 tokens.

          So:
          Code: [Select]
          @echo off
          cls
          set input=now is the time for all good men.

          for /f "tokens=1-8" %%^< in ("%input%") do (
              echo %%^<
              echo %%=
              echo %%^>
              echo %%?
              echo %%@
              echo %%A
              echo %%B
              echo %%C
          )
          works..


          billrich

          • Guest
          Re: How to modify this for loop:
          « Reply #12 on: October 16, 2009, 07:51:25 AM »

          C:\>type list.txt
          house1.map
          house2.map.zip
          house3.map
          house4.map.zip
          house5.map.zip
          house6.map.zip
          house7.map.zip
          house8.map
          house9.map.zip
          house10.map
          C:\>type map.bat
          Code: [Select]
          @echo off

          setLocal EnableDelayedExpansion
          set /a c=0
          for /f "delims=" %%a in (list.txt) do (
          set /a c+=1

            set map!c!=%%a

          echo map!c! = %%a

          )

          REM echo map!c! = !map1!


          OUTPUT:

          C:\>map.bat
          map1 = house1.map
          map2 = house2.map.zip
          map3 = house3.map
          map4 = house4.map.zip
          map5 = house5.map.zip
          map6 = house6.map.zip
          map7 = house7.map.zip
          map8 = house8.map
          map9 = house9.map.zip
          map10 = house10.map

          C:\>
          « Last Edit: October 16, 2009, 12:09:18 PM by billrich »

          PPowerHouseK

          • Guest
          Re: How to modify this for loop:
          « Reply #13 on: October 16, 2009, 12:22:41 PM »
          Thank you Bill. I had almost given up on this topic.  :'(  As it seemed to branch into another matter.

          That code works perfect, thank you, but i want to ONLY list the .map files as a map and skip the .map.zip
          Therefore, with the input from list.txt as:
          Code: [Select]
          house.map
          house.map.zip
          house2.map
          house2.map.zip
          house3.map
          house3.map.zip

          Would be returned as:
          Code: [Select]
          Map1= house.map
          Map2=house2.map
          Map3=house3.map
          Therefore skipping the .zip files.
          When the function lists the files in a text, it lists alphabetically alternating the .map and .map.zip, beginning with the .map file. This may allow for counting every other one, or perhaps it would be more efficient to exclude the .zip file extension. This is where I am hung.
          Thanks in advance to relevant answers.  ;D

          Salmon Trout

          • Guest
          Re: How to modify this for loop:
          « Reply #14 on: October 16, 2009, 01:07:31 PM »
          alter Billrich's code:

          either

          Code: [Select]
          for /f "delims=" %%a in ( ' type list.txt ^| find /v /i ".zip" ' ) do (   
          OR

          Code: [Select]
          echo %%a | find /i ".zip" || set map!c!=%%a
          ... but not both

          billrich

          • Guest
          Re: How to modify this for loop:
          « Reply #15 on: October 16, 2009, 02:37:02 PM »

          I want to ONLY list the .map files as a map and skip the .map.zip
          When the function lists the files in a text, it lists alphabetically alternating the .map and .map.zip, beginning with the .map file. This may allow for counting every other one, or perhaps it would be more efficient to exclude the .zip file extension.



          C:\>type list2.txt
          house1.map
          house1.map.zip
          house2.map
          house2.map.zip
          house3.map
          house3.map.zip
          house4.map
          house4.map.zip

          C:\>type zip.bat
          Code: [Select]
          @echo off

          findstr /v "zip"  list2.txt


          C:\> zip.bat
          house1.map
          house2.map
          house3.map
          house4.map
          C:\>



          PPowerHouseK

          • Guest
          Re: How to modify this for loop:
          « Reply #16 on: October 16, 2009, 04:18:42 PM »
          Well it now works perfectly. Thanks guys that did just the trick.  ;D
          Special thanks to Bill and Salmon! You guys Rock!
          I used Bill's code with Salmon's first adjustment.
          I suppose this can be marked as solved. 8)

          billrich

          • Guest
          Re: How to modify this for loop:
          « Reply #17 on: October 16, 2009, 05:13:54 PM »
          PPowerHouseK   wrote:

          "When the function lists the files in a text, it lists alphabetically alternating the .map and .map.zip, beginning with the .map file. This may allow for counting every other one, or perhaps it would be more efficient to exclude the .zip file extension. This is where I am hung. "


          See Input below*

          C:\>type zip2.bat

          Code: [Select]
          @echo off

          setLocal EnableDelayedExpansion
          echo. > ziplist.txt
          echo. > finalziplist.txt

          findstr /v "zip"  list2.txt

          findstr /v "zip"  list2.txt >  ziplist.txt

          set /a c=0
          for /f "delims=" %%a in (ziplist.txt) do (
          set /a c+=1

            set map!c!=%%a

          echo map!c! = %%a

          echo map!c! = %%a  >>   finalziplist.txt

          )

          REM echo map!c! = !map1!


          OUTPUT:


          C:\>zip2.bat
          house1.map
          house2.map
          house3.map
          house4.map
          map1 = house1.map
          map2 = house2.map
          map3 = house3.map
          map4 = house4.map
          C:\>type finalziplist.txt

          map1 = house1.map
          map2 = house2.map
          map3 = house3.map
          map4 = house4.map

          C:\>


          *Input:

          C:\>type list2.txt
          house1.map
          house1.map.zip
          house2.map
          house2.map.zip
          house3.map
          house3.map.zip
          house4.map
          house4.map.zip

          C:\>
          « Last Edit: October 16, 2009, 05:34:54 PM by billrich »