-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-rename
executable file
·65 lines (53 loc) · 1.96 KB
/
wp-rename
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#! /usr/bin/env python3
import os
import random
PATH = os.getcwd()
BLACKLIST = [".git"]
ATLAS = [i for i in "abcdefghijklmnopqrstuvwxyz"]
WALL_DIR = "/media/storage/pictures/wallpapers/"
prevFiles = [os.path.splitext(f)[0] for f in os.listdir(WALL_DIR)]
def permutation(n, r):
def factorial(n):
if n == 0:
return 1
else:
return n*factorial(n-1)
return int(factorial(n)/factorial(n-r))
def nameGenerator(template: list, length: int) -> str:
name = []
while len(name) < length:
letter = random.choice(template)
if letter not in name:
name.append(letter)
return "".join(name)
def multiNameGenerator(template: list, length: int, amt: int) -> str:
names = []
while len(names) < amt:
name = nameGenerator(template, length)
if name not in names:
if name not in prevFiles:
names.append(name)
return names
def randomRenamer(dire: str, blacklist: list, atlas: list, length: int) -> None:
oldFiles = [i for i in os.listdir(dire) if i not in blacklist]
nFiles = len(oldFiles)
interFiles = [str(_+1).zfill(4)+os.path.splitext(oldFiles[_])[-1]
for _ in range(nFiles)]
newNames = multiNameGenerator(atlas, length, nFiles)
newFiles = [newNames[_] +
os.path.splitext(oldFiles[_])[-1] for _ in range(nFiles)]
confirmation = input(f"Are you sure you want to rename {nFiles} files? (Y/n) ")
if confirmation in ["y", ""]:
for _ in range(nFiles):
os.rename(os.path.join(dire, oldFiles[_]), os.path.join(
dire, interFiles[_]))
for _ in range(nFiles):
os.rename(os.path.join(
dire, interFiles[_]), os.path.join(dire, newFiles[_]))
print("Renamed %d files" % nFiles)
elif confirmation=="n":
pass
else:
print("Invalid option entered.")
if __name__ == "__main__":
randomRenamer(PATH, BLACKLIST, ATLAS, 8)