2

Is there a command to change the current directory to default directory? (I'm not asking about permanently changing the default directory, here!)

For example, if the current directory is "..\xyz" and my default command prompt directory is D:\Abc, is there a way to navigate directly to D:\Abc (without giving >cd D:\Abc)?

Andrea
  • 1,536
CRoshanLG
  • 229

6 Answers6

3

There's no option built into cd to do this but you could certainly create a .cmd script file that does it. For example, you could put this into a home.cmd file somewhere on your search PATH and go to D:\Abc just by typing home:

@ echo off
cd /D D:\Abc
3

There's no in-built command, but why not create your own, for example dd (Default Directory)? Just save the following command in a batch/script file in any location, say C:\Macros.bat or C:\Macros.cmd:

@doskey dd=cd /d D:\Abc

Now in the registry (Regedit.exe) navigate to:

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor

Create a string value (REG_SZ) or expandable string value (REG_EXPAND_SZ) called AutoRun and set it to C:\Macros.bat (or .cmd as the case may be).

The same AutoRun value can also be added to:

HKEY_CURRENT_USER\Software\Microsoft\Command Processor

Any commands specified in the HKLM AutoRun value will run before those in the HKCU counterpart. See cmd /? for more.

Now whenever you open a cmd.exe instance/window, Macros.bat (or .cmd) will be executed automatically and the DOSKey command alias will be (re)created as a result. So you can simply type your new command dd to jump to the specified default directory.

Karan
  • 57,289
2
cd %HOMEPATH%

Although creating a batch file as Nicole mentions is probably easier.

slhck
  • 235,242
Benedict
  • 121
2

Well, if your command line started out in D:\Abc and you only need to be in ..\xyz for a few commands, use the pushd and popd commands. Here is a batch script example:

@echo off
echo.Im at this directory: %CD%
pushd "..\xyz"
echo.Im now at this directory: %CD%
popd
echo.Im back at this directory: %CD%
pause

Otherwise you will need to use the cd command to change the current directory. This only works for a batch script.

@echo off
echo.Im at this directory: %CD%
cd "..\xyz"
echo.Im now at this directory: %CD%
cd "%~dp0"
echo.Im back at this directory: %CD%
pause

Note that "%~dp0" will take you back to the original directory in which the batch script started.

1

This is a universal command to create the "home" command tool on any Windows machine.

Press WinR and enter the following command:

CMD /C ((SETLOCAL ENABLEDELAYEDEXPANSION &ECHO ^@ECHO OFF &ECHO ECHO Changing to "home" directory... 1^>CON&ECHO CD /D %%USERPROFILE%%)1>"%SYSTEMROOT%\home.cmd")
slhck
  • 235,242
1

I know I'm late to the game but I would like to post my solution for anybody that stumbles across this question (Like me). I created a simple how-to that nobody else has mentioned and it's easier and more direct IMO. Check it out: Spiceworks - Change Default Directory

  1. Open the Command Prompt's Location.

    On the start screen / menu, type in "cmd", right-click it and select "Open File Location".

  2. Open the Command Prompt Properties and change the "Start In" Property.

    Right-click on the "Command Prompt" icon, select "Properties", and edit the "Start In" property to your desired path.

karel
  • 13,706