1

How to use 'cd' command to change the drive from C: to E: ? Also how to use 'cd' command to go to the directory present in other drive? I know the 'cd..' command to go back to root directory.

rsbtp
  • 47
  • 1
  • 3

2 Answers2

2

You need to use the /D option to change the drive and the directory at the same time.

cd /d e:\somedir

If you run cd /? it will show all the options it supports

jsbueno
  • 99,910
  • 10
  • 151
  • 209
0

Before the combined and then alphabetically ordered list of user and system environment variables that you get when typing set, there are actually some hidden variables, each of which get created and appear in the process PEB environment block every time that you access a new volume, on demand.

Using !peb on cmd.exe they can be seen:

Environment:  0000000002a2b0a0
    =::=::\
    =C:=C:\Users
    =D:=D:\Phone\mi mix post root
    ALLUSERSPROFILE=C:\ProgramData

Every time that you go to a drive by typing C: or D: and then cd to a directory, this variable updates. You can access the variable by enclosing it in percentage signs.

The 'command prompt' is actually the string C:\Users>. Just remember there is an implicit \ at the beginning of any directory that you now cd to or any file that you access. The implicit backslash before files and directories in the current directory is only shown in the command prompt when you're in the root directory. When you're in C:\Users however, you might get the mental notion to cd to \All Users, to provide the backslash, but this would actually look for a directory called All Users in the root directory. It is safely interpreted in this way because there cannot be a C:\Users\\All Users i.e. a blank directory name with a nested All Users in the current directory as blank file and directory names are not allowed; or alternatively, there can't be a file or directory in the current directory that contains a backslash i.e. \All Users because file and directory names are disallowed from containing the backslash delimiter.

D: is the volume, and when you access it by typing D:, it will automatically put you in the root directory D:\ if you haven't accessed the volume before in the cmd.exe instance, as D:\ is the default value of the volume cwd environment variable when it gets created when you first type D:. D:\ is the root directory of the D: volume, and using just \ will be interpreted as the root directory of the current volume, which is whatever volume you're currently viewing according to the command prompt. cd D:\directory will change the cwd D: volume variable to D:\directory and if you are viewing the D: volume then the command prompt will visibly change to it, otherwise if you are viewing a different volume, the next time you access that volume, the command prompt will have changed to reflect the new cwd.

The FileName of any file or directory (in its file object) is always the full string without the volume name i.e. the FileName of a file in C:\ (C:\file) is \file; the FileName of a directory in C:\ (C:\Users) is \Users; and the file name of the root directory is \. To confirm this, I did cd /d D:\, which causes cmd.exe to open a handle to the root directory file object, which can be seen on process explorer. By applying the _FILE_OBJECT symbol to the virtual address of the actual object in the kernel space (which process explorer reveals via presumably an IOCTL call to its driver, which reads from the process's EPROCESS handle table using the index in the handle), you can see the file name:

lkd> dt _FILE_OBJECT 0xFFFFFA802F6B18C0
nt!_FILE_OBJECT
   +0x000 Type             : 0n5
   +0x002 Size             : 0n216
   +0x008 DeviceObject     : 0xfffffa80`1a6a3cd0 _DEVICE_OBJECT
   +0x010 Vpb              : 0xfffffa80`1a6a0d40 _VPB
   +0x018 FsContext        : 0xfffff8a0`00643bc0 Void
   +0x020 FsContext2       : 0xfffff8a0`23bf5e10 Void
   +0x028 SectionObjectPointer : (null)
   +0x030 PrivateCacheMap  : (null)
   +0x038 FinalStatus      : 0n0
   +0x040 RelatedFileObject : (null)
   +0x048 LockOperation    : 0 ''
   +0x049 DeletePending    : 0 ''
   +0x04a ReadAccess       : 0x1 ''
   +0x04b WriteAccess      : 0 ''
   +0x04c DeleteAccess     : 0 ''
   +0x04d SharedRead       : 0x1 ''
   +0x04e SharedWrite      : 0x1 ''
   +0x04f SharedDelete     : 0 ''
   +0x050 Flags            : 0x40002
   +0x058 FileName         : _UNICODE_STRING "\"
   +0x068 CurrentByteOffset : _LARGE_INTEGER 0x0
   +0x070 Waiters          : 0
   +0x074 Busy             : 0
   +0x078 LastLock         : (null)
   +0x080 Lock             : _KEVENT
   +0x098 Event            : _KEVENT
   +0x0b0 CompletionContext : (null)
   +0x0b8 IrpListLock      : 0
   +0x0c0 IrpList          : _LIST_ENTRY [ 0xfffffa80`2f6b1980 - 0xfffffa80`2f6b1980 ]
   +0x0d0 FileObjectExtension : (null)

Now we know why the root directory is the only exception in showing the backslash that is inserted before accessing files or cding to directories in the current directory, because it is already in the name of the directory – it is the name of the directory, and no implicit insertion needs to take place. When you cd Users from C:\ it doesn't insert the slash before Users because it's already there as the name of the current directory, but when you cd All Users from C:\Users, it inserts a backslash between the path of the current directory and the string entered in cd, i.e. All Users. As it automatically inserts the backslash at the beginning of the string in cd, if you provide a backslash at the beginning of the string, it interprets it as the time it doesn't insert a backslash, i.e., the root directory.

Lewis Kelsey
  • 4,129
  • 1
  • 32
  • 42