![]() |
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-01 19:02 |
48,037 topics / 350,122 posts / today 2 new / 48,249 members |
| DOS批处理 & 脚本技术(批处理室) » How can a batch file determine whether a command is built into the system? |
| Printable Version 1,043 / 6 |
| Floor1 namejm | Posted 2006-06-11 00:22 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
Under XP, I want a batch file to automatically determine whether a command entered is built into the system. How should this be handled?
My first thought was to judge it by the output of "%input% /?". The specific idea is: output the contents of %input% /? to a text file, then use the find command to check whether that text file contains the string "is not recognized as an internal or external command". If it does, then %input% is not a command built into the system. But this has a drawback: it creates a temporary file. That is what I am very dissatisfied with. Of course, doing it this way also adds code to delete the temporary file, so it does not look concise enough. From the above solution, I then thought of another question. If this question can be solved well, then all the problems will be easily resolved. That is: when using a pipe symbol, how can the output of some command be used as the object for the find command to search for a specific string? dir /? |find "is not recognized as an internal or external command" This statement does not work. |
|
| Floor2 Climbing | Posted 2006-06-11 01:05 |
| 铂金会员 Posts 2,753 Credits 6,962 From 河北保定 | |
|
You can use help | find /i "%input%" to make the same determination.
|
|
| Floor3 无奈何 | Posted 2006-06-11 03:27 |
| 荣誉版主 Posts 356 Credits 1,338 | |
|
Try this:
%input% 2>nul || echo Command error or no such command. If it's in a script, use if ERRORLEVEL value to judge it; see if /? for details. |
|
| Floor4 namejm | Posted 2006-06-11 13:29 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
Thanks to both of you. But climbing's code cannot achieve what I want.
|
|
| Floor5 3742668 | Posted 2006-06-11 13:33 |
| 荣誉版主 Posts 718 Credits 2,013 | |
|
if %errorlevel% == 9009 do command
Refer to the method here. This has nothing to do with whether the command is built into the system, but rather with %cd% and %path% as well as internal commands. As for the second question, dir /? |find "is not recognized as an internal or external command this line merely uses the output of dir /? as the object being searched. If you want to use it as the search object, you might try using for to obtain the output handle and then operate on it. |
|
| Floor6 willsort | Posted 2006-06-11 20:08 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
Re Ups:
Brother Wunaihe's solution is the royal road and the proper way, and also the first method one would think of. This made me think: although CMD does not support handle pipes like command 2|0 find "'pp'", so the error handle output of a command cannot directly flow through a pipe into the input handle of another command, is it possible to copy error handle 2 to standard output handle 1 first, and then make the error output flow into the pipe? Simple testing proves that handle copying seems to work only for redirection, not for pipes. Still, I feel somewhat in the dark about the use of handle copying, and it is hard to find more in-depth materials, so perhaps there may yet be a new breakthrough. In the end, it seems the only option is to use a file as a substitute for a pipe: %input% 2> errfile & find "'%input%'" <errfile>nul && echo Invalid command! Also, the for-based method of obtaining output handles mentioned by brother 3742668 does not seem to have anything to do with the flow of the standard error handle, does it? But brother namejm's dislike of temporary files is rather similar to brother 3742668's style :) |
|
| Floor7 3742668 | Posted 2006-06-11 21:08 |
| 荣誉版主 Posts 718 Credits 2,013 | |
|
Hmm, it seems I was thinking too simply about using for to read an output stream. But as for the previous question, just using %input% 2>nul || do command does not seem able to distinguish the case where the %input% command exists but produces an error, so I still advocate using errorlevel to judge it. As for question 2, if you do not want to create a temporary file, I am afraid the only way is to construct the string yourself, but I am still very doubtful about whether that is feasible. Besides, it feels like treating the output stream as the search string rather than the searched string is really rather meaningless.
Also, I am not particularly hostile to the creation of temporary files either (mainly I was considering it from other people's point of view, worried that others might dislike temporary files). After all, compared with the disk fragmentation caused by disk swapping during actions like booting the system, opening IE, playing online games, etc., the temporary files produced by batch files can be completely ignored. On the contrary, I am rather helpless with things like for and if, which easily lead to errors; if I can avoid using them, I do. On this point, willsort, you really wronged me. |
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |