Re wind_7th:
First of all, both the pure DOS platform and NTs' CMD do not have the simple, direct, and powerful feature or command to save the output result of a program to a variable (here refers to an environment variable).
Secondly, we can complete the simplest saving task through a simple combination of some commands, that is, save the content of the first line of the output to an environment variable.
Under NTs' CMD, the following code can be used (what is confusing is that set /p supports redirection but not pipeline, I wonder if anyone can explain? ), of course, the for /f solution provided by Nai Wai He is also very good.
command arguments > output.txt
set /p output= < output.txt
del output.txt
Under pure DOS, the earliest solution I adopted was the scheme of pre-writing an environment declaration header file, and the specific details are in ; soon, in view of the shortcoming that this scheme must edit the header file in advance, I switched to using debug to dynamically generate this header file, realizing the fully automated self-sufficiency of the script. The code is in the programs on floors 1, 2, and 3 in . They all implement the corresponding variable operations by saving and analyzing the output of the dir command. The :init segment is the concentrated application of this principle. Later I also tried many other schemes such as edlin/date/prompt, but they all have limitations and are used in different application scenarios, and none is as universal as debug.
Finally, if you want to implement more complex variable saving, such as saving the text of a specified line, specified word, specified column, or filtering some command line special symbols such as pipeline, redirection, etc., more complex code is required.
Under NTs' CMD, internal commands such as set, for, find can be used to control the text content. And under pure DOS, the realization of this control is through more complex debug scripts plus find for the text content (see the last two codes in ); or using third-party tools such as LMOD, NSET, strings, awk, sed to implement these tasks.
The above provides many solutions, but please remember that no program or solution can perfectly and concisely and effectively handle all problems. We should first be familiar with these solutions and their advantages and disadvantages, and then choose according to the specific environment.
References:
How to assign the output of a command to a variable (SET header file)
http://www.cn-dos.net/forum/viewthread.php?tid=6318
How to let a variable store the result of a command (LMOD)
http://www.cn-dos.net/forum/viewthread.php?tid=14250
How to use the echo command to output the execution result of another command (LMOD)
http://www.cn-dos.net/forum/viewthread.php?tid=16748
How to assign the content in a txt file to a variable? (NSET)
http://www.cn-dos.net/forum/viewthread.php?tid=12413
Three batch programs for text traversal operations (later supplemented to 6)
http://www.cn-dos.net/forum/viewthread.php?tid=9588