Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If all you're trying to do is count how many times a cmd script is called, you can just append one character to a file every time it runs rather than fiddling around with expression evaluation every time the script is run. This also has the advantage of making the script quicker since the analysis of the count is moved elsewhere.</p> <p>The counter file expands by one byte every time, so watch out if you're calling it a truly large number of times since the file will expand. But even, calling it once per second, a 1G file will accrue only after 30 years.</p> <p>At the top of your script, just put:</p> <pre><code>set &gt;&gt;countfile.txt &lt;nul: /p x=X </code></pre> <p>This simply adds the character <code>X</code> to the end of the <code>countfile.txt</code> file every time the script is called. It uses the "<code>set/p</code>" command with input/output redirection, which is the only way I'm aware of to get a character out without a CR/LF following it (like the UNIX "<code>echo -n</code>").</p> <p>To get a count of the number of calls to date, you can use the file size environment variable modifier, as in the following script (I expect this will be done less often than running the script so it's better to put the grunt work here [in fact, it's not a lot of grunt work since it's not counting the characters, rather it gets the information directly from the directory entry]):</p> <pre><code> @echo off goto :main :getsize echo %~z1 goto :eof :main setlocal enableextensions enabledelayedexpansion call :getsize countfile.txt endlocal </code></pre> <p>To reset the count, use the following extremely complicated command (I'm thinking of patenting this):</p> <pre><code>del countfile.txt </code></pre> <p>One other thing I'd suggest - you don't need to prefix every command with "<code>@</code>" to prevent echo, you can simply put "<code>@echo off</code>" at the top of your script for global no-echo. If you want to selectively echo some commands, just ignore this paragraph.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload