Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add feature to remove characters from start or end of filename #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ optional arguments:
prefix filename with prefix string
-B string, --postfix string
postfix filename with postfix string
-C int, --remove int remove characters from start or end of filename (+ve
for start, -ve for end)
-r string, --rename string
replace filename with string
-n, --dryrun perform a dry run (will not run any actions)
Expand Down
30 changes: 30 additions & 0 deletions py_rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@ def postfix_it(self, postfix_str):
new_name = old_name + postfix_str
self._rename(new_name)

def remove_it(self,n_chars):
"""Remove characters from the start or end of filename

:n_chars: int, number of characters to remove. +ve from start, -ve from end
:returns: None
"""
old_name = self.fname
length = len(old_name)
if abs(n_chars) >= length:
self._print(
"Failed to rename {old} : {err}".
format(old=self.full_name, err="Number of characters equals or exceeds length of filename"))
elif n_chars >= 0:
new_name = old_name[n_chars::]
self._rename(new_name)
else:
new_name = old_name[0:n_chars]
self._rename(new_name)

def lower_it(self):
"""Lowercase the filename
:returns: None
Expand Down Expand Up @@ -152,6 +171,15 @@ def rename(self, rename_string):
action='store',
help="postfix filename with postfix string"
)
parser.add_argument(
'-C',
'--remove',
dest='remove',
type=int,
metavar='int',
action='store',
help="remove characters from start or end of filename (+ve for start, -ve for end)"
)
parser.add_argument(
'-r',
'--rename',
Expand Down Expand Up @@ -205,6 +233,8 @@ def rename(self, rename_string):
rename_it.prefix_it(args.prefix)
if args.postfix:
rename_it.postfix_it(args.postfix)
if args.remove:
rename_it.remove_it(args.remove)
if args.lower:
rename_it.lower_it()
if args.remove_space:
Expand Down