How to accept user input command in batch file?

5,828

Use set /P to get the user's input into a variable, then run the variable as if it's a command:

@echo off
set /P CommandVar=Command? 
%CommandVar%

From set /?:

The /P switch allows you to set the value of a variable to a line of input entered by the user. Displays the specified promptString before reading the line of input. The promptString can be empty.

Share:
5,828

Related videos on Youtube

user3152251
Author by

user3152251

Updated on September 18, 2022

Comments

  • user3152251
    user3152251 over 1 year

    I would like to write a batch file in which the user input would be run as a command, essentially functioning like command prompt.

    Example:

    batch file is run:
    user input: echo hello
    program output: hello
    

    How would I implement this?

  • LPChip
    LPChip over 9 years
    Oh wow, I never knew you could actually execute a command like that. Thanks. I learned something today. :)