Batch file 如何";隐藏“;来自批处理文件的文本?

Batch file 如何";隐藏“;来自批处理文件的文本?,batch-file,text,plugins,cursor-position,Batch File,Text,Plugins,Cursor Position,我想知道是否有解决方案: Main.bat: @echo off goto 'input' : 'input' cls set "inp=" set /p inp=What would you like to do? set firstresponse=%inp:~0,5% if %firstresponse%==help goto 'help' pause if /I %firstresponse%==check set firstresponse=dir && set

我想知道是否有解决方案:

Main.bat:

@echo off
goto 'input'

: 'input'
cls
set "inp="
set /p inp=What would you like to do?
set firstresponse=%inp:~0,5%
if %firstresponse%==help goto 'help'
pause
if /I %firstresponse%==check set firstresponse=dir && set                             
executeparttwo=%inp:~5%
if /I %firstresponse%==remov goto 'remove'

%firstresponse%%executeparttwo%
pause
goto 'input'

: 'remove'
set "firstresponse=" && set firstresponse=%inp:~0,6%
if /I %firstresponse%==remove set firstresponse=del
set executeparttwo=%inp:~6%
%firstresponse%%executeparttwo%
pause
goto 'input'

: 'help'
cls
echo Check = Dir in regular command prompt, checks a directory.
echo Remove = del in regular command prompt, deletes something.
pause
goto 'input'
如果用户键入了无效命令,它将显示CMD的功能('command'未识别…)

我想做的是将CMD invalid命令文本替换为我自己的文本,就像“command”是无效命令一样,但要做到这一点,我需要“隐藏”CMD文本(因为如果用户键入无效命令,它将不会向他显示“自定义消息”)

我尝试使用一些批处理插件,如batbox、CursorPos等。。。替换光标位置,但我没有得到我想要的。因此,如果有人有一个解决方案,我将非常感激

  • 祝你度过愉快的一天,谢谢你的阅读
      拆分命令和参数并不理想,有一种更简单、更安全的方法。此外,每个命令自己的子例程的方法是次优的(尤其是在添加越来越多的命令时)


      (有经验的批处理用户注意:是的,我知道有可能进行一些“代码注入”)

      您可以检查命令是否与此-一起存在。此外,当命令不存在时,errorlevel设置为9009。请使用
      选项
      命令或其他将最终用户限制为仅有效条目的方法!感谢您的回答,正如您在我的帖子中所看到的那样,%firstresponse%%executeparttwo%表示用户键入的内容将作为命令执行,如果命令无效,它将打印未识别的“命令”。。。我想做的是“隐藏”这条信息,并用我自己的信息替换它-祝您有个美好的一天!正如您在我的评论中所看到的,如果您将脚本编码为只接受有效的条目/命令,那么您就不需要隐藏“未识别”消息,因为不会有任何消息@Compo我尝试了
      选项
      命令,但它不允许您键入长命令,例如带有语法的
      del
      ,或自定义命令
      apt get update
      等。。。它只接受一个Letter,谢谢你的回答-祝您有个美好的一天!伟大的成功了!但是,当我创建自己的自定义命令,如“apt get update”或任何其他包含“space”的命令时,它将不起作用。如果有任何解决方案,我将非常感激-祝你今天愉快,谢谢!我想这是因为在我所知道的每种语言中,命令都只是一个单词(加上开关和/或参数)。(即使像
      wmic
      netsh
      这样看起来像两个(或多个)单词的命令,实际上也是一个单词的命令,可以解析第二个(…)“命令部分”因此,请将其视为一个带参数的一个单词命令-请参阅我编辑的答案您的代码真的帮助了我再次表示感谢,您回复的最后一行让我感到疑惑,那么您能告诉我如何通过输入“注入”代码吗?
      @echo off
      call :commandlist  REM build translation table
      
      :input
      REM get input line:
      set /p "commandline=Enter Command: "
      REM split to command and parameters
      for /f "tokens=1,*" %%a in ("%commandline%") do (
        set "command=_%%a"
        set "params=%%b"
      )
      REM check for valid command:
      set _|findstr /bi "%command%=" >nul || (
        echo invalid command: '%command:~1%'.
        goto :input
      )
      REM execute the command:
      call %%%command%%% %params%
      goto :input
      
      :Commandlist
      set "_check=dir /b"
      set "_remove=del"
      set "_help=:help"
      set "_where=call echo %%cd%%"
      set "_change=cd"
      set "_apt-get=:apt"
      set "_bye=exit /b"   'secret' exit command  ;)
      goto :eof
      
      :help
      echo Check = Dir in regular command prompt, checks a directory.
      echo Remove = del in regular command prompt, deletes something.
      echo Where = echo %%cd%% in regular command prompt, print working folder.
      echo Change = cd in regular command prompt, change working folder
      goto :eof
      
      :apt 
      if /i "%~1" == "update" echo updating... & goto :eof
      if /i "%~1" == "whatever" echo whatevering... & goto :eof
      echo invalid command: '%command:~1% %1'
      goto :eof