WThirtyTwoBrowseNetDrives

The code on this page lets you access a list of your MS Windows drives, to navigate to any of them in DiredMode.

This feature is particularly useful for `^’ (‘dired-up-directory’) in Dired. Assuming there is no objection, I have added it to Dired+, modifying it slightly. The code here was apparently contributed anonymously. If you wrote it, let us know who you are. ;-) And if you prefer that this not be added to Dired+, let me know. Thx – DrewAdams


First version:

(defun netdir()
  (interactive)
  (require 'widget)
  (let* ((drvL))
    (with-temp-buffer
      (let ((out (shell-command "net use" (current-buffer))))
	(if (eq out 0)
	    (while (re-search-forward "[A-Z]: +\\\\\\\\[^ ]+" nil t nil)
	      (setq drvL (cons (split-string (match-string 0)) drvL)))
	    (error "Unable to issue the NET USE command"))))
    (pop-to-buffer "*NET DIR LIST*")
    (erase-buffer)
    (widget-minor-mode 1)
    (mapcar
     (lambda (x)
       (lexical-let ((x x))
	 (widget-create 'push-button
			:notify (lambda (widget &rest ignore)
				  (kill-buffer (current-buffer))
				  (dired (car x)))
			(concat (car x) "  " (cadr x))))
       (widget-insert "\n"))
     (reverse drvL))))

Second version:

Specify your local drives using variable ‘localdrives’.

Uses ‘copy-sequence’. The reason is this: drvL points to the first cell in ‘localdrives’. Then we append more items to the front of the list. Then we use ‘sort*’, which is destructive. See DestructiveOperations. It changes the order of the cells in the list. So next time you use ‘localdrives’, the first cell will be the same, but the rest of the cells might be different.

(setq localdrives '(("A:" "Floppy")("C:" "Home")("D:" "Datadisk")("E:" "CD")))

(defun netdir()
  (interactive)
  (require 'widget)
  (let ((drvL (copy-sequence localdrives)))
    (with-temp-buffer
      (insert (shell-command-to-string "net use"))
      (goto-char (point-min))
      (while (re-search-forward "[A-Z]: +\\\\\\\\[^ ]+" nil t nil)
	(setq drvL (cons (split-string (match-string 0)) drvL))))
    (switch-to-buffer "*NET DIR LIST*")
    (erase-buffer)
    (widget-minor-mode 1)
    (mapcar
     (lambda (x)
       (lexical-let ((x x))
	 (widget-create 'push-button
			:notify (lambda (widget &rest ignore)
				  (kill-buffer (current-buffer))
				  (dired (car x)))
			(concat (car x) "  " (cadr x))))
       (widget-insert "\n"))
     (sort* drvL 'string-lessp :key 'car)))
  (goto-char (point-min)))


(global-set-key [C-M-return] 'netdir)

(defun show-net-connections ()
      (interactive) 
      (shell-command "net use"))

Redefinition of standard Dired command (`^’). Calls ‘netdir’ when going up from the root of a drive:

(defun dired-up-directory (&optional other-window)
    "Run Dired on parent directory of current directory.
Find the parent directory either in this buffer or another buffer.
Creates a buffer if necessary."
    (interactive "P")
    (let* ((dir (dired-current-directory))
	   (up (file-name-directory (directory-file-name dir))))
      (or (dired-goto-file (directory-file-name dir))
	  ;; Only try dired-goto-subdir if buffer has more than one dir.
	  (and (cdr dired-subdir-alist)
	       (dired-goto-subdir up))
	  (progn
	    (if other-window
		(dired-other-window up)
	      (dired up))
	    (dired-goto-file dir))
	  (netdir)))) ;; Get list of drives

CategoryWThirtyTwo CategoryDirectories