Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to make a batch file that, when run, looks and works exactly the same as a command prompt, except that I can do something with everything the user enters (e.g. logging it). How can I do this?

share|improve this question

1 Answer 1

The following short batch file will function the same as a prompt:

@echo off
:start
set inp=
set /P inp=%cd%^> 
:: do evil stuff with %inp% here (optional)
%inp%
goto start

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 = sign.
  • %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 variable.
  • %inp%executes the content of the inp variable.
  • 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.)

share|improve this answer
    
(0) I added a line of code. (1) What do you mean by “the inp variable will be visible to the person typing”? (2) Your batch file assumes that the prompt should be $P$G$S (current path, greater than, and space), even if the user normally has it set to something else (and I believe that the default is $P$G, without the trailing space). And if the user tries to change the prompt while talking to the batch file, there will be no effect. –  Scott Mar 14 '13 at 5:45
    
(3) If the user runs another batch file, your batch file goes away. When the new batch file terminates, the user will be back at the real Command Prompt. (4) If the user types a multi-line command, your batch file croaks (and this is legal at the real Command Prompt). –  Scott Mar 14 '13 at 5:46
    
I didn't know about #3. #1: if you do set which prints all variables. #0: I saw, and I think I actually had that in the second version that I don't remember. #4: I don't think I ever tried a multi-line command typed in :\. I'll look into #2 later. –  Riking Mar 14 '13 at 5:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.