22import os
33
44if 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!" )
66
77import winreg
88
99_registry = winreg .ConnectRegistry (None , winreg .HKEY_LOCAL_MACHINE )
1010_key = winreg .OpenKey (_registry , "SYSTEM\\ CurrentControlSet\\ Control\\ Session Manager" , 0 , winreg .KEY_ALL_ACCESS )
1111
1212def __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+ """
1321 file_ops_values = None
1422 i = 0
1523 while True :
@@ -26,10 +34,29 @@ def __get_current_values():
2634
2735
2836def __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+ """
2945 winreg .SetValueEx (_key , "PendingFileRenameOperations" , 0 , winreg .REG_MULTI_SZ , values )
3046
3147
3248def 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+ """
3360 file_path = file_path .replace ("/" , "\\ " )
3461 if not (os .path .isfile (file_path )):
3562 raise FileNotFoundError ("Path {} does not exist!" .format (file_path ))
@@ -40,6 +67,19 @@ def DeleteFile(file_path):
4067
4168
4269def 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+ """
4383 from_path = from_path .replace ("/" , "\\ " )
4484 if not os .path .isfile (from_path ): # Don't move non-existant path
4585 raise FileNotFoundError ("Path {} does not exist!" .format (from_path ))
@@ -62,11 +102,37 @@ def MoveFile(from_path, to_path):
62102
63103
64104def RenameFile (from_path , to_path ):
105+ """MoveFile Alias."""
65106 MoveFile (from_path , to_path )
66107
67108
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+
68135if __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 ()
72138 sys .exit ()
0 commit comments