A Script to Batch Rename Multiple Files Using an Editor
Python
2014-01-29 10:41 (12 years ago)

When you start the program, a list of files is displayed in the editor. You can modify and save the list.
The program will rename the files according to your modifications.
Usage:
bulk-rename bulk-rename *.txt bulk-rename temp/
The arguments are passed directly to the ls command.
Be careful as using options like -l might cause issues.
It seems safe to strip any /-w+/ patterns if they're given as arguments.
#!/usr/bin/env python # -*- coding: utf-8 -*-import subprocess import sys import os
TEMP_FILE_NAME = '/tmp/bulk-rename-file-names' EDITOR = 'vim'
def main(): command_result = subprocess.check_output(["ls", "-1"] + sys.argv[1:]) file_names = command_result.splitlines()
with open(TEMP_FILE_NAME, 'w') as fp: fp.write('\n'.join(file_names)) subprocess.check_call([os.environ.get('EDITOR', EDITOR), TEMP_FILE_NAME]) with open(TEMP_FILE_NAME, 'r') as fp: destination_file_names = fp.readlines() for source, destination in zip(file_names, destination_file_names): if source and destination: destination = destination.strip() if source != destination: print "{} -> {}".format(source, destination) os.rename(source, destination) os.unlink(TEMP_FILE_NAME)if name == 'main': main()
Please rate this article (No signup or login required)
Currently unrated
Related posts
How to Add "Copy Full File Path to Clipboard" to Finder's Services Menu
Replacing Version Numbers with Python re (Regex Replacement Only Within Parentheses)
Fixing the Issue of Separated Voiced Marks in Folder Names on Mac When Outputting from Python
Building a single-file portable Python script with embedded dependency management
The author runs the application development company Cyberneura.
We look forward to discussing your development needs.
We look forward to discussing your development needs.