Is there a command to change path from current location to default directory, in Windows command prompt?

21
2013-11
  • CRoshanLG

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

  • Answers
  • Nicole Hamilton

    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
    
  • slhck
    cd %HOMEPATH%
    

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

  • David Ruhmann

    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.

  • Karan

    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.

  • slhck

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

  • Related Question

    Change to default start folder for Windows XP command prompt
  • Pops

    My new work computer's command prompt automatically brings up "H:\>" when I open it. Is there a way for me to change that safely? Specifically, to "C:\foo\bar\Something Else"? I found the method of changing the Autorun value through Google, but it came with a warning that it "might affect the functionality of batch scripts."

    Bonus note: I was originally getting at this issue in this question, but I didn't ask very directly. The answer I ended up accepting was really good, so I wanted to leave it there.


  • Related Answers
  • Jared Harley

    The command prompt automatically starts in the Home Directory specified in your user profile.

    A "workaround" to avoid messing with scripts and such is to create a new shortcut to cmd.exe.

    • Create a new shortcut to %systemroot%\system32\cmd.exe
    • Once the shortcut is created, right-click and select Properties
    • In the Shortcut tab, set the "Start in" field to the path you want to start in, ie, C:\foo\bar\Something Else
    • Click OK and try your new shortcut!
  • harrymc

    First method : Execute a change-directory command when cmd starts

    From "How to change the default startup directory for Command Prompt?":

    Click Start, Run, and type Regedit.exe.
    Navigate to the following branch:

    HKEY_CURRENT_USER\Software\Microsoft\Command Processor
    

    In the right-pane, double-click Autorun and set the startup folder path as its data, preceded by "CD /d ".
    If Autorun value is missing, you need to create one, of type String in the above location.

    Example: To set the startup directory to F:\Windows, set the Autorun value data to "CD /d F:\Windows".

    Second method : Change user's home folder

    Right-click "My computer" and choose Manage.
    Select "Local users and Groups" and then Users.
    Double-click the user in question and go to the Profile tab.
    Set in "Home folder" the required directory as the value of "Local path" and do OK.