about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorGermainZ <germanosz@gmail.com>2018-10-15 02:33:23 +0200
committerGermainZ <germanosz@gmail.com>2018-10-16 21:36:28 +0200
commiteb3daffa19abbbd7403e4a6a8fb32945fef5b3c6 (patch)
tree1027355f7a7752c4a16f00c991f8e6f9abd123b0
parent16949531f739ccf0cbf8ae543e7282de4291e165 (diff)
downloadranger-eb3daffa19abbbd7403e4a6a8fb32945fef5b3c6.tar.gz
Make bulkrename create destination directories (fixes #963)
-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"))