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-01 20:06
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How can a batch file determine whether a command is built into the system? View 1,044 Replies 6
Original Poster Posted 2006-06-11 00:22 ·  中国 浙江 杭州 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
  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.
Floor 2 Posted 2006-06-11 01:05 ·  中国 河北 保定 移动
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
You can use help | find /i "%input%" to make the same determination.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 3 Posted 2006-06-11 03:27 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
21-year member
UID 40733
Gender Male
Status Offline
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.
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 4 Posted 2006-06-11 13:29 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
  Thanks to both of you. But climbing's code cannot achieve what I want.
Floor 5 Posted 2006-06-11 13:33 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
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.
Floor 6 Posted 2006-06-11 20:08 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
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 :)
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 7 Posted 2006-06-11 21:08 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
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.
Forum Jump: