Written by /523066680
Posted at:http://www.cn-dos.net/forum/viewthread.php?tid=45240&fpage=1
First, I should state that I didn't invent this usage. I just happened to think of the same method too. In order to promote this method, I
posted this article.
The first time I thought of using it this way was when making Snake. Related analysis:http://www.cn-dos.net/forum/viewthread.php?tid=43369&fpage=1&highlight=%2B523066680 post #23
Here is the relevant part:
So you can see that making proper use of this property can save a lot of trouble.
Let me give a simple example: ping. Usually delays are done with ping (especially for special effects), and you often see scripts using ping in many places.
If you're lazy enough, you can use %variable% in place of ping, like this:
set "ping=ping -n 2 127.1>nul"
%ping%
pause
To change the ping time, you can use %ping: 2 = 3 %
Then for the later occurrences of ping, just use %ping% directly, hehe.
But it feels a bit too lazy. This is just a demonstration; I think the advantage shows up more with longer statements. Of course, special characters are also an issue.
(Not discussed here.)
We can also use %var% to execute for statements, and they can even be nested multiple times. What I found interesting is that the closing parenthesis at the end of for can still be represented with
%var2%, so you can also use another for statement to set up multiple nesting for this %var%,
and then set the number of closing parentheses...
For application examples, see: http://www.cn-dos.net/forum/viewthread.php?tid=41243&page=3&sid=8HBO3h#pid317040 post #27
and How to generate a purely numeric password dictionary
http://www.cn-dos.net/forum/viewthread.php?tid=33139&pid=317042&page=1&sid=8HBO3h#pid317042 post #6
As I said, I didn't invent this method. I just discovered it and am acting as an advocate for it. I checked the batch files that come with the system,
and one of them, called msdtcvtr.bat (all in English... *sweat*)
also has a very good example. Here's the opening part:
@echo off
rem *************** start of 'main'
set DEBUG=0
if "%DEBUG%"=="1" (set TRACE=echo) else (set TRACE=rem)
rem Note that right now there is a bug in tracerpt.exe cause of which you might want to use tracefmt.exe instead.
rem set the value 1 if you want to use tracefmt.exe instead
set USE_TRACE_FMT=1
%TRACE% The value of variable USE_TRACE_FMT is %USE_TRACE_FMT%
:
:
Everyone sees the TRACE variable, right? It decides whether to show a line to the user. I think this is an especially good example.
Because later there are many places with
%TRACE% "information that may need to be displayed"
If all of those used if to decide whether to display, I guess the code....
[ Last edited by 523066680 on 2008-12-19 at 10:52 ]
Posted at:http://www.cn-dos.net/forum/viewthread.php?tid=45240&fpage=1
First, I should state that I didn't invent this usage. I just happened to think of the same method too. In order to promote this method, I
posted this article.
The first time I thought of using it this way was when making Snake. Related analysis:http://www.cn-dos.net/forum/viewthread.php?tid=43369&fpage=1&highlight=%2B523066680 post #23
Here is the relevant part:
About recording the coordinates of a point with a certain "index":
set cut%i%="set /a a=%line%,b=%col%,c=%C2%" This whole statement that defines three variables is put into one
index variable, hehe. Taking advantage of the nature of batch files, when these three variables are needed later, one line of !cut%i%:"=! is equivalent to executing
set /a a=%line%,b=%col%,c=%C2% After that, just use %a% %b% %c% directly, rather than !var%num%! or %%var%num%%%.
So you can see that making proper use of this property can save a lot of trouble.
Let me give a simple example: ping. Usually delays are done with ping (especially for special effects), and you often see scripts using ping in many places.
If you're lazy enough, you can use %variable% in place of ping, like this:
set "ping=ping -n 2 127.1>nul"
%ping%
pause
To change the ping time, you can use %ping: 2 = 3 %
Then for the later occurrences of ping, just use %ping% directly, hehe.
But it feels a bit too lazy. This is just a demonstration; I think the advantage shows up more with longer statements. Of course, special characters are also an issue.
(Not discussed here.)
We can also use %var% to execute for statements, and they can even be nested multiple times. What I found interesting is that the closing parenthesis at the end of for can still be represented with
%var2%, so you can also use another for statement to set up multiple nesting for this %var%,
and then set the number of closing parentheses...
For application examples, see: http://www.cn-dos.net/forum/viewthread.php?tid=41243&page=3&sid=8HBO3h#pid317040 post #27
and How to generate a purely numeric password dictionary
http://www.cn-dos.net/forum/viewthread.php?tid=33139&pid=317042&page=1&sid=8HBO3h#pid317042 post #6
@echo off &setlocal enabledelayedexpansion
set /p "n=Enter number of digits, maximum is 9: "
for /l %%x in (1,1,%n%) do (
set fo="for %%%%x in (0 1 2 3 4 5 6 7 8 9) do (!fo!"
set end="!end!)"
set echo=!echo! %%%%x
)
%fo:"=%
echo %echo%
%end:"=%
pause
As I said, I didn't invent this method. I just discovered it and am acting as an advocate for it. I checked the batch files that come with the system,
and one of them, called msdtcvtr.bat (all in English... *sweat*)
also has a very good example. Here's the opening part:
@echo off
rem *************** start of 'main'
set DEBUG=0
if "%DEBUG%"=="1" (set TRACE=echo) else (set TRACE=rem)
rem Note that right now there is a bug in tracerpt.exe cause of which you might want to use tracefmt.exe instead.
rem set the value 1 if you want to use tracefmt.exe instead
set USE_TRACE_FMT=1
%TRACE% The value of variable USE_TRACE_FMT is %USE_TRACE_FMT%
:
:
Everyone sees the TRACE variable, right? It decides whether to show a line to the user. I think this is an especially good example.
Because later there are many places with
%TRACE% "information that may need to be displayed"
If all of those used if to decide whether to display, I guess the code....
[ Last edited by 523066680 on 2008-12-19 at 10:52 ]
Recent Ratings for This Post
( 1 in total)
Click for details
| Rater | Score | Time |
|---|---|---|
| HAT | +2 | 2008-12-20 16:55 |

