Home > OS >  Batch File, get each word of variable and set for each arguments
Batch File, get each word of variable and set for each arguments

Time:06-07

Basicily everything is in the title.

:console_exec
set /p "cmd=->"
set "console_exec=True"
goto :main

When I pass an input to to set /p it is reading as entire string In :main

:main
if "%cmd%"=="say" (
    set "_string="
    goto :say
    )

When I do "say hi" in the set /p it is reading it as if "say hi" == "say" but i want "hi" to be the next argument passed like %~2, any help is appriecieted. Thanks.

CodePudding user response:

No need for if, when you use the given capabilities of the language. Just try to jump to the "command" label. Redirect the errormessage (with non-existing labels). You can even give an own message then:

@echo off
setlocal 
:console_exec
set /p "cmd=->"
2>nul call :%cmd% || echo invalid command
goto :console_exec

:say
echo --- %1
REM echo --- %*  // for more than one word
  • Related