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

Author Topic: Increment name and assign variable in a loop: Help  (Read 11971 times)

0 Members and 1 Guest are viewing this topic.

buddyh

    Topic Starter


    Starter

    Increment name and assign variable in a loop: Help
    « on: March 17, 2010, 10:17:48 AM »
    I searched for a solution a lot.  It may be covered elsewhere but I have not found it.
    I am new to this dos/nt/xp scripting at this level so please bear with me.

    OS is Windows XP so I have that advantage but I need a pure batch script solution.

    I have a file with three lines.
    I need to assign a line to a variable that survives the FOR DO or some loop.

    I have three programs to uninstall they each have similar names except they are different by one character and each installation is in a unique directory.

    To uninstall  the uninstall.exe, some keywords, a trail.txt file and the install.log is needed.  I am able to find all three versions of both the uninstall.exe and install.log. The uninstall.exe locations are in one text file and the install.log locations in another text file.

    I was hoping to loop through each text file and assign the string to a variable and then call the corresponding variable in a for loop.  This part should be easy.


    I tried this snippet but it over writes the Variable
    FOR /F "tokens=1*" %%G IN (%myfile%) DO set Line=%%G %%H
    echo %Line%

    So then I tried to auto increment the variable name which fails
    set Line=%1
    FOR /F "tokens=1*" %%G IN (%myfile%) DO (
      echo %%G %%H
      set Line=%%G %%H
      echo Line = %Line%
      set line+=1
      )

    result in:
    The first echo has the correct line displayed through each iteration but the variable echo is blank.

    To sum how can variable be incremented and assigned?
    Tx

    greg



      Intermediate

      Thanked: 7
      Re: Increment name and assign variable in a loop: Help
      « Reply #1 on: March 17, 2010, 11:24:06 AM »

      I have a file with three lines.
      I need to assign a line to a variable that survives the FOR DO or some loop.



      C:\batch>type  bud.bat
      @echo off
      Code: [Select]
      setlocal enabledelayedexpansion
      FOR /F "tokens=1*" %%g in (myfile.txt) do (
        echo %%g %%h
        set Line=%%g %%h
        echo Line = !Line!
         )

      Output:
      C:\batch>bud.bat
      Line one
      Line = Line one
      Line two
      Line = Line two
      Line three
      Line = Line three

      C:\batch>
      Have a Nice Day

      greg



        Intermediate

        Thanked: 7
        Re: Increment name and assign variable in a loop: Help
        « Reply #2 on: March 17, 2010, 11:53:40 AM »

        I have a file with three lines.
        I need to assign a line to a variable that survives the FOR DO or some loop.




        C:\batch>type  bud2.bat
        Code: [Select]
        @echo off
        set /a c=0
        setlocal enabledelayedexpansion
        FOR /F "tokens=1*" %%g in (myfile.txt) do (
          set /a c+=1
        echo %%g %%h
        if !c!==1  ( set Line1=%%g %%h
        echo line1=!Line1!)
        if !c!==2   ( set Line2=%%g %%h
        echo Line2=!Line2!)
        if !c!==3   ( set Line3=%%g %%h
        echo Line3=!Line3!)
           )
        echo out of loop: Line1=%Line1%
        echo out of loop: Line2=%Line2%
        echo out of loop: Line3=%Line3%

        Output:

        C:\batch> bud2.bat
        Line one
        line1=Line one
        Line two
        Line2=Line two
        Line three
        Line3=Line three
        out of loop: Line1=Line one
        out of loop: Line2=Line two
        out of loop: Line3=Line three

        Input:
        C:\batch>type  myfile.txt
        Line one
        Line two
        Line three
        C:\batch>
        « Last Edit: March 17, 2010, 12:09:43 PM by greg »
        Have a Nice Day

        buddyh

          Topic Starter


          Starter

          Re: Increment name and assign variable in a loop: Help
          « Reply #3 on: March 17, 2010, 01:17:55 PM »
          Great,  I believe I can work with this.

          So I need a separate "set" for each line.  And !c! only increments thanks to delayed expansion.

          My next goal will be to make the number of lines and variables based on the value returned from doing a line count on the file.  I'm thinking an IF that checks against the line count that exits the FOR loop when that number is exceeded.

          Let work on on that for a while.
          Thanks

          greg



            Intermediate

            Thanked: 7
            Re: Increment name and assign variable in a loop: Help
            « Reply #4 on: March 17, 2010, 01:37:59 PM »
            So I need a separate "set" for each line.  And !c! only increments thanks to delayed expansion.

            If you need to set each input line to a variable and those variables are available outside of the loop we need a separate variable for each input line.  When we use "Line" as the only variable, then outside of the loop only the last input line is available.

            Or you might call a subroutine from inside the loop?

            There are other better designs.  Maybe Sidewinder will offer a better design.

            Good Luck.

            p.s. Sidewinder is the most skilled code man at CH

            --------------------------------------------

            "CALL a second batch file
            The CALL command will launch a new batch file context along with any
            specified arguments.
            When the end of the second batch file is reached (or if EXIT is used),
            control will return to just after the initial CALL statement."

            reference:

            http://ss64.com/nt/call.html
            « Last Edit: March 17, 2010, 01:54:17 PM by greg »
            Have a Nice Day

            Salmon Trout

            • Guest
            Re: Increment name and assign variable in a loop: Help
            « Reply #5 on: March 17, 2010, 03:42:54 PM »
            p.s. Sidewinder is the most skilled code man at CH

            I thought you were, greg!

            BC_Programmer


              Mastermind
            • Typing is no substitute for thinking.
            • Thanked: 1140
              • Yes
              • Yes
              • BC-Programming.com
            • Certifications: List
            • Computer: Specs
            • Experience: Beginner
            • OS: Windows 11
            Re: Increment name and assign variable in a loop: Help
            « Reply #6 on: March 17, 2010, 04:57:16 PM »
            I thought you were, greg!

            Now now, let's not interrupt ol' Billrich during one of his schizophrenic episodes where he plays the part of multiple people.
            I was trying to dereference Null Pointers before it was cool.

            greg



              Intermediate

              Thanked: 7
              Re: Increment name and assign variable in a loop: Help
              « Reply #7 on: March 17, 2010, 05:40:08 PM »
              I have a file with three lines.
              I need to assign a line to a variable that survives the FOR DO or some loop.

              I hope you discover  the code needed.

              There are several negative, off topic posts  but no new suggestions.

              Lets us know your progress.

              I will work on the subroutine idea and post latter.
              Have a Nice Day

              greg



                Intermediate

                Thanked: 7
                Re: Increment name and assign variable in a loop: Help
                « Reply #8 on: March 17, 2010, 06:35:53 PM »

                I have a file with three lines.
                I need to assign a line to a variable that survives the FOR DO or some loop.

                I have three programs to uninstall they each have similar names except they are different by one character and each installation is in a unique directory.


                C:\batch>type bud3.bat
                Code: [Select]
                @echo off
                setlocal enabledelayedexpansion
                FOR /F "tokens=1*" %%g in (myfile.txt) do (
                  echo %%g %%h
                  set Line=%%g %%h
                  echo Line = !Line!
                call uninstall.bat !Line!
                echo Get next input line.
                   )

                C:\batch>type   uninstall.bat
                Code: [Select]
                @echo off
                rem Usage uninstall.bat  !Line!
                echo  %1 %2
                set line=%1 %2
                echo In uninstall.bat
                echo line=%line%
                Rem Enter uninstall commands

                echo  return to bud3.bat

                exit /b

                Output:

                C:\batch>bud3.bat
                Line one
                Line = Line one
                 Line one
                In uninstall.bat
                line=Line one
                 return to bud3.bat
                Get next input line.
                Line two
                Line = Line two
                 Line two
                In uninstall.bat
                line=Line two
                 return to bud3.bat
                Get next input line.
                Line three
                Line = Line three
                 Line three
                In uninstall.bat
                line=Line three
                 return to bud3.bat

                C:\batch>
                Have a Nice Day

                greg



                  Intermediate

                  Thanked: 7
                  Re: Increment name and assign variable in a loop: Help
                  « Reply #9 on: March 18, 2010, 06:18:20 PM »
                  I have three programs to uninstall they each have similar names except they are different by one character and each installation is in a unique directory.

                  To uninstall  the uninstall.exe, some keywords, a trail.txt file and the install.log is needed.  I am able to find all three versions of both the uninstall.exe and install.log. The uninstall.exe locations are in one text file and the install.log locations in another text file.


                  Did the uninstall.exe come with each program?

                  Were you able to uninstall the programs?

                  Usually a batch file is not needed to run an uninstall program?

                  Please post your results

                  -------------------------------------------

                  http://www.file.net/process/uninstall.exe.html

                  "Important: Some malware camouflage themselves as Uninstall.exe, particularly if they are located in c:\windows or c:\windows\system32 folder. Thus check the Uninstall.exe process on your pc whether it is pest. "
                  Have a Nice Day

                  greg



                    Intermediate

                    Thanked: 7
                    Re: Increment name and assign variable in a loop: Help
                    « Reply #10 on: March 19, 2010, 06:52:29 PM »

                    To uninstall  the uninstall.exe, some keywords, a trail.txt file and the install.log is needed.  I am able to find all three versions of both the uninstall.exe and install.log. The uninstall.exe locations are in one text file and the install.log locations in another text file.


                    C:\batch>type uninstall.bat

                    Code: [Select]
                    @echo off
                    rem Usage uninstall.bat  !Line!
                    echo  %1 %2
                    set line=%1 %2
                    echo In uninstall.bat
                    echo line=%line%
                    Rem Enter uninstall commands
                    if %2==Belarc (
                    echo Belarc
                    pause
                    cd "C:\Program Files\Belarc\Advisor"
                    uninstall.exe
                    pause
                    )

                    echo  return to bud3.bat

                    exit /b

                    Output:

                    C:\batch>bud4.bat
                    Line one
                    Line = Line one
                     Line one
                    In uninstall.bat
                    line=Line one
                     return to bud3.bat
                    Get next input line.
                    Line Belarc
                    Line = Line Belarc
                     Line Belarc
                    In uninstall.bat
                    line=Line Belarc
                    Belarc

                    Have a Nice Day

                    greg



                      Intermediate

                      Thanked: 7
                      Re: Increment name and assign variable in a loop: Help
                      « Reply #11 on: March 20, 2010, 12:58:45 AM »
                      I have three programs to uninstall they each have similar names except they are different by one character and each installation is in a unique directory.

                      To uninstall  the uninstall.exe, some keywords, a trail.txt file and the install.log is needed.  I am able to find all three versions of both the uninstall.exe and install.log. The uninstall.exe locations are in one text file and the install.log locations in another text file.


                      Read the CH  information how to use uninstall.exe:

                      http://www.computerhope.com/issues/ch000347.htm
                      .
                      .
                      .

                      . . ."There are various commercial and free available solutions for uninstalling software on your computer. We highly recommend and suggest users use Revo Uninstaller, a free and easy to use uninstall program and junk file utility."

                      http://www.revouninstaller.com/revo_uninstaller_free_download.html

                      Have a Nice Day

                      buddyh

                        Topic Starter


                        Starter

                        Re: Increment name and assign variable in a loop: Help
                        « Reply #12 on: April 06, 2010, 07:03:36 AM »
                        Here is an update

                        Thanks everyone.  With what Greg posted I was able to get exactly what I needed.  It may not have been as dynamic as I dreamed of, but it certainly solved my problem.
                        The total objective was to find  and remove all locations of a program installed on a work station and update to the latest version and in only one standardized  location.  The program has been install in several locations and in several releases, major an minor.  I could find each installation using the "whereis.bat" at http://garbo.uwasa.fi/pub/pc/link/tscmd.zip and http://www.netikka.net/tsneti/info/tscmd.php.    Each install location had two files I needed at two different locations so i used Greg's script twice.  The  %%g %%h were needed because of a space in the location, path, that create two tokens.   The return from whereis.bat was parsed to strip out the path and the results stored in justpath.txt and instlogpath.txt.  I set up if statements to hand those rare cases where the number of installations exceeded three. 


                        set /a c=0
                        setlocal enabledelayedexpansion
                        setlocal enableextensions
                        echo.

                           rem File 1 search
                           set /a c=0
                           set locfile=justpath.txt
                           FOR /F "tokens=1*" %%g in (%locfile%) do (
                               set /a c+=1
                               if !c!==1  ( set File1Line1=%%g %%h )
                               if !c!==2   ( set File1Line2=%%g %%h )
                               if !c!==3    ( set File1Line3=%%g %%h )
                              )
                           
                           rem File 2 search
                           echo.
                           set /a c=0
                           set locfile=instlogpath.txt

                           FOR /F "tokens=1*" %%g in (%locfile%) do (
                               set /a c+=1
                               if !c!==1  ( set File2Line1=%%g %%h )
                               if !c!==2   ( set File2Line2=%%g %%h )
                               if !c!==3    ( set File2Line3=%%g %%h )
                              )