summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rwxr-xr-xranger/config/commands.py12
1 files changed, 9 insertions, 3 deletions
diff --git a/ranger/config/commands.py b/ranger/config/commands.py
index d177203a..4d2357f6 100755
--- a/ranger/config/commands.py
+++ b/ranger/config/commands.py
@@ -1063,7 +1063,7 @@ class bulkrename(Command):
     After you close it, it will be executed.
     """
 
-    def execute(self):  # pylint: disable=too-many-locals,too-many-statements
+    def execute(self):  # pylint: disable=too-many-locals,too-many-statements,too-many-branches
         import sys
         import tempfile
         from ranger.container.file import File
@@ -1094,8 +1094,14 @@ class bulkrename(Command):
         script_lines = []
         script_lines.append("# This file will be executed when you close the editor.\n")
         script_lines.append("# Please double-check everything, clear the file to abort.\n")
-        script_lines.extend("mv -vi -- %s %s\n" % (esc(old), esc(new))
-                            for old, new in zip(filenames, new_filenames) if old != new)
+        new_dirs = []
+        for old, new in zip(filenames, new_filenames):
+            if old != new:
+                basepath, _ = os.path.split(new)
+                if basepath and basepath not in new_dirs and not os.path.isdir(basepath):
+                    script_lines.extend("mkdir -vp -- %s\n" % esc(basepath))
+                    new_dirs.append(basepath)
+                script_lines.extend("mv -vi -- %s %s\n" % (esc(old), esc(new)))
         script_content = "".join(script_lines)
         if py3:
             cmdfile.write(script_content.encode("utf-8"))