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?

jax
  • 728
  • 1
  • 11
  • 31
  • What's wrong with just setting the [`PROMPT` environment variable](http://stackoverflow.com/questions/12028372/how-to-change-command-line-prompt-in-windows)? – Some programmer dude Jul 18 '14 at 07: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. – jax Jul 18 '14 at 07:26
  • @user246694 Your are wrong. PROMPT is both an environmental variable and DOS/Windows command which sets this variable. – Kao Jul 18 '14 at 07: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. – jax Jul 18 '14 at 07:50
  • use the `prompt` command, unless you want to change the default at the user or system level, in which case use the System control panel applet. – Cheers and hth. - Alf Jul 18 '14 at 07:50
  • Like I said, I would prefer if it to done in C++. What is the "System control panel applet.? – jax Jul 18 '14 at 07:52
  • 2
    @user246694 you were wrong when you stated that 'PROMPT isn't a environment variable' – Kao Jul 18 '14 at 07:55
  • Well, context wise, I was not. I might not have known that there was indeed a variable with the same name that were related to each other. But then again, I will still only referring to the command. Now, however, I have learned that there is environment variable also with the same name. Thank you. – jax Jul 18 '14 at 07:59
  • Why would you write a program to do something that the system already has fully functional tools for? – David Heffernan Jul 18 '14 at 09:20
  • That actually isn't that main feature of the program. The program actually does something else. But I wanted to change the prompt when the program is initiated. Thought it would neat to have the program do that a la the "netsh" command. – jax Jul 18 '14 at 09:40
  • There's no supported way to do this to your parent command interpreter. The console functions won't help: they're for manipulating the console, not the command interpreter. – Harry Johnston Jul 19 '14 at 08:34
  • From what point of view do you want to do this? A CLI application running in said command prompt? a parent process creating a command prompt/console window? a random 3rd party process? – Deanna Jul 30 '14 at 09:40
  • I'm still not clear on what you mean? I believe i'm trying to do the first option. – jax Jul 31 '14 at 00:13
  • @agentNil please see edits to my answer – Kao Jul 31 '14 at 06:05

1 Answers1

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
Kao
  • 7,225
  • 9
  • 41
  • 65
  • 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 – Cheers and hth. - Alf Jul 18 '14 at 08:02
  • **-1** `setenv` should be `std::setenv` when using ``, the current code is not guaranteed to compile. however, using `` is not a good choice. also, as earlier commented, this approach only works for command interpreter instances started from this process (if it works at all, I'm not clear about that). – Cheers and hth. - Alf Jul 18 '14 at 08:05
  • @Cheersandhth.-Alf Corrected. Well, I am aware this is not a good choice. If you have some better one, feel free to post it. – Kao Jul 18 '14 at 08:09
  • re the header just use . 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. – Cheers and hth. - Alf Jul 18 '14 at 08:12
  • I was thinking maybe to just settle for the std::system from and call std::system("Prompt myprompt"); – jax Jul 18 '14 at 09:33
  • @agentNil: no, that would only affect the command shell started by the call to `system`, which would exit without doing anything else. – Harry Johnston Jul 19 '14 at 08:29
  • How then are programs like netsh or git able to change the prompt? That's what I'm trying to mimic. – jax Jul 29 '14 at 19:44
  • @agentNil I edited an answer by adding new solution. Hope this will help. – Kao Jul 30 '14 at 09:26
  • @Kao Okay, i'll check out tomorrow. Thanks. Looks promising. – jax Jul 31 '14 at 06:51