Skip to content

Commit b83be21

Browse files
committed
Merge pull request geekcomputers#20 from Ricky-Wilson/master
modified: batch_file_rename.py
2 parents 502beb7 + 0140659 commit b83be21

File tree

1 file changed

+51
-25
lines changed

1 file changed

+51
-25
lines changed

batch_file_rename.py

+51-25
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,51 @@
1-
# Script Name : batch_file_rename.py
2-
# Author : Craig Richards
3-
# Created : 6th August 2012
4-
# Last Modified :
5-
# Version : 1.0
6-
7-
# Modifications :
8-
9-
# Description : This will batch rename a group of files in a given directory, once you pass the current and new extensions
10-
11-
import os # Load the library module
12-
import sys # Load the library module
13-
14-
work_dir=sys.argv[1] # Set the variable work_dir with the first argument passed
15-
old_ext=sys.argv[2] # Set the variable work_dir with the first argument passed
16-
new_ext=sys.argv[3] # Set the variable work_dir with the first argument passed
17-
18-
files = os.listdir(work_dir) # Set the variable files, by listing everything in the directory
19-
for filename in files: # Loop through the files
20-
file_ext = os.path.splitext(filename)[1] # Get the file extension
21-
if old_ext == file_ext: # Start of the logic to check the file extensions, if old_ext = file_ext
22-
newfile = filename.replace(old_ext, new_ext) # Set newfile to be the filename, replaced with the new extension
23-
os.rename( # Write the files
24-
os.path.join(work_dir, filename),
25-
os.path.join(work_dir, newfile))
1+
# batch_file_rename.py
2+
# Created: 6th August 2012
3+
4+
'''
5+
This will batch rename a group of files in a given directory,
6+
once you pass the current and new extensions
7+
'''
8+
9+
__author__ = 'Craig Richards'
10+
__version__ = '1.0'
11+
12+
import os
13+
import sys
14+
15+
16+
def batch_rename(work_dir, old_ext, new_ext):
17+
'''
18+
This will batch rename a group of files in a given directory,
19+
once you pass the current and new extensions
20+
'''
21+
# files = os.listdir(work_dir)
22+
for filename in os.listdir(work_dir):
23+
# Get the file extension
24+
file_ext = os.path.splitext(filename)[1]
25+
# Start of the logic to check the file extensions, if old_ext = file_ext
26+
if old_ext == file_ext:
27+
# Set newfile to be the filename, replaced with the new extension
28+
newfile = filename.replace(old_ext, new_ext)
29+
# Write the files
30+
os.rename(
31+
os.path.join(work_dir, filename),
32+
os.path.join(work_dir, newfile)
33+
)
34+
35+
36+
def main():
37+
'''
38+
This will be called if the script is directly envoked.
39+
'''
40+
# Set the variable work_dir with the first argument passed
41+
work_dir = sys.argv[1]
42+
# Set the variable old_ext with the second argument passed
43+
old_ext = sys.argv[2]
44+
# Set the variable new_ext with the third argument passed
45+
new_ext = sys.argv[3]
46+
batch_rename(work_dir, old_ext, new_ext)
47+
48+
49+
if __name__ == '__main__':
50+
main()
51+

0 commit comments

Comments
 (0)