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

Author Topic: Find out how many times a Batch is used.  (Read 6028 times)

0 Members and 1 Guest are viewing this topic.

BatchRocks

    Topic Starter


    Hopeful
  • Thanked: 3
    Find out how many times a Batch is used.
    « on: March 19, 2009, 04:12:51 PM »
    Does anyone know if there is a way to find out how MANY times a batch file is opened?

    I'm curious :O.

    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: Find out how many times a Batch is used.
    « Reply #1 on: March 19, 2009, 04:14:05 PM »
    you could echo a value into the batch file itself as an alternate data stream... but unless you code for it you cannot know...
    I was trying to dereference Null Pointers before it was cool.

    BatchRocks

      Topic Starter


      Hopeful
    • Thanked: 3
      Re: Find out how many times a Batch is used.
      « Reply #2 on: March 19, 2009, 04:24:50 PM »
      Can it be... (Make 'Result.txt' and put a 1 in it, then edit this code accordingly)

      Code: [Select]
      set /a num=1
      echo %num% >>Result.txt
      find /i "%num%" Result.txt
      if exist set %num%+1=%num% >> Result.txt

      That makes a file names Result.txt and then it echoes some random gibberish, and in Result.txt, it has:

      Quote
      1
      1

      Each time it is opened.

      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: Find out how many times a Batch is used.
      « Reply #3 on: March 19, 2009, 04:48:25 PM »
      use > when saving back to the file.
      I was trying to dereference Null Pointers before it was cool.

      BatchRocks

        Topic Starter


        Hopeful
      • Thanked: 3
        Re: Find out how many times a Batch is used.
        « Reply #4 on: March 19, 2009, 04:51:38 PM »
        Still not working

        BatchFileCommand



          Hopeful
        • Thanked: 1
          Re: Find out how many times a Batch is used.
          « Reply #5 on: March 19, 2009, 05:00:26 PM »
          Here's an advanced and clean way.  Number.txt is the file which keeps track of the count.


          Code: [Select]

          @echo off
          setlocal enabledelayedexpansion
          if exist number goto add

          echo 0 > number.txt

          :add
          cls
          for /f %%a in ('type number.txt') do (
            set uses=%%a
            set /a uses=1 + !uses!
            del number.txt
            echo !uses! > number.txt
            echo This file has been used !uses! time(s^)
            pause>nul
            exit
          )


          οτη άβγαλτος μεταφ βαθμολογία

          BatchRocks

            Topic Starter


            Hopeful
          • Thanked: 3
            Re: Find out how many times a Batch is used.
            « Reply #6 on: March 19, 2009, 05:03:06 PM »
            It's not working :O!

            BatchFileCommand



              Hopeful
            • Thanked: 1
              Re: Find out how many times a Batch is used.
              « Reply #7 on: March 19, 2009, 05:14:28 PM »
              I got it working, then I tried to do something and screwed it up. But now when I try to do

              Code: [Select]
              echo (something) > (something).txt

              or

              Code: [Select]
              echo (something) >> (something).txt

              It says access denied.


              οτη άβγαλτος μεταφ βαθμολογία

              BatchRocks

                Topic Starter


                Hopeful
              • Thanked: 3
                Re: Find out how many times a Batch is used.
                « Reply #8 on: March 19, 2009, 05:16:31 PM »
                It's confusing D:. What's the difference between %random% and !Random!?

                BatchFileCommand



                  Hopeful
                • Thanked: 1
                  Re: Find out how many times a Batch is used.
                  « Reply #9 on: March 19, 2009, 07:17:38 PM »
                  I will explain the difference. If a variable is set in a for loop then used in the same for loop must be sorrounded by !'s instead of %'s .
                  οτη άβγαλτος μεταφ βαθμολογία

                  oldun

                  • Guest
                  Re: Find out how many times a Batch is used.
                  « Reply #10 on: March 19, 2009, 09:09:56 PM »
                  How about this:

                  Code: [Select]
                  @echo off
                  if not exist times_opened.txt (Echo 1 >times_opened.txt&Echo %0 has been executed 1 time&goto :eof)
                  for /f %%a in (times_opened.txt) do (
                  set /a times=%%a+1)
                  echo %times% >times_opened.txt
                  echo %0 has been executed %times% times

                  Reno



                    Hopeful
                  • Thanked: 32
                    Re: Find out how many times a Batch is used.
                    « Reply #11 on: March 19, 2009, 10:16:36 PM »
                    hello, Duo BatchFile-Rock

                    you can save the value inside the batch file itself as suggested by BC_Programmer
                    here is how to do it:
                    Code: [Select]
                    @set c=0
                    @echo off
                    call:tick

                    echo Your Batch Code Goes Here
                    echo Current Count=%c%
                    echo Batch Code End Here

                    goto:eof

                    :tick
                    set/a c+=1
                    >$$$ (echo @set c=%c%& more +1 %~nx0)
                    move $$$ %~nx0

                    to try it type in command prompt:
                    for /l %a in (1 1 20) do @save.bat
                    « Last Edit: March 20, 2009, 04:31:26 AM by Reno »

                    Reno



                      Hopeful
                    • Thanked: 32
                      Re: Find out how many times a Batch is used.
                      « Reply #12 on: March 20, 2009, 12:13:28 AM »
                      It's confusing D:. What's the difference between %random% and !Random!?
                      I will explain the difference. If a variable is set in a for loop then used in the same for loop must be sorrounded by !'s instead of %'s .

                      batchfilecommand is right, the ! sign is mostly used inside a for loop when setting variable, it's used in conjunction with the setlocal enabledelayedexpansion. but there is a pitfall when using it, for example parsing a string containing this character !, and setting the token to a var, then you will lose the string surrounded by ! !.

                      here is the detail explanation:
                      Quote
                      Delayed environment variable expansion is useful for getting around
                      the limitations of the current expansion which happens when a line
                      of text is read, not when it is executed.  The following example
                      demonstrates the problem with immediate variable expansion:

                          set VAR=before
                          if "%VAR%" == "before" (
                              set VAR=after
                              if "%VAR%" == "after" @echo If you see this, it worked
                          )

                      would never display the message, since the %VAR% in BOTH IF statements
                      is substituted when the first IF statement is read, since it logically
                      includes the body of the IF, which is a compound statement.  So the
                      IF inside the compound statement is really comparing "before" with
                      "after" which will never be equal.  Similarly, the following example
                      will not work as expected:

                          set LIST=
                          for %i in (*) do set LIST=%LIST% %i
                          echo %LIST%

                      in that it will NOT build up a list of files in the current directory,
                      but instead will just set the LIST variable to the last file found.
                      Again, this is because the %LIST% is expanded just once when the
                      FOR statement is read, and at that time the LIST variable is empty.
                      So the actual FOR loop we are executing is:

                          for %i in (*) do set LIST= %i

                      which just keeps setting LIST to the last file found.

                      Delayed environment variable expansion allows you to use a different
                      character (the exclamation mark) to expand environment variables at
                      execution time.  If delayed variable expansion is enabled, the above
                      examples could be written as follows to work as intended:

                          set VAR=before
                          if "%VAR%" == "before" (
                              set VAR=after
                              if "!VAR!" == "after" @echo If you see this, it worked
                          )

                          set LIST=
                          for %i in (*) do set LIST=!LIST! %i
                          echo %LIST%

                      you will realized that it is from the built-in help file. yes, correct, it's the best place to learn batch and lots of tricks can be learn from built-in help.
                      type: set/?

                      BatchRocks

                        Topic Starter


                        Hopeful
                      • Thanked: 3
                        Re: Find out how many times a Batch is used.
                        « Reply #13 on: March 20, 2009, 05:05:57 AM »
                        How about this:

                        Code: [Select]
                        @echo off
                        if not exist times_opened.txt (Echo 1 >times_opened.txt&Echo %0 has been executed 1 time&goto :eof)
                        for /f %%a in (times_opened.txt) do (
                        set /a times=%%a+1)
                        echo %times% >times_opened.txt
                        echo %0 has been executed %times% times


                        His is the best one- It makes a file named times_opened.txt And Puts 1, then I use the Batch again, 2, ect.

                        All the batch files don't stay open though, they close really quickly.

                        feras



                          Beginner

                          Thanked: 1
                        • Experience: Experienced
                        • OS: Linux variant
                        Re: Find out how many times a Batch is used.
                        « Reply #14 on: March 27, 2009, 11:09:37 AM »
                        hello

                        this script will do that but it rely on the registry

                        Code: [Select]
                        @echo off
                        cls
                        REG QUERY "HKLM\software\usage"
                        if not errorlevel=1 goto count
                        if errorlevel=1 reg add "HKLM\software\usage" /v used /t REG_SZ /d 1 /f
                        cls
                        title It is the first time you open this program
                        cls
                        pause > nul
                        exit
                        :count
                        FOR /F "tokens=2* delims= " %%A IN ('REG QUERY "HKLM\software\usage"') DO SET n=%%B
                        set /a r=n+1
                        reg add "HKLM\software\usage" /v used /t REG_SZ /d %r% /f
                        cls
                        title this program has been used for %r% time (s)
                        pause > nul

                         :)
                        « Last Edit: April 01, 2009, 11:09:22 AM by feras »