Search code examples
windowsemacsdired

Is there a way to list drive letters in dired?


On windows, how could I open a dired buffer showing all drive letters. When you do C-x d you should always provide a directory, but I want to start at the drive letters level instead of a root directory of a particular drive.

If no standard solution exists, do you have one (an extension to dired ?) ? or links to articles on the subject ?


Solution

  • In dired you can only view directories, and since no directory exists which contains your drive letters, you can't see a list of them.

    To do this you'd have to write an emacs-lisp extension for dired.

    AFAIK there's no existing extension, however, a call to wmic can give you a listing of drive letters and volume names, which would be a good starting point.

    The wmic command:

    wmic logicaldisk get caption,drivetype,providername,volumename
    

    Calling it from emacs-lisp and getting the result as a string.

    (let (sh-output volumes)
      (setq sh-output (shell-command-to-string "wmic LogicalDisk get Caption,DriveType,ProviderName,VolumeName"))
    )
    

    Will give you a list of the volumes (DriveType : 3 = HDD, 4 = Network Mapping, 5 = Optical.)

    However, you can't get dired to recognize a buffer with this output, so you'd need to create a major mode for browsing windows volumes, which would show this listing and bind RET to find the drive letter on the current line and do a dired at it's root.

    If you just want the drive letters listed...

    (let (sh-output volumes)
      (setq sh-output (shell-command-to-string "wmic LogicalDisk get Caption"))
    )
    

    Will do that.