Last active
June 25, 2018 23:26
-
-
Save jerblack/4bb80a18de88775242e106cca47eb0cf to your computer and use it in GitHub Desktop.
Append and Prepend, a pair of simple scripts to append or prepend text to a set of filenames
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
help = """Simple script to append text to specified files before the extension | |
usage: append (-n)(-i) "text to append" files | |
-n: dry-run, will show before and after filenames without making changes | |
-i: ignore file extensions and just append to the end of the filename | |
append " (2017)" *.docx | |
'outline.docx' -> 'outline (2017).docx' | |
append -i ".(DRAFT)" * | |
'plans.for.2019' -> 'plans.for.2019.(DRAFT)'' | |
append \ \(Finished\ reading\) *.epub | |
'amazing stories.epub' -> 'amazing stories (Finished reading).epub' | |
""" | |
import sys | |
from sys import argv as args | |
import os | |
if len(args) < 3: | |
print(help) | |
sys.exit(0) | |
dry_run = False | |
extensions = True | |
action = 'Renaming' | |
if '-n' in args: | |
action = 'Would have renamed' | |
print('Dry-run enabled. No files will be changed.') | |
dry_run = True | |
args.remove('-n') | |
if '-i' in args: | |
print('ignoring extensions in filenames.') | |
extensions = False | |
args.remove('-i') | |
append_string = args[1] | |
files = args[2:] | |
for f in files: | |
before = f | |
if extensions and '.' in before: | |
name, ext = before.rsplit('.', 1) | |
after = f'{name}{append_string}.{ext}' | |
else: | |
after = f'{before}{append_string}' | |
print(f'{action}: \'{before}\' -to- \'{after}\'') | |
if not dry_run: | |
os.rename(before, after) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
help = """Simple script to prepend text to specified files | |
usage: prepend (-n) "text to prepend" files | |
-n: dry-run, will show before and after filenames without making changes | |
prepend "Project 1 - " *.docx | |
'outline.docx' -> 'Project 1 - outline.docx' | |
prepend \(Finished\ reading\)\ *.epub | |
'amazing stories.epub' -> '(Finished reading) amazing stories.epub' | |
""" | |
import sys | |
from sys import argv as args | |
import os | |
if len(args) < 3: | |
print(help) | |
sys.exit(0) | |
dry_run = False | |
action = 'Renaming' | |
if '-n' in args: | |
action = 'Would have renamed' | |
print('Dry-run enabled. No files will be changed.') | |
dry_run = True | |
args.remove('-n') | |
prepend_string = args[1] | |
files = args[2:] | |
for f in files: | |
before = f | |
after = f'{prepend_string}{f}' | |
print(f'{action}: \'{before}\' -to- \'{after}\'') | |
if not dry_run: | |
os.rename(before, after) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment