Check if the command is internal in CMD

I have a team name and I need to check if this command is internal. How to do this in a script package?

+4
source share
2 answers

So, after a lot of customization, and thanks to the help of @Andriy M, it finally works.

@ECHO off CALL :isInternalCommand dir dirInternal ECHO is dir internal: %dirInternal% CALL :isInternalCommand find findInternal ECHO is find internal: %findInternal% exit /b 0 :isInternalCommand SETLOCAL MKDIR %TEMP%\EMPTY_DIR_FOR_TEST > NUL 2>& 1 CD /D %TEMP%\EMPTY_DIR_FOR_TEST SET PATH= %~1 /? > NUL 2>&1 IF ERRORLEVEL 9009 (ENDLOCAL SET "%~2=no" ) ELSE (ENDLOCAL SET "%~2=yes" ) GOTO :EOF 

OLD SOLUTION

You can use where . If this fails, the command is probably internal. If this succeeds, you will get an executable path that proves that it is not internal.

 C:\Users\user>where path INFO: Could not find files for the given pattern(s). C:\Users\user>where find C:\Windows\System32\find.exe 

EDIT: As the comments suggest, this may not be the best solution if you are looking for portability, not just research. So here is another possible solution.

Set %PATH% nothing, so HELP cannot find anything, and then run HELP on the command you are trying to verify.

 C:\Users\user>set PATH= C:\Users\user>path PATH=(null) C:\Users\user>%WINDIR%\System32\help del Deletes one or more files. DEL [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names [...] C:\Users\user>%WINDIR%\System32\help find 'find' is not recognized as an internal or external command, operable program or batch file. 

This may still fail if the team has no help.

EDIT 2: Nothing, this won't work either. Both cases return %ERRORLEVEL%=1 .

+7
source

Kichik has a good answer. However, it can give a false positive if there is an executable or batch script in the current directory that matches the specified command name.

The only way I can avoid this problem is to create a folder, which, as you know, is empty in the %TEMP% directory, and then run the test from this folder.

Here is a modified version of kichik solution that should work.

 @echo off setlocal ::Print the result to the screen call :isInternal find call :isInternal dir ::Save the result to a variable call :isInternal find resultFind call :isInternal dir resultDir set result exit /b :isInternal command [rtnVar] setlocal set "empty=%temp%\empty%random%" md "%empty%" pushd "%empty%" set path= >nul 2>nul %1 /? if errorlevel 9009 (set rtn=not internal) else (set rtn=internal) popd rd "%empty%" ( endlocal if "%~2" neq "" (set %~2=%rtn%) else echo %1 is %rtn% ) exit /b 0 

Here is a script that will simply list all internal commands, assuming HELP includes a complete list of internal commands.

Update: Both FOR and IF have special parsing rules that do not allow these commands to work if they are executed through a FOR variable or a slow extension. I had to rewrite this script to use CALL and execute the command instead of the CALL argument.

 @echo off setlocal enableDelayedExpansion set "empty=%temp%\empty%random%" md "%empty%" pushd "%empty%" for /f "delims= " %%A in ('help^|findstr /rc:"^[^ ][^ ]* "') do call :test %%A popd rd "%empty%" exit /b :test setlocal set path= %1 /? >nul 2>nul if not errorlevel 9009 echo %1 exit /b 0 
+2
source

Source: https://habr.com/ru/post/1443720/


All Articles