![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-04 18:29 |
48,038 topics / 350,123 posts / today 0 new / 48,251 members |
| DOS批处理 & 脚本技术(批处理室) » [Sharing] Using %var% in place of statement execution |
| Printable Version 3,000 / 9 |
| Floor1 523066680 | Posted 2008-12-19 10:46 |
| 银牌会员 Posts 1,133 Credits 2,362 | |
|
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 ] |
|
| Floor2 wxcute | Posted 2008-12-19 11:13 |
| 中级用户 Posts 211 Credits 458 | |
|
You still use it better.
When I was modifying your Snake, I used one too, but mine felt a bit like gilding the lily. |
|
| Floor3 523066680 | Posted 2008-12-20 16:52 |
| 银牌会员 Posts 1,133 Credits 2,362 | |
|
No one actually looked...... sigh I'm too much of a newbie
always doing thankless hard work |
|
| Floor4 zjw767676 | Posted 2009-04-17 11:27 |
| 新手上路 Posts 8 Credits 15 | |
|
Good wine needs no bush! I came over from bathome to bump your thread!! O(∩_∩)O~
|
|
| Floor5 netbenton | Posted 2009-04-17 12:20 |
| 银牌会员 Posts 752 Credits 1,916 From 广西 | |
| Floor6 523066680 | Posted 2009-04-18 05:55 |
| 银牌会员 Posts 1,133 Credits 2,362 | |
|
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 ] |
|
| Floor7 netbenton | Posted 2009-04-18 06:15 |
| 银牌会员 Posts 752 Credits 1,916 From 广西 | |
|
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. |
|
| Floor8 523066680 | Posted 2009-04-18 21:02 |
| 银牌会员 Posts 1,133 Credits 2,362 | |
|
Oh, got it. Posting this as a reply.
|
|
| Floor9 hugreen | Posted 2009-04-18 22:28 |
| 初级用户 Posts 15 Credits 23 | |
|
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. |
|
| Floor10 yishanju | Posted 2009-04-18 23:02 |
| 银牌会员 Posts 1,357 Credits 1,488 | |
|
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.
|
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |