Skip to content

Update py_rename.py #11

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
43 changes: 43 additions & 0 deletions py_rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,39 @@ class RenameIt(object):
* camel_case
* rename
"""
def bulk_rename(self, directory):
"""Recursively rename files in a directory

:directory: str, the root directory to start renaming files
:return: None
"""
for root, _, files in os.walk(directory):
for file_name in files:
full_path = os.path.join(root, file_name)
self._bulk_rename_single_file(full_path)

def _bulk_rename_single_file(self, full_path):
"""Rename a single file in bulk renaming mode

:full_path: str, the full path of the file to be renamed
:return: None
"""
self.full_name = full_path
self.fname, self.fext = os.path.splitext(os.path.basename(full_path))

# Perform the selected renaming operations
if args.rename:
self.rename(args.rename)
if args.prefix:
self.prefix_it(args.prefix)
if args.postfix:
self.postfix_it(args.postfix)
if args.lower:
self.lower_it()
if args.remove_space:
self.replace_space()
if args.camel_case:
self.camel_case()

def __init__(self, filename, dryrun, silent):
self.full_name = filename
Expand Down Expand Up @@ -199,6 +232,16 @@ def rename(self, rename_string):

rename_it = RenameIt(args.filename, args.dryrun, args.silent)

if os.path.isdir(args.filename):
# If the specified path is a directory, perform bulk renaming
rename_it.bulk_rename(args.filename)
else:
# If the specified path is a file, perform single file renaming
if args.rename or args.prefix or args.postfix or args.lower or args.remove_space or args.camel_case:
rename_it._bulk_rename_single_file(args.filename)
else:
print("No bulk renaming operations specified.")

if args.rename:
rename_it.rename(args.rename)
if args.prefix:
Expand Down