Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
    
There is a great answer here.. superuser.com/a/699052/33955 –  Anindya Chatterjee Apr 5 at 21:19

5 Answers 5

up vote 3 down vote accepted

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
share|improve this answer
    
Or call home in batch files ;) –  Joey Dec 14 '12 at 6:19
cd %HOMEPATH%

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

share|improve this answer
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 Dec 14 '12 at 7:01
    
@barlop Are you running cmd from a shortcut, the file directly, or from the run dialog? –  David Ruhmann Dec 14 '12 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 Dec 14 '12 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 Dec 15 '12 at 14:54

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.

share|improve this answer
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. –  Marcks Thomas Dec 14 '12 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. –  David Ruhmann Dec 14 '12 at 16:50

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.

share|improve this answer

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")
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.