2
2
import os
3
3
4
4
if sys .platform != "win32" :
5
- raise OSError ("python- movefile-restart module is only supported on Windows systems!" )
5
+ raise OSError ("movefile-restart module is only supported on Windows systems!" )
6
6
7
7
import winreg
8
8
9
9
_registry = winreg .ConnectRegistry (None , winreg .HKEY_LOCAL_MACHINE )
10
10
_key = winreg .OpenKey (_registry , "SYSTEM\\ CurrentControlSet\\ Control\\ Session Manager" , 0 , winreg .KEY_ALL_ACCESS )
11
11
12
12
def __get_current_values ():
13
+ """Get Values.
14
+
15
+ Internal function to get the current values stored inside PendingFileRenameOperations as a giant list of strings.
16
+
17
+ Returns:
18
+ str[]: List of strings in PendingFileRenameOperations
19
+
20
+ """
13
21
file_ops_values = None
14
22
i = 0
15
23
while True :
@@ -26,10 +34,29 @@ def __get_current_values():
26
34
27
35
28
36
def __set_registry (values ):
37
+ """Set PendingFileRenameOperations.
38
+
39
+ Use at your own risk internal function. Takes a list of strings, and writes it to PendingFileRenameOperations.
40
+
41
+ Args:
42
+ values (str[]): List of strings to write to PendingFileRenameOperations key.
43
+
44
+ """
29
45
winreg .SetValueEx (_key , "PendingFileRenameOperations" , 0 , winreg .REG_MULTI_SZ , values )
30
46
31
47
32
48
def DeleteFile (file_path ):
49
+ """Queue File for Deletion.
50
+
51
+ Adds the Registry information to delete a file on reboot.
52
+
53
+ Args:
54
+ file_path (str): A path to the file to delete.
55
+
56
+ Raises:
57
+ FileNotFoundError: Raised if the file_path doesn't exist.
58
+
59
+ """
33
60
file_path = file_path .replace ("/" , "\\ " )
34
61
if not (os .path .isfile (file_path )):
35
62
raise FileNotFoundError ("Path {} does not exist!" .format (file_path ))
@@ -40,6 +67,19 @@ def DeleteFile(file_path):
40
67
41
68
42
69
def MoveFile (from_path , to_path ):
70
+ """Queue File for Moving.
71
+
72
+ Adds the Registry information to move a file on reboot.
73
+
74
+ Args:
75
+ from_path (str): The directory being moved from.
76
+ to_path (str): The directory being moved to.
77
+
78
+ Raises:
79
+ FileNotFoundError: Raised if the from_path doesn't exist or if the directory of to_path doesn't exist.
80
+ FileExistsError: Raised if to_path already exists.
81
+
82
+ """
43
83
from_path = from_path .replace ("/" , "\\ " )
44
84
if not os .path .isfile (from_path ): # Don't move non-existant path
45
85
raise FileNotFoundError ("Path {} does not exist!" .format (from_path ))
@@ -62,11 +102,37 @@ def MoveFile(from_path, to_path):
62
102
63
103
64
104
def RenameFile (from_path , to_path ):
105
+ """MoveFile Alias."""
65
106
MoveFile (from_path , to_path )
66
107
67
108
109
+ def GetFileOperations ():
110
+ """Get Pending File Operations.
111
+
112
+ Returns a list with tuples of the format (from_path, to_path). If to_path is empty, then the file is being deleted.
113
+
114
+ Returns:
115
+ tuple[]: A list of tuples containing the pending file operations.
116
+
117
+ """
118
+ values = __get_current_values ()
119
+ to_return = []
120
+ for i in range (int (len (values ) / 2 )):
121
+ to_return .append ((values [2 * i ].replace ("\\ ??\\ " , "" ), values [2 * i + 1 ].replace ("\\ ??\\ " , "" )))
122
+ return to_return
123
+
124
+
125
+ def PrintFileOperations ():
126
+ """Prints Pending File Operations."""
127
+ vals = GetFileOperations ()
128
+ for i in vals :
129
+ if i [1 ] == "" :
130
+ print ("Deleting {}" .format (i [0 ]))
131
+ else :
132
+ print ("Moving {} to {}" .format (i [0 ], i [1 ]))
133
+
134
+
68
135
if __name__ == "__main__" :
69
- DeleteFile ("C:\\ Users\\ hammy3502\\ Desktop\\ a.txt" )
70
- MoveFile ("C:\\ Users\\ hammy3502\\ Desktop\\ b.txt" , "C:\\ Users\\ hammy3502\\ Desktop\\ a.txt" )
71
- print ("Currently, there isn't anything implemented for directly running this file." )
136
+ print ("Currently pending file operations: " )
137
+ PrintFileOperations ()
72
138
sys .exit ()
0 commit comments