-
Notifications
You must be signed in to change notification settings - Fork 6
/
文件名重命名成文件夹名+Vol_序号的格式后移到上级目录并删除文件夹(拖拽,仅多个文件夹).py
101 lines (92 loc) · 4.42 KB
/
文件名重命名成文件夹名+Vol_序号的格式后移到上级目录并删除文件夹(拖拽,仅多个文件夹).py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# encoding:utf-8
# https://github.com/wangandi520/andyspythonscript
from pathlib import Path
import sys
import os
def writefile(filename, filereadlines):
newfile = open(filename, mode='w', encoding='ANSI')
newfile.writelines(filereadlines)
newfile.close()
def main(inputPath):
# get folder name
# create log and recover file = True, don't create = False
# 如果不想生成日志和恢复文件,下面这行改成createLogAndRecover = False
createLogAndRecover = False
# show window,如果不想显示处理窗口,showCMDwindows = False
showCMDwindows = False
for aPath in inputPath:
if Path.is_dir(Path(aPath)):
# first book start index,第一本书的序号
startIndex = 1
folderName = Path(aPath).name
renameLastFile = False
# remove [XX完] or [XX未]
if folderName.endswith('完]') or folderName.endswith('全]'):
renameLastFile = True
if folderName.endswith('完]') or folderName.endswith('全]') or folderName.endswith('未]'):
loc = folderName.rfind('[')
folderName = folderName[:loc]
# if folder and file mix, exit
inFile = False
inFolder = False
for file in Path(aPath).glob('*'):
if Path.is_file(file):
inFile = True
if Path.is_dir(file):
inFolder = True
if inFile and inFolder:
print("Sorry, Not support folders and files in same folder.")
print("抱歉,暂时不支持文件和文件夹的在同一个文件夹。")
os.system("pause")
sys.exit()
# rename all file
cmdLog = []
recoverLog = []
lastFileName = ''
lastFileOldName = ''
for file in Path(aPath).glob('*'):
newFileName = folderName + 'Vol_' + str(startIndex).zfill(2)
# get file suffix
if Path.is_file(file):
newFileName = newFileName + file.suffix
# to do
imputCmd = 'rename "' + str(Path(aPath).joinpath(file)) + '" "' + newFileName + '"'
# .bat
recoverCmd = 'rename "' + str(Path(aPath).joinpath(newFileName)) + '" "' + file.name + '"'
cmdLog.append(imputCmd + '\n')
recoverLog.append(recoverCmd + '\n')
# output
print(file.name + ' -> ' + newFileName)
os.system(imputCmd)
startIndex = startIndex + 1
lastFileName = newFileName
lastFileOldName = file.name
# rename last file.
if renameLastFile:
if Path.is_dir(Path(aPath).joinpath(lastFileName)):
imputCmd = 'rename "' + str(Path(aPath).joinpath(lastFileName)) + '" "' + lastFileName + ' End'
recoverCmd = 'rename "' + str(Path(aPath).joinpath(lastFileName + ' End')) + '" "' + lastFileOldName + '"'
print(lastFileName + ' -> ' + lastFileName + ' End')
if Path.is_file(Path(aPath).joinpath(lastFileName)):
imputCmd = 'rename "' + str(Path(aPath).joinpath(lastFileName)) + '" "' + lastFileName[0:-4] + ' End' + lastFileName[-4:] + '"'
recoverCmd = 'rename "' + str(Path(aPath).joinpath(lastFileName[0:-4] + ' End' + newFileName[-4:])) + '" "' + lastFileOldName + '"'
print(lastFileName + ' -> ' + lastFileName[0:-4] + ' End' + lastFileName[-4:])
cmdLog.append(imputCmd)
recoverLog.append(recoverCmd)
os.system(imputCmd)
if createLogAndRecover:
writefile('Log ' + folderName + '.txt', cmdLog)
writefile('Recover ' + folderName + '.bat', recoverLog)
for file in Path(aPath).glob('*'):
oldFilePath = Path(aPath).joinpath(Path(file.name))
newFilePath = Path(aPath).parent.joinpath(Path(file.name))
Path(oldFilePath).replace(newFilePath)
Path.rmdir(Path(aPath))
if showCMDwindows:
os.system("pause")
if __name__ == '__main__':
try:
if len(sys.argv) >= 2:
main(sys.argv)
except IndexError:
pass