-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSanitizer.py
More file actions
37 lines (28 loc) · 986 Bytes
/
FileSanitizer.py
File metadata and controls
37 lines (28 loc) · 986 Bytes
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
import os
import re
import sys
dirToWalk = "."
if len(sys.argv) == 2:
dirToWalk = sys.argv[1]
if os.path.isdir(dirToWalk) is False:
print('Directory "{0}" does not exist'.format(dirToWalk))
exit()
p = re.compile('100[0-9]{6}')
for root, dirs, files in os.walk(dirToWalk):
for file in files:
for itr in p.finditer(file):
m = next(p.finditer(file))
newFileName = file[m.start():m.end()]
_, ext = os.path.splitext(file)
oldFilePath = root + "/" + file
newFilePath = root + "/" + newFileName + ext
print(oldFilePath)
if oldFilePath == newFilePath:
continue
if os.path.isfile(newFilePath):
print("File {} already exists. Rename FAILED.".format(newFilePath))
continue
os.rename(oldFilePath, newFilePath)
print("Renamed {} to {}".format(oldFilePath, newFilePath))
break
break