0

I want to change the command prompt string in a windows command prompt?

For example.

C:\current\path -> $CustomPrompt>

If possible, I would prefer the solution to make use of the winapi. I already looked at some of the console functions, but I cannot find any that I would assume wold manipulate the prompt string? Does anyone know how to do this?

14
  • What's wrong with just setting the PROMPT environment variable? Commented Jul 18, 2014 at 7:24
  • I'm writing a C++ program so, if possible, I would like to stick to using c++. Oh, as a correction, PROMPT isn't a environment variable, it's a command. Commented Jul 18, 2014 at 7:26
  • @user246694 Your are wrong. PROMPT is both an environmental variable and DOS/Windows command which sets this variable. Commented Jul 18, 2014 at 7:46
  • How am I wrong, if I am indeed referring to the command? You stated yourself that it is a command that sets the variable. Commented Jul 18, 2014 at 7:50
  • 2
    @user246694 you were wrong when you stated that 'PROMPT isn't a environment variable' Commented Jul 18, 2014 at 7:55

1 Answer 1

1

Command Prompt String is definied as environmental variable PROMPT. You can modify this variable using setenv() function from cstdlib :

#include <stdlib.h>

//...

setenv("PROMPT", "$A$A", true);

This will, for example, set prompt string to '&&' (double ampersand). For more interesting examples check this out.

EDIT: There is a way to achieve this without need to restart command interpreter. Create following batch file:

@echo off
break off
title custom command prompt
color 0a
cls

:cmd
 set /p cmd=command:

 %cmd%
 echo.
 goto cmd

Lets name it "change_prompt.bat" Then, in your c++ code execute the batch file:

system("change_prompt.bat");

As a result, prompt will look like this:

new prompt

As you can see, this changes:

  • prompt window title
  • prompt color
  • prompt string
Sign up to request clarification or add additional context in comments.

8 Comments

unless this is a silly rep-harvesting maneuver, the OP should note that this only affects the prompts of command interpreter instances created (later on) by this process, and remove the "solution" selection
@Cheersandhth.-Alf Corrected. Well, I am aware this is not a good choice. If you have some better one, feel free to post it.
re the header just use <stdlib.h>. re the function, i'm sorry i didn't notice that it's Posix, not C++ std. so the correction you did was in the wrong direction, just remove the std:: (it's not a standard function), use stdlib.h, and note that it's a Posix function.
I was thinking maybe to just settle for the std::system from <cstdlib> and call std::system("Prompt myprompt");
@agentNil: no, that would only affect the command shell started by the call to system, which would exit without doing anything else.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.