Skip to content

modified: batch_file_rename.py #20

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

Merged
merged 1 commit into from
Jan 17, 2016
Merged
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
76 changes: 51 additions & 25 deletions batch_file_rename.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,51 @@
# Script Name : batch_file_rename.py
# Author : Craig Richards
# Created : 6th August 2012
# Last Modified :
# Version : 1.0

# Modifications :

# Description : This will batch rename a group of files in a given directory, once you pass the current and new extensions

import os # Load the library module
import sys # Load the library module

work_dir=sys.argv[1] # Set the variable work_dir with the first argument passed
old_ext=sys.argv[2] # Set the variable work_dir with the first argument passed
new_ext=sys.argv[3] # Set the variable work_dir with the first argument passed

files = os.listdir(work_dir) # Set the variable files, by listing everything in the directory
for filename in files: # Loop through the files
file_ext = os.path.splitext(filename)[1] # Get the file extension
if old_ext == file_ext: # Start of the logic to check the file extensions, if old_ext = file_ext
newfile = filename.replace(old_ext, new_ext) # Set newfile to be the filename, replaced with the new extension
os.rename( # Write the files
os.path.join(work_dir, filename),
os.path.join(work_dir, newfile))
# batch_file_rename.py
# Created: 6th August 2012

'''
This will batch rename a group of files in a given directory,
once you pass the current and new extensions
'''

__author__ = 'Craig Richards'
__version__ = '1.0'

import os
import sys


def batch_rename(work_dir, old_ext, new_ext):
'''
This will batch rename a group of files in a given directory,
once you pass the current and new extensions
'''
# files = os.listdir(work_dir)
for filename in os.listdir(work_dir):
# Get the file extension
file_ext = os.path.splitext(filename)[1]
# Start of the logic to check the file extensions, if old_ext = file_ext
if old_ext == file_ext:
# Set newfile to be the filename, replaced with the new extension
newfile = filename.replace(old_ext, new_ext)
# Write the files
os.rename(
os.path.join(work_dir, filename),
os.path.join(work_dir, newfile)
)


def main():
'''
This will be called if the script is directly envoked.
'''
# Set the variable work_dir with the first argument passed
work_dir = sys.argv[1]
# Set the variable old_ext with the second argument passed
old_ext = sys.argv[2]
# Set the variable new_ext with the third argument passed
new_ext = sys.argv[3]
batch_rename(work_dir, old_ext, new_ext)


if __name__ == '__main__':
main()