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

Author Topic: Parse two string lists  (Read 3034 times)

0 Members and 1 Guest are viewing this topic.

cutemartian

    Topic Starter


    Starter

    Parse two string lists
    « on: December 01, 2009, 12:25:54 AM »
    Hi there,

    I have a problem that I can't seem to be able to resolve.  The batch file that I am working on expects to get 4 parameters, two of which are lists of strings:

    IPADDRESS=172.16.0.1
    SERVICENAME=Service1,Service2
    BINPATH="C:\Programs Files\Company Name\Product Name\bin.exe"
    DISPLAYNAME="DisplayName 1","DisplayName 2""

    I need to call the following command line in a for loop, such that on the first iteration it would use the first string in the comma delimited list in the SERVICENAME and the first string in the comma delimited list in the DISPLAYNAME string list.

    First iteration:

    sc \\172.16.0.1 create "Service1" binPath= "C:\Programs Files\Company Name\Product Name\bin.exe" DisplayName= "Display Name 1" obj= "NT Authority\Network Service" start= demand

    Second iteration:
    sc \\172.16.0.1 create "Service2" binPath= "C:\Programs Files\Company Name\Product Name\bin.exe" DisplayName= "Display Name 2" obj= "NT Authority\Network Service" start= demand

    Here is my sample script that does not quite work the way I want it to:

    FOR %%a in (%SERVICENAME%) do (FOR %%c in (%DISPLAYNAME%) do (
       FOR /F "tokens=1 delims=," %%b in ("%%a") do ( set srvnm=%%b & FOR /F "tokens=1 delims=," %%d in ("%%c") do (echo %%b %%d))))

    This displays:

    Service1 "Display Name 1"
    Service1 "Display Name 2"
    Service2 "Display Name 1"
    Service2 "Display Name 2"

    I also have tried the following, but it doesn't work:

    FOR %%a in (%DISPLAYNAME%) do (
       FOR /F "tokens=1 delims=," %%b in ("%%a") do (echo %srvnm% %%b))

    Service2 "Display Name 1"
    Service2 "Display Name 2"


    Thank you in advance.
    Natalia

    Salmon Trout

    • Guest
    Re: Parse two string lists
    « Reply #1 on: December 01, 2009, 02:04:42 AM »
    Because you can use an arithmetic (number) variable for the tokens= value, you can step through the delimited tokens by incrementing the variable with set /a in a loop

    Code: [Select]
    @echo off
    set SERVICENAME=Service1,Service2,Service3,Service4,Service5,Service6
    set DISPLAYNAME="DisplayName 1","DisplayName 2","DisplayName 3","DisplayName 4","DisplayName 5","DisplayName 6"

    REM add end markers
    set mySERVICENAME=%SERVICENAME%,eof
    set myDISPLAYNAME=%DISPLAYNAME%,eof

    REM Temporarily remove quotes around "Displayname N"
    set myDISPLAYNAME=%myDISPLAYNAME:"=%

    set iter=1

    :loop

                    for /f "tokens=%iter% delims=," %%A in ("%mySERVICENAME%") do set var1=%%A
                    if %var1%==eof goto done
                    for /f "tokens=%iter% delims=," %%B in ("%myDISPLAYNAME%") do set var2="%%B"
                    if %var2%=="eof" goto done

                    echo [Iteration=%iter%] %var1% %var2%
                   
                    set /a iter+=1
                    REM alternative format
                    REM set /a iter=%iter%+1

    goto loop

    :done

    echo.
    echo Finished

    output

    Code: [Select]
    [Iteration=1] Service1 "DisplayName 1"
    [Iteration=2] Service2 "DisplayName 2"
    [Iteration=3] Service3 "DisplayName 3"
    [Iteration=4] Service4 "DisplayName 4"
    [Iteration=5] Service5 "DisplayName 5"
    [Iteration=6] Service6 "DisplayName 6"
    « Last Edit: December 02, 2009, 12:30:29 AM by Salmon Trout »

    Salmon Trout

    • Guest
    Re: Parse two string lists
    « Reply #2 on: December 01, 2009, 03:40:45 PM »
    How are you passing the 4 parameters?

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Parse two string lists
    « Reply #3 on: December 01, 2009, 03:59:36 PM »
    How are you passing the 4 parameters?

    Look at his post.
    The parameters have to be done as literal items, on the command line. With quotation makes, with commas. Exactly as he posted.
    This is a candidate for a shell script or a VBS or something more that just pure batch commands.   :-\
    BTW, Nothing was said about error recovery. If he has more than a four or five servers, it is going to be a mess when an error happens. And the do happen.

    Salmon Trout

    • Guest
    Re: Parse two string lists
    « Reply #4 on: December 02, 2009, 12:15:52 AM »
    Look at his post.
    The parameters have to be done as literal items, on the command line. With quotation makes, with commas. Exactly as he posted.

    That was what I was hoping. Incidentally "he" signs "his" post "Natalia", so I think I may have read it a bit more carefully than you did ;)

    cutemartian

      Topic Starter


      Starter

      Re: Parse two string lists
      « Reply #5 on: December 02, 2009, 12:19:25 AM »
      Thank your for your solution, Salmon. :-)   Let me give it a try.  To answer your question, the 4 parameters are being passed from an executable that invokes the script as command line argument to the script. (%1, %2, %3, %4, %0 is reserved to indicate the test mode operation.)

      To answer Geek-9pm's concern about error handling, it was omitted intentionally in order to clearly state the problem space.  The error handling does exist in the script. :-)

      Thanks,
      Natalia

      cutemartian

        Topic Starter


        Starter

        Re: Parse two string lists
        « Reply #6 on: December 02, 2009, 12:22:10 AM »
        LOL, on the "his" comment.  Got me laughing :-)

        Salmon Trout

        • Guest
        Re: Parse two string lists
        « Reply #7 on: December 02, 2009, 01:08:23 AM »
        I am reminded of the old story about the Englishman travelling in Ireland who is unsure how to get to a certain town. He asks an elderly local who says "Ah sure, begorrah, if I wanted to get to there, I wouldn't be startin' from here!".

        Natalia, if you look through this forum, you will see many threads in which somebody asks for a batch solution to a problem. An answer is given, but also people jump in saying "Don't bother with batch, use Visual Basic Script/Perl/Python/Awk/GNU grep/sed, (or whatever)", because those methods are somehow "better" or more "elegant" or "efficient". This may be so, but I think what sometimes escapes these code warriors is that in many cases the person asking the question is not requesting a code snippet that solves a problem, text-book fashion, or a lesson in some other scripting language, but rather a solution is desired to enable them to finish a larger batch script that they have already spent some time and effort writing. Often other solutions are impossible because of restrictions on third party installations and/or use of VBScript.

        OK the sermon is over. Natalia, I notice that in your comma separated lists, that "DisplayName N" is quoted, but ServiceN is not. I removed the quotes from DisplayName, and put them back again, because of a problem I had (XP SP3) with a strange error message to do with oddly named files not being found if the variable was used with quotes as the dataset in a FOR command.