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

Author Topic: For loop string processing  (Read 4336 times)

0 Members and 1 Guest are viewing this topic.

remstadt

    Topic Starter


    Greenhorn

    • Experience: Experienced
    • OS: Windows 7
    For loop string processing
    « on: March 31, 2014, 12:08:25 PM »
    Hi! I have a for loop in a *.bat file that looks something like:

    Code: [Select]
    SET ENVVARLIST=env-vars-list.txt
    FOR /F "tokens=1,* delims==" %%A IN (%ENVVARLIST%) DO (
        SETX %%A "%%B"
    )

    and env-vars-list.txt looks something like:
    Code: [Select]
    LIB_ROOT=%~dp0%mylib
    it basically has a bunch of environment variables with their paths
    The For loop doesn't seem to process the %~dp0%
    if I put the command in the For loop it works, but since the environment variables all have different paths it's a bit inconvienient.

    Does anyone have a fix for this?

    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: For loop string processing
    « Reply #1 on: March 31, 2014, 12:36:53 PM »
    Well for starters your SETX command has no equals sign.  It is removed from your text file in the FOR command because you specified it as a delimiter.

    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: For loop string processing
    « Reply #2 on: March 31, 2014, 12:40:45 PM »
    This should solve your expansion problem
    Code: [Select]
    @echo off
    SET ENVVARLIST=env-vars-list.txt
    FOR /F "tokens=1,* delims==" %%A IN (%ENVVARLIST%) DO (
        call SETx %%A="%%B"
    )

    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: For loop string processing
    « Reply #3 on: March 31, 2014, 12:43:54 PM »
    But why bother using the equals sign as a delimiter.  Use it to your advantage.
    Code: [Select]
    @echo off
    SET ENVVARLIST=env-vars-list.txt
    FOR /F "delims=" %%A IN (%ENVVARLIST%) DO (
        call SETX %%A
    )

    remstadt

      Topic Starter


      Greenhorn

      • Experience: Experienced
      • OS: Windows 7
      Re: For loop string processing
      « Reply #4 on: March 31, 2014, 01:00:27 PM »
      Thank you for the reply!
      There is a very subtle syntax difference between SET and SETX. Set uses an =, but SETX does not use one. I believe that part of my syntax was correct, but I may be missing the interpretation of your comment...

      How do you get the command to parse the %~dp0% when it is read from an external file? when it reads it from the file it doesn't convert it to the current drive and path information... it's treated as a string literal in the environment table.

      i.e. I'm hoping that %~dp% gets converted to something like:
      C:\Users\MyName\Documents\Visual Studio xxx\Projects\


      remstadt

        Topic Starter


        Greenhorn

        • Experience: Experienced
        • OS: Windows 7
        Re: For loop string processing
        « Reply #5 on: March 31, 2014, 01:05:03 PM »
        testing it with the Call command.... hope that works

        remstadt

          Topic Starter


          Greenhorn

          • Experience: Experienced
          • OS: Windows 7
          Re: For loop string processing
          « Reply #6 on: March 31, 2014, 01:08:42 PM »
          No use of the call command didn't help with the parsing. Any other suggestions?

          Squashman



            Specialist
          • Thanked: 134
          • Experience: Experienced
          • OS: Other
          Re: For loop string processing
          « Reply #7 on: March 31, 2014, 01:18:36 PM »
          No use of the call command didn't help with the parsing. Any other suggestions?
          Let me see your code.  It has to work.  The %%A and %%B are expanded first even before the call statement run.  The CALL statement then starts its own environment and expands the %dp0 variable.

          Your code should look like this.
          Code: [Select]
          @echo off
          SET ENVVARLIST=env-vars-list.txt
          FOR /F "tokens=1,* delims==" %%A IN (%ENVVARLIST%) DO (
              call SETX %%A "%%B"
          )

          But for testing purposes I am using this.
          Code: [Select]
          @echo off
          SET ENVVARLIST=env-vars-list.txt
          FOR /F "tokens=1,* delims==" %%A IN (%ENVVARLIST%) DO (
              call SET %%A=%%B
              set lib_
          )

          Squashman



            Specialist
          • Thanked: 134
          • Experience: Experienced
          • OS: Other
          Re: For loop string processing
          « Reply #8 on: March 31, 2014, 01:23:50 PM »
          When I run it with SETX this is the output I get.
          Code: [Select]
          SUCCESS: Specified value was saved.

          Squashman



            Specialist
          • Thanked: 134
          • Experience: Experienced
          • OS: Other
          Re: For loop string processing
          « Reply #9 on: March 31, 2014, 01:25:02 PM »
          And now when I open up a cmd prompt it is now a static environmental variable.
          Code: [Select]
          H:\>set lib_
          LIB_ROOT=C:\BatchFiles\environ\mylib

          Squashman



            Specialist
          • Thanked: 134
          • Experience: Experienced
          • OS: Other
          Re: For loop string processing
          « Reply #10 on: March 31, 2014, 01:29:47 PM »
          And you should also be aware of this if you are trying to use that new variable in your current batch file.
          From the SETX help file
          Quote
          NOTE: 1) SETX writes variables to the master environment in the registry.

                2) On a local system, variables created or modified by this tool
                   will be available in future command windows but not in the
                   current CMD.exe command window.

          remstadt

            Topic Starter


            Greenhorn

            • Experience: Experienced
            • OS: Windows 7
            Re: For loop string processing
            « Reply #11 on: March 31, 2014, 01:39:30 PM »
            Hah! you were right all along, I just needed to open a new window. Thanks!