I found this function by xah to do this but it also looks at the parent directories. Ideally, this would just look at the filename not including the parent name or parent directories. Something along the lines of this regex:
where it would just take group \2 for replacing the spaces with hyphens instead of the whole path.
(defun xah-dired-rename-space-to-underscore () "In dired, rename current or marked files by replacing space to underscore _. If not in `dired', do nothing. URL `http://ergoemacs.org/emacs/elisp_dired_rename_space_to_underscore.html' Version 2017-01-02" (interactive) (require 'dired-aux) (if (equal major-mode 'dired-mode) (progn (mapc (lambda (x) (when (string-match " " x ) (dired-rename-file x (replace-regexp-in-string " " "_" x) nil))) (dired-get-marked-files )) (revert-buffer)) (user-error "Not in dired.")))