起動すると、ファイル一覧がエディタに表示されるので、 修正して保存する。
修正した通りにリネームしてくれる。
使い方:
bulk-rename bulk-rename *.txt bulk-rename temp/
引数は、そのまま ls の引数になる。
-l とか与えると多分おかしくなるので注意
/-w+/ が引数に与えられたらはぎ取っても良さそう
#!/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()