From 7f580e7715621d7abe87d56f9b547224b96e1987 Mon Sep 17 00:00:00 2001
From: Moran Senyor
Date: Sun, 14 Dec 2014 22:30:44 +0200
Subject: [PATCH 1/3] added a check to make sure no files are overwriten during
the rename
---
batch_file_rename.py | 17 ++++++++++++-----
1 file changed, 12 insertions(+), 5 deletions(-)
diff --git a/batch_file_rename.py b/batch_file_rename.py
index bab145e909b..50d8b33e231 100644
--- a/batch_file_rename.py
+++ b/batch_file_rename.py
@@ -9,17 +9,24 @@
# 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
+#import sys # Load the library module
+from sys import argv
-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
+#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
+script, work_dir, old_ext, new_ext = argv
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
+ while os.path.isfile(newfile): # Check if the new file name already exists to prevent overwrite
+ num = 2 # a number to add to the file name
+ num_new_ext = "_" + str(num) + new_ext # creates a new string to append to the end of file name
+ newfile = filename.replace(old_ext, num_new_ext) # apply changes to the new file name
+ num += 1 # increment the number in case the name still exists
os.rename( # Write the files
os.path.join(work_dir, filename),
- os.path.join(work_dir, newfile))
\ No newline at end of file
+ os.path.join(work_dir, newfile))
From 49b673670da6f64b62abe3566581e9d50ab8ec8a Mon Sep 17 00:00:00 2001
From: Moran Senyor
Date: Sun, 14 Dec 2014 23:07:19 +0200
Subject: [PATCH 2/3] edited argv assignment to ignore the 0 index item
---
batch_file_rename.py | 8 ++------
1 file changed, 2 insertions(+), 6 deletions(-)
diff --git a/batch_file_rename.py b/batch_file_rename.py
index 50d8b33e231..d124f8d4bff 100644
--- a/batch_file_rename.py
+++ b/batch_file_rename.py
@@ -9,13 +9,9 @@
# 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
-from sys import argv
+from sys import argv # imports argv from sys to take arguments given in the command line when the program is initiated
-#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
-script, work_dir, old_ext, new_ext = argv
+work_dir, old_ext, new_ext = argv[1:] # work_dir is the work directory, old_ext and new_ext are the current and wanted file extentions respectively.
files = os.listdir(work_dir) # Set the variable files, by listing everything in the directory
for filename in files: # Loop through the files
From c9f67227731cde548ac0bba7cb9cda6521dd2668 Mon Sep 17 00:00:00 2001
From: Moran Senyor
Date: Sun, 14 Dec 2014 23:13:01 +0200
Subject: [PATCH 3/3] added new line at end of file
---
batch_file_rename.py | 1 +
1 file changed, 1 insertion(+)
diff --git a/batch_file_rename.py b/batch_file_rename.py
index d124f8d4bff..613637fa53e 100644
--- a/batch_file_rename.py
+++ b/batch_file_rename.py
@@ -26,3 +26,4 @@
os.rename( # Write the files
os.path.join(work_dir, filename),
os.path.join(work_dir, newfile))
+