6

I am trying to modify my CMD, to show only the current directory name dynamically, like:

Desktop $

When I switch the folder, it must be updated.

It is not required to be code in a pure batch file. It may depend on any external commands, Cygwin Bash, etc.

@echo off
set a=bash -c "pwd | sed 's,^\(.*/\)\?\([^/]*\),\2,'"
%a%
cmd

Outputs

_test-et
Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. Tüm hakları saklıdır.
>>

But

>> prompt %a%

gives

bash -c "pwd | sed 's,^\(.*/\)\?\([^/]*\),\2,'"

4 Answers 4

1
+100

Using Microsoft's PowerShell, the prompt is produced by a function that can do anything at all.

A prompt function that does what you want is:

function prompt { (Split-Path -Leaf $pwd) + '$ ' }

For more information, see:

1
  • It is time to switch off CMD age and switch to PowerShell :) Thanks. Difficult to give up habituation but it is more likely cygwin, cd ~ works on PowerShell, but not in cmd.
    – guneysus
    Nov 12, 2013 at 9:57
4

I believe you can use the prompt command.

PROMPT [text]

  text    Specifies a new command prompt.

Prompt can be made up of normal characters and the following special codes:

  $A   & (Ampersand)
  $B   | (pipe)
  $C   ( (Left parenthesis)
  $D   Current date
  $E   Escape code (ASCII code 27)
  $F   ) (Right parenthesis)
  $G   > (greater-than sign)
  $H   Backspace (erases previous character)
  $L   < (less-than sign)
  $N   Current drive
  $P   Current drive and path
  $Q   = (equal sign)
  $S     (space)
  $T   Current time
  $V   Windows version number
  $_   Carriage return and linefeed
  $$   $ (dollar sign)

If Command Extensions are enabled the PROMPT command supports
the following additional formatting characters:

  $+   zero or more plus sign (+) characters depending upon the
       depth of the PUSHD directory stack, one character for each
       level pushed.

  $M   Displays the remote name associated with the current drive
       letter or the empty string if current drive is not a network
       drive.

Example:

setx PROMPT $P$S$$$S

Update

Create a batch file containing the code below and run.

@echo off

set root=%~p0
for %%F in ("%root%.") do set "ParentFolder=%%~nF"
setx PROMPT %ParentFolder%$S$F$S

Result is:

enter image description here

3
  • Can you see above CURRENT DIRECTORY variable? Let's test it. Open cmd, and set prompt to: set prompt=$B$C$D$E$F$G$H$L$N$P$Q$S$T$V$_$$ which contains all variables except $+ and $M. But still I can not see still current dir, but give |(Pzt 04.11.2013←)<CC:\Users= 17:44:02,69Microsoft Windows [Version 6.3.9600] Why? Is there a huge bug on cmd.exe?
    – guneysus
    Nov 4, 2013 at 15:44
  • @guneysus I updated my answer. :-)
    – stderr
    Nov 4, 2013 at 17:54
  • Thanks but i must change it, because current folder is not dynamic and with setx it is systemwide :) It shoud be set not setx In addition i will try replace cd function, to update it dynamically.
    – guneysus
    Nov 4, 2013 at 18:49
2

Based on the wikipedia entry for AUTOEXEC.BAT prompt $P$G will do what you want. This is a holdover from good old DOS. God only knows why something like this was never made a default since the early days of ... DOS3.3 or so.

1
  • This is not what OP wants. It gives the complete path. OP wants only the last part of the current directory (without its parent-path). And it needs to change when changing the directory. It's not possible with the current prompt command. At least not in cmd.exe.
    – Rik
    Nov 30, 2013 at 0:43
0

I like knowing the full context of where I am in the console, but the deeper you go down the directory tree, the longer the "Current drive and path", so to have the best of both worlds, I have the full drive and path followed by a CR then a ">".

This is what I now do on all Windows machines I get access to:

SETX PROMPT "%username%@%computername% $P$_$G"

Which gives you a prompt showing the username, hostname, full path to the current folder, and a whole new line for typing:

USERNAME@HOSTNAME C:\PATH\TO\CURRENT\FOLDER
>

SETX stores the variable (in this case, "PROMPT") permanently (through reboots, until you change it).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .