Last active
May 29, 2022 09:43
-
-
Save ccfiel/d99f420e9e285dd110724cb6c4b0802d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import shutil | |
command_help = 'rename.py <source folder> <distation folder>' | |
if len(sys.argv) < 3: | |
print("error: lacking paramaters.") | |
print(command_help) | |
exit(0) | |
source = sys.argv[1] | |
dist = sys.argv[2] | |
if not source: | |
print("No source folder parameter.") | |
print(command_help) | |
exit(0) | |
if not dist: | |
print("No distanation folder parameter.") | |
print(command_help) | |
exit(0) | |
if not os.path.isdir(source): | |
print("Source folder should exist") | |
print(command_help) | |
exit(0) | |
if not os.path.isdir(dist): | |
print("Distnation folder should exist") | |
print(command_help) | |
exit(0) | |
dist_data = [ | |
{"folder": dist + '/pictures', "ext": ['.JPG']}, {"folder": dist + '/videos', "ext": ['.MP4']} | |
] | |
dist_pictures = dist + '/pictures' | |
dist_videos = dist + '/videos' | |
count = 1 | |
for data in dist_data: | |
if not os.path.isdir(data['folder']): | |
os.mkdir(data['folder']) | |
for subdir, dirs, files in os.walk(source): | |
for filename in files: | |
ext_count = 0 | |
for ext in data['ext']: | |
if filename.find(ext) > 0: | |
subdirectoryPath = os.path.relpath(subdir, source) | |
filePath = os.path.join(subdirectoryPath, filename) | |
name, ext = os.path.splitext(filePath) | |
newFilename = data['folder'] + '/' + str(count) + ext | |
shutil.move(source + '/' + filePath, newFilename) | |
count += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment