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)?

1

6 Answers 6

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
  • Or call home in batch files ;)
    – Joey
    Commented Dec 14, 2012 at 6:19
  • Might I point out the security risk? - I can just add malicious code to your batch file or even have a similar named file earlier in the PATH and you wouldn't have an idea. Having batch files available in path is so wrong for security. Commented Nov 19, 2014 at 11:26
  • There's nothing special about scripts. You could have the same problem with malicious code in an .exe earlier in your PATH and you also wouldn't have an idea. Commented Nov 25, 2014 at 16:15
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.

2
cd %HOMEPATH%

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

4
  • 2
    though on my computer cmd.exe defaults to c:\windows\system32 for some reason, regardless of the fact that %HOMEPATH% is the user profile.
    – barlop
    Commented Dec 14, 2012 at 7:01
  • @barlop Are you running cmd from a shortcut, the file directly, or from the run dialog? Commented Dec 14, 2012 at 16:59
  • 1
    @DavidRuhmann start..run..cmd<ENTER> on xp. But looks like I might've found what makes the difference. In the registry HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor Having a Key called Autorun, with no value. Try that. And see if yours goes to c:\windows\system32 It might be that you don't have the key at all. For example, if I rename the key to autorunn(like no autorun key at all no doubt) then the cmd prompt goes to %homepath%. If the key is called Autorun with no value, then it goes to c:\windows\system32
    – barlop
    Commented Dec 14, 2012 at 17:26
  • 1
    On a related note. In Windows 7 if you run cmd with administrative privileges it goes to c:\windows\system32 as well though I haven't checked if that's related to administrator on my comp anyway not having a profile as i haven't tried logging in as that. but i doubt it's because of that, as i've heard that in win7, unlike xp. runas administrator sticks to the same profile, which (in my case anyway) has administrative privileges, and just elevates the process to use that.
    – barlop
    Commented Dec 15, 2012 at 14:54
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.

2
  • 2
    Note that a in batch file located in directory A, but executed from directory B, %~dp0 will point to directory A, not necessarily the starting directory. Commented Dec 14, 2012 at 16:24
  • @MarcksThomas That is correct, I tried to make that point with my last note in the answer, I guess it was not clear enough. Commented Dec 14, 2012 at 16:50
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")
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.

You must log in to answer this question.

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