Loading ...
Sorry, an error occurred while loading the content.

Re: [BATCH WORLD] Processing a List

Expand Messages
  • Michael Marquart
    Smee again Bob. I had some idle time today so had another look at your batch file and here s what I came up with, after tidying some loose ends and renaming
    Message 1 of 9 , Jan 1, 2002
    • 0 Attachment
      Smee again Bob.

      I had some idle time today so had another look at your batch file and
      here's what I came up with, after tidying some loose ends and renaming the
      temp files with temp/tmp in them.

      Happy New Year to you and the others reading the list.


      :: LIST_BAT.BAT
      @echo off
      :: Notes
      :: Must do! Create a file named setit.txt with one line
      :: of text which reads "set fname=" No quotes.
      :: Don't hit return after the = sign.
      :start
      set m=c:\mine\batch\bat_list\master.doc
      if exist %m% del %m%
      set line=~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      set line=%line%%line%
      echo Working...............................
      dir c:\mine\batch\*.bat /o:n /b > listtemp.txt
      c:
      cd c:\mine\batch\bat_list
      ::
      :loop1
      :: setit.txt is a one line text file of "set fname="
      :: with no terminating cr/lf pair and without the quotes.
      :: The next command below places the command "set fname="
      :: in a file named filetmp1.txt
      type setit.txt>filetmp1.txt
      type listtemp.txt >> filetmp1.txt
      type filetmp1.txt | find "set fname=" > filetmp2.bat
      type filetmp1.txt | find /v "set fname=" > listtemp.txt
      call filetmp2.bat
      echo Processing %fname%
      echo %fname% Batch File Commands >>%m%
      type "c:\mine\batch\%fname%" >> %m%
      echo %line% >> %m%
      ::
      :: When file2.txt is empty DOS generates a "0 file(s) copied"
      :: message. FIND detects the 0 and batch processing stops.
      copy listtemp.txt NUL | find "0" > NUL
      if errorlevel 1 goto loop1

      :done
      echo.
      echo. COMPLETE
      echo.
      echo.
      choice /c:YN /n Do You Want To Read The Document? Y or N ?
      if not errorlevel 2 start wordpad %m%

      :cleanup
      set m=
      set file=
      if exist filetmp1.txt erase filetmp1.txt
      if exist filetmp2.bat erase filetmp2.bat
      if exist listtemp.txt erase listtemp.txt
      :end



      If I can make a minor criticism - When changing to a defined directory
      you should always change the drive too.
      Changing to the root before changing to an explicit path is not necessary.


      Although it makes batchs a little harder to read, it's good programming
      practice not to include spaces prior to, or after the redirection or piping
      characters > >> and |
      One reason can be seen from this example. These two lines will not produce
      the same result in %var% as the first one will have a trailing space
      set var=abc > file.txt
      set var=abc>file.txt
      and in some situations you may unintentially create a temporary filename
      with a leading space.


      I've removed some of the erase commands from the loop to save some room as
      when > is used with a file then it will automatically be deleted if it
      already exists, before the redirection is performed. It may be wiser to
      become used to using DEL instead of ERASE as del is more commonly used.

      The other changes I made are cosmetic - if you have any queries then ask.



      On Mon, 31 Dec 2001 07:47:31 -0000, "ocalabob" <bobb@...> wrote:

      >...win98se...
      >
      >My Objective:
      >Make a master document of each of my batch files
      >line by line.
      >
      >Problem:
      >The master file heading for each "typed" file has a *.bob
      >extension rather than the real file name extension of *.bat
      >
      >For me this is a "cosmetic" issue since I alone use the file but
      >I'd like to fix it. (I do this for fun!!)
      >
      >Any suggested remedies? Any way to "tighten up" this bat file code?

      --

      Regards,
      Michael

      --> Honk if you love peace and quiet.
    • Michael Marquart
      As you can see I ve been batchin again :) ... Glad to hear it. ... @echo off if %2 == ! goto recurse for %%a in (c: mine batch *.bat) do call %0 %%a ! goto
      Message 2 of 9 , Jan 1, 2002
      • 0 Attachment
        As you can see I've been batchin' again :)

        On Tue, 01 Jan 2002 01:29:07 -0000, "ocalabob" <bobb@...> wrote:

        >Whoa Michael! The redesign you posted in your first reply was
        >great fun to work with and, it worked right out of the box!
        >No batteries Required! <smile>

        Glad to hear it.

        >But your redesign has stumped me (as you asked) on how
        >each file name gets passed to the working bat file. I've
        >listed it below and would appreciate your comments on
        >the lines that make this file work.

        @echo off
        if "%2"=="!" goto recurse

        for %%a in (c:\mine\batch\*.bat) do call %0 %%a !
        goto end

        :recurse
        echo %1 Batch File Commands>>%m%
        :end


        The above illustrates the minimum required for some recursion.
        1) the batch file starts, ignores the if test and runs the for in do

        2) the for in do launches a second copy of the same batch file (the name of
        which is stored in %0 when you launch any batch file) and it passes it two
        parameters. The first, %%a is the name of the first item in the *.bat list
        and the second, ! is simply there to provide a means of testing when to
        branch to the working part of the batch file.

        3) So the second copy of the batch file runs, the if test passes and it
        branches to :recurse , performs the commands (in this case it echos the
        first parameter that was on the command line [of the second copy of the
        batch file]) and runs out of batch file so it ends.

        4) then the first iteration of the batch file takes command again and the
        for in do continues to do it's work and calls the batch file again as in 2)
        but with the %%a containing the second file from the *.bat entry.

        It continues to follow this behavior, launching a second copy of the batch
        file with a different item every time, until the for in do command runs out
        of things in it's list and then the original batchfile drops to the next
        command, which is "goto end" and the batch file finally concludes.

        Clear as mud? ;)

        --

        Regards,
        Michael

        --> Honk if you love peace and quiet.
      • Michael Marquart
        On Tue, 01 Jan 2002 21:53:31 +1100, Michael Marquart ... Of course those two examples aren t legal batch commands. I meant something like
        Message 3 of 9 , Jan 1, 2002
        • 0 Attachment
          On Tue, 01 Jan 2002 21:53:31 +1100, Michael Marquart <micm@...>
          wrote:

          >Although it makes batchs a little harder to read, it's good programming
          >practice not to include spaces prior to, or after the redirection or piping
          >characters > >> and |
          >One reason can be seen from this example. These two lines will not produce
          >the same result in %var% as the first one will have a trailing space
          >set var=abc > file.txt
          >set var=abc>file.txt
          >and in some situations you may unintentially create a temporary filename
          >with a leading space.

          Of course those two examples aren't legal batch commands. I meant
          something like this:

          echo set var=abc > file.bat
          echo set var=abc>file.bat

          --

          Regards,
          Michael

          --> Honk if you love peace and quiet.
        • Michael Marquart
          I m like a dog with a bone - can t leave it alone! On Mon, 31 Dec 2001 23:00:42 +1100, Michael Marquart wrote: This version works here
          Message 4 of 9 , Jan 1, 2002
          • 0 Attachment
            I'm like a dog with a bone - can't leave it alone!

            On Mon, 31 Dec 2001 23:00:42 +1100, Michael Marquart <micm@...>
            wrote:

            This version works here under Win98se - try it without the CLS and see what
            appears on the screen.

            You could try removing the trailing backslash from
            cd \mine\batch\
            but I don't think that is an issue.

            There must be some bat files in c:\mine\batch of course, but that's not the
            problem is it.

            For debugging purposes you can insert a pause command after the ::top line
            and examine the temporary files when it pauses.


            > @echo off
            > set m=c:\mine\batch\bat_list\master.doc
            > set line=~~~~~~~~
            > set line=%line%%line%
            > set line=%line%%line%
            > set line=%line%%line%
            > echo Working...............................
            > if exist %m% del %m%
            > c:
            > cd \mine\batch\
            > dir *.bat /o:n /b>temp0.001
            > :start
            > :: number the list
            > echo.>>temp0.001
            > echo.>>temp0.001
            > type temp0.001|time|find /i "enter"|find /n /i "enter">temp0.bat
            > del temp0.001
            > echo set target=%%6>enter.bat
            > echo set count=%%2>----------.bat
            > if exist temp1.bat del temp1.bat
            > :loop
            > :: counter
            > echo !>>temp1.bat
            > find /c "!" temp1.bat>temp2.bat
            > call temp2.bat
            > :: strip the item with the number of the counter
            > type temp0.bat|find "[%count%]Enter "|time|find "[%count%]">temp3.bat
            > call temp3.bat
            > :: check for final item
            > if "%target%"=="" goto done
            >
            > :: top
            > echo Processing item number %count% - %target%
            > echo %target% Batch File Commands>>%m%
            > type %target%>>%m%
            > echo %line%>>%m%
            > :: bottom
            >
            > goto loop
            > :done
            > cls
            > echo.
            > echo COMPLETE
            > echo.
            > echo.
            > choice /c:YN /n Do You Want To Read The Document? Y or N ?
            > if not errorlevel 2 start wordpad %m%
            > set m=
            > set file=
            >
            > :end
            > for %%a in (enter ----------) do if exist %%a.bat del %%a.bat
            > for %%a in (temp0 temp1 temp2 temp3) do if exist %%a.bat del %%a.bat
            > for %%a in (target count dir file) do set %%a=
            >

            --

            Regards,
            Michael

            --> Honk if you love peace and quiet.
          • ocalabob
            ... Naw, more like a bum on a ham sandwich! I can take a bone away from a dog! Success! The batch file works great! Listed at the end of this post is
            Message 5 of 9 , Jan 1, 2002
            • 0 Attachment
              --- In batchworld@y..., Michael Marquart <micm@m...> wrote:
              > I'm like a dog with a bone - can't leave it alone!

              Naw, more like a bum on a ham sandwich! I can take a bone
              away from a dog! <grin>

              Success! The batch file works great! Listed at the end
              of this post is the final product that I'm using.

              Big thanks to Michael Marquart for his comments,
              suggestions and examples. That was quite a
              learning experience!

              A major tip I learned that helped me exorcise demons
              from the batch file is: command /y /c filename.bat
              for example click start ---> run then type
              command /y /c autoexec.bat and you will step
              through each line of your autoexec.bat file.
              Y or N replys are obvious but typing an
              "a" (no quotes) will complete the batch file.
              (well, it was news to me!)

              This is a great tool. After reading Michael's "clear
              as mud" reply on recursion I printed his explanation
              and then "watch" the batch file work line by line.
              Very informative.

              Happy New Years All !!

              -----------------------------------------------
              :: LST_BATS.BAT
              :: Batch file to create a document with the name of each batch file
              :: and a list of its command lines. Many thanks to Michael Marquart
              :: for his major contribution ie complete re-work.
              ::Tested in W95 version 4.00.950 and W98 version 4.10.2222
              :: No allowances are made for long file names or file names with
              spaces.
              :: Completed 1-1-2002
              ::
              @echo off
              :: Uncomment the line below if running from a c: prompt
              ::smartdrv
              :: Change the line below to point to the correct batch file folder.
              set m=c:\mine\batch\mst_list.doc
              set line=~~~~~~~~
              set line=%line%%line%
              set line=%line%%line%
              set line=%line%%line%
              echo Working...............................
              if exist %m% del %m%
              c:
              :: Change the line below to point to the correct batch file folder.
              cd\mine\batch
              dir *.bat /o:n /b>temp0.001
              :start
              :: number the list
              echo.>>temp0.001
              echo.>>temp0.001
              type temp0.001|time|find /i "enter"|find /n /i "enter">temp0.bat
              del temp0.001
              echo set target=%%6>enter.bat
              echo set count=%%2>----------.bat
              if exist temp1.bat del temp1.bat
              :loop
              :: counter
              echo !>>temp1.bat
              find /c "!" temp1.bat>temp2.bat
              call temp2.bat
              :: strip the item with the number of the counter
              type temp0.bat|find "[%count%]Enter "|time|find "[%count%]">temp3.bat
              call temp3.bat
              :: check for final item
              if "%target%"=="" goto done

              :: top
              echo Processing item number %count% - %target% >nul
              echo %target% Batch File Line By Line Commands>>%m%
              type %target%>>%m%
              echo %line%>>%m%
              :: bottom

              goto loop
              :done
              cls
              echo.
              echo COMPLETE
              echo.
              echo.
              choice /c:YN /n Do You Want To Read The Document? Y or N ?
              :: Swap the lines below if running from a c: prompt
              ::if not errorlevel 2 edit %m%
              if not errorlevel 2 start wordpad %m%
              set m=
              set file=

              :end
              for %%a in (enter ----------) do if exist %%a.bat del %%a.bat
              for %%a in (temp0 temp1 temp2 temp3) do if exist %%a.bat del %%a.bat
              for %%a in (target count dir file) do set %%a=
              -------------------------------------------------------

              Keep honking that horn Michael, I'm reloading! <smile>
            Your message has been successfully submitted and would be delivered to recipients shortly.