没看懂是因为: 试图一上来就从含有(变量赋值、变量内容替换、变量显示、如何显示特殊符号)等混合内容的命令串读起:)
) 变量赋值: set str=www.cn-dos.net
) 变量内容替换: %str:www=bbs%
) 变量显示: echo 执行set str=%%str:www=bbs%%
(%%代表显示%号本身)
) 显示特殊符号: echo 执行set str=%%str:www=bbs%%
%%双百分号代表显示%本身(与值与处理无关),显示结果为:执行set str=%str:www=bbs%
) ……
下面代码很简单 (之所以称做简单是因为: 如果基础知识都学过了,就是很简单)
下面代码也比较难(之所以称作比较难是因为: 如果基础知识不看,上来看就象看程序一样的读,势必总有读不懂的地方)
@echo off
set str=www.cn-dos.net
echo 替换前变量str的内容为: %str%
echo 执行set str=%%str:www=bbs%%
echo 替换后变量str的内容为: %str:www=bbs%
pause
建议dshh98、xue2ye兄先从基础知识学起,会越来越得心应手的~:)
批处理和类似C语言或是VB的学习点不同:
-----------------------------------
) C与VB等都有函数库(函数)可以调用,无论是文件操作还是字符串提取、打印等都有专门的函数库可选。
) 而批处理就是命令的集和。无论是DOS命令或是Windows的CMD Shell还是调用系统或第三方提供的应用程序等……
它之所以称之为“批处理”,是因为你把那些想要调用运行的命令和赋值等操作统一的使用“成批处理”的方式运行~:)
) “批处理”没有提供给你象API还是函数库的调用解决之道,系统(DOS或是Windows)给你提供内部、外部命令或应用程序有什么就可以调用什么。
例如:DIR是系统的内部命令,但DIR有很多的参数……(它不是批处理提供的)
以个人理解不标准的非官方的解释就是:批处理就是命令的集和,把很多命令或是程序集中在一个脚本写成的文件里统一运行。
) 所以,C或VB的学习点是语法、函数库、API……
) 批处理学习点:你对操作系统提供的内部命令或是外部命令或是第三方应用程序理解有多深,用的就有多深。
(举例:) REG.EXE (CMD Shell),它对注册表操作。
REG有很多执行参数可以选择,批处理应用它只不过是调用了它的某个参数,
放到批处理里集中和别的指令一块运行而已。
(举例:) Format 格式化的命令,也有很多参数,可以直接运行,也可以放到批处理这个脚本文件里调用它执行。
所以,批处理里要想用的得心应手(举例:象版主那样想怎么做就能怎么做),
就必须将变量的赋值、变量内容替换、特殊符号、转义(脱逸符)等基础知识要学透:)
而这些基础知识学得有多深、理解的有多透,就意味着你怎么的得心应手的完成意愿~:)
) 如果象学编程一样(象现代教学法:高速什么24小时速成,什么从入门到精通之类的学习方法),根本行不通:)
到头来还得回过来从新学习这些基础知识:)
) 建议从基础学起。(批处理还就靠这些基础,没这些基础寸步难行)
==========================================
(举一个实例)
《如何判断变量的长度?》
如果使用批处理来计算变量(一个存有字符串的变量)内含有字符串的长度,
系统并没有给你直接提供象C或VB计算字串长度的函数,
这个时候,算法和平日积蓄起来的大量处理变量的经验就告诉了你:
应该使用循环单个变量和累加计数等不同的方式来巧妙达到以上功能~:)
(详细实例见下面地址:)
http://www.cn-dos.net/forum/viewthread.php?tid=24745&fpage=1
而这些并不难,它应用的还是变量赋值、循环、累加(也是变量赋值)、延时变量……的基础知识,
所以,离开了基础知识将寸步难行~:)
--------------------------------
所以,学批处理不从基础知识开始学起等于白学~:)
Last edited by redtek on 2006-11-15 at 09:57 PM ]
The reason for not understanding is: Trying to start reading from a command string mixed with (variable assignment, variable content replacement, variable display, how to display special symbols) and so on from the beginning :)
) Variable assignment: set str=www.cn-dos.net
) Variable content replacement: %str:www=bbs%
) Variable display: echo Execute set str=%%str:www=bbs%%
(%% represents displaying the % sign itself)
) Displaying special symbols: echo Execute set str=%%str:www=bbs%%
%% double percent signs represent displaying % itself (irrelevant to the value and processing), and the display result is: Execute set str=%str:www=bbs%
) ……
The following code is very simple (the reason it is called simple is: If you have learned all the basic knowledge, it is very simple)
The following code is also relatively difficult (the reason it is called relatively difficult is: If you don't look at the basic knowledge and start reading it like a program, there will always be things you don't understand)
@echo off
set str=www.cn-dos.net
echo The content of variable str before replacement is: %str%
echo Execute set str=%%str:www=bbs%%
echo The content of variable str after replacement is: %str:www=bbs%
pause
It is suggested that brothers dshh98 and xue2ye start learning from the basic knowledge first, and you will become more and more handy ~:)
Batch processing is different from the learning points of C language or VB:
-----------------------------------
) C and VB, etc., all have function libraries (functions) that can be called. Whether it is file operation or string extraction, printing, etc., there are special function libraries to choose from.
) While batch processing is a collection of commands. Whether it is a DOS command or the Windows CMD Shell or calling system or third-party provided applications, etc....
The reason it is called "batch processing" is that you uniformly use the "batch processing" method to run those commands and assignment operations that you want to call and run ~:)
) "Batch processing" does not provide you with a way to call APIs or function libraries. The system (DOS or Windows) provides you with internal, external commands or applications, and you can call whatever is available.
For example: DIR is an internal command of the system, but DIR has many parameters... (it is not provided by batch processing)
According to personal understanding, an unofficial and non-standard explanation is: Batch processing is a collection of commands, which means putting many commands or programs in a script-written file and running them uniformly.
) So, the learning points of C or VB are grammar, function libraries, APIs...
) Learning points of batch processing: How deep you understand the internal commands or external commands provided by the operating system or third-party applications, and how deep you can use them.
(For example:) REG.EXE (CMD Shell), which operates on the registry.
REG has many execution parameters that can be selected. Batch processing applies it just by calling one of its parameters,
and putting it in the batch processing to be run together with other instructions.
(For example:) Format the command, which also has many parameters. It can be run directly or called to execute in this batch processing script file.
So, to use batch processing smoothly (for example: like the version master who can do whatever he wants to do),
you must thoroughly learn the basic knowledge such as variable assignment, variable content replacement, special symbols, escape (escape characters), etc. ~:)
And how deep you have learned these basic knowledge and how thoroughly you understand them means how smoothly you can complete your will ~:)
) If you study it like programming (like modern teaching methods: high-speed something like 24-hour quick success, something like from entry to proficiency learning methods), it simply won't work ~:)
In the end, you have to come back and learn these basic knowledge again ~:)
) It is suggested to start learning from the basics. (Batch processing depends on these basics, and you can't get anywhere without these basics)
==========================================
(An example is given)
"How to judge the length of a variable?"
If you use batch processing to calculate the length of the string contained in a variable (a variable that stores a string),
the system does not directly provide a function like C or VB to calculate the length of a string,
at this time, the algorithm and a large amount of experience in processing variables accumulated on weekdays tell you:
You should use different methods such as looping through individual variables and cumulative counting to cleverly achieve the above function ~:)
(The detailed example can be found at the following address:)
http://www.cn-dos.net/forum/viewthread.php?tid=24745&fpage=1
And these are not difficult. It still applies the basic knowledge such as variable assignment, looping, accumulation (also variable assignment), delayed variables...
So, you can't get anywhere without the basic knowledge ~:)
--------------------------------
Therefore, not starting to learn from the basic knowledge when learning batch processing is equal to learning in vain ~:)
Last edited by redtek on 2006-11-15 at 09:57 PM ]