The following short batch file will function the same as a prompt:
set /P inp=%cd%^>
:: do evil stuff with %inp% here (optional)
With the following caveats: the
inp variable will be visible to the person typing, and if you do Control+C, it will ask you whether you want to terminate the batch file.
Walkthrough of code:
@ – hides the next command from the console
echo off – hides all following commands from the console
set inp= – sets the
inp variable to a null string, because the following command will leave
inp unchanged if the user just types Enter.
set /P inp= – Set /P will set a variable based on user input, with the prompt being what's after the
%cd%^> (note the space at the end) – The current directory, and the
> character (for some inane reason,
^ is the escape character in Command Prompt). This is used as the prompt for the
%inp% – executes the content of the
goto start – loops back up to the top.
Obviously, you have to ask yourself why this is possible without some kind of
exec() call or similar. The reason is that each line of batch is 'compiled' at runtime - variables are substituted into the commands. You can see this when you do a
for loop where you use
set /A ("arithmetic") to increment a variable each time with
% - you get the same value every time. (For expected behavior, use
var instead of
%var% in set /A except for constants.)