-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrbexp.py
80 lines (70 loc) · 2.2 KB
/
rbexp.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
# Recycle Bin Exporter 1.0
# Author: https://github.com/mt1006
# py -m pip install pypiwin32
# py -m pip install winshell
import os
import errno
import winshell
PATH = 'recycle_bin'
INDEX_FILE_NAME = 'index.csv'
CREATE_INDEX_FILE = True
def copyRecycleBin(path = PATH, indexFileName = INDEX_FILE_NAME,
createIndexFile = CREATE_INDEX_FILE, consoleOutput = True):
def getNewName(name):
retName = name
if name in filesSet:
i = 1
fileName, fileExt = os.path.splitext(name)
while True:
newName = f'{fileName}_{i}{fileExt}'
if newName not in filesSet:
retName = newName
break
i += 1
filesSet.add(retName)
return retName
finalPath = os.path.abspath(path)
filesSet = set()
try:
os.mkdir(path)
except OSError as error:
print("Failed to copy recycle bin!")
if error.errno == errno.EEXIST:
print(f'Directory "{finalPath}" currently exists...')
return False
if createIndexFile:
try:
indexFile = open(indexFileName, 'x', encoding='utf-8')
except OSError as error:
print("Failed to copy recycle bin!")
if error.errno == errno.EEXIST:
print(f'Index file "{indexFileName}" currently exists...')
return False
os.chdir(path)
elements = list(winshell.recycle_bin())
if createIndexFile:
indexFile.write(f'"Recycle bin files index";"Number of elements: {len(elements)}"\n\n')
indexFile.write(f'"File name";"Original file name";"Created at";"Deleted at"\n')
for i, element in enumerate(elements):
curFilePath = element.filename()
orgFileName = element.original_filename()
fileName = getNewName(os.path.basename(orgFileName))
try:
cTime = str(element.getctime())
except:
cTime = '[unknown]'
winshell.copy_file(curFilePath, fileName)
if createIndexFile:
indexFile.write(f'"{fileName}";"{orgFileName}";"{cTime}";"{element.recycle_date()}"\n')
if consoleOutput:
print(f'{i + 1}/{len(elements)}')
if consoleOutput:
print('Copying completed!')
return True
if __name__ == '__main__':
print(f'All your files in recycle bin will be copied to {os.path.abspath(PATH)}')
cont = input('Do you want to continue? [Y/N]:')
if cont.lower() != 'y':
exit()
copyRecycleBin()
input()