China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-04 19:35
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Sharing] Using %var% in place of statement execution View 3,002 Replies 9
Original Poster Posted 2008-12-19 10:46 ·  中国 广东 揭阳 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
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:

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
RaterScoreTime
HAT +2 2008-12-20 16:55
Floor 2 Posted 2008-12-19 11:13 ·  中国 福建 三明 电信
中级用户
★★
Credits 458
Posts 211
Joined 2006-07-26 19:42
20-year member
UID 59307
Status Offline
You still use it better.
When I was modifying your Snake, I used one too, but mine felt a bit like gilding the lily.
┌───────┐
├→学习→实践→┤
└───────┘
Floor 3 Posted 2008-12-20 16:52 ·  中国 广东 揭阳 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
No one actually looked...... sigh I'm too much of a newbie
always doing thankless hard work
Floor 4 Posted 2009-04-17 11:27 ·  中国 江苏 常州 中移铁通
新手上路
Credits 15
Posts 8
Joined 2009-01-12 16:11
17-year member
UID 136479
Gender Male
Status Offline
Good wine needs no bush! I came over from bathome to bump your thread!! O(∩_∩)O~
Floor 5 Posted 2009-04-17 12:20 ·  中国 广东 东莞 电信
银牌会员
★★★
批处理编程迷
Credits 1,916
Posts 752
Joined 2008-12-28 04:30
17-year member
UID 135147
Gender Male
From 广西
Status Offline
精简
=> 个人网志
Floor 6 Posted 2009-04-18 05:55 ·  中国 广东 揭阳 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
In the internet café, in a rush, my brain just buzzed for a second, and I simply couldn't figure out how to use it.

[ Last edited by 523066680 on 2009-4-18 at 05:57 ]
Floor 7 Posted 2009-04-18 06:15 ·  中国 广东 东莞 电信
银牌会员
★★★
批处理编程迷
Credits 1,916
Posts 752
Joined 2008-12-28 04:30
17-year member
UID 135147
Gender Male
From 广西
Status Offline
After running it, it asks you to enter one line of batch command,
after entering the command and pressing Enter, (press Enter directly to quit)
it will test how long that command takes to run 1000 times (milliseconds)

Then the prompt that appears is:

是否保存记录[Y][R]:

Enter y to save the result you just tested into the file,
enter r to test once again; if you save after multiple tests, it will take the average result.
Entering anything else returns you to the state of entering the command line again.

=================

This tool can measure and compare which command is more efficient,
but some commands can't be tested, such as: exit, which will quit directly.
精简
=> 个人网志
Floor 8 Posted 2009-04-18 21:02 ·  中国 广东 揭阳 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
Oh, got it. Posting this as a reply.
Floor 9 Posted 2009-04-18 22:28 ·  中国 广东 广州 电信
初级用户
Credits 23
Posts 15
Joined 2008-03-26 19:44
18-year member
UID 114120
Gender Male
Status Offline
Thanks for sharing
Collected
That TRACE example is very good. It makes one think of the corresponding TRACE function in C++. The better the algorithm is written, the closer batch files get to high-level languages.
Floor 10 Posted 2009-04-18 23:02 ·  中国 北京 联通
银牌会员
★★★★
[b]看你妹啊[/b]
Credits 1,488
Posts 1,357
Joined 2006-05-20 12:00
20-year member
UID 55770
Status Offline
When first learning batch, what you write are commands. Later, what you write are collections of commands. Go further and master it, and you'll start writing it as a programming language.

有问题请发论坛或者自行搜索,再短消息问我的统统是SB
Forum Jump: