Skip to content

Commit 5ae5b3b

Browse files
committed
GetFileOperations and PrintFileOperations
1 parent 0045ad6 commit 5ae5b3b

File tree

3 files changed

+76
-5
lines changed

3 files changed

+76
-5
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ To import, use `from movefile_restart import movefile_restart`
1313
From there, you have a couple functions at your disposal:
1414

1515
`movefile_restart.DeleteFile(file_path)`: Queues `file_path` for deletion.
16+
1617
`movefile_restart.MoveFile(from_path, to_path)` or `movefile_restart.RenameFile(from_path, to_path)`: Moves the file from `from_path` to `to_path`.
1718

19+
`movefile_restart.GetFileOperations()`: Get a list of tuples containing the source and destination of all file movings queued.
20+
21+
`movefile_restart.PrintFileOperations()`: Print a list of file operations that are scheduled to occur during reboot.
22+
1823
## Current Limitations
1924

2025
* Files cannot currently be un-queued.

movefile_restart/movefile_restart.py

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@
22
import os
33

44
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!")
66

77
import 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

1212
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+
"""
1321
file_ops_values = None
1422
i = 0
1523
while True:
@@ -26,10 +34,29 @@ def __get_current_values():
2634

2735

2836
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+
"""
2945
winreg.SetValueEx(_key, "PendingFileRenameOperations", 0, winreg.REG_MULTI_SZ, values)
3046

3147

3248
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+
"""
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

4269
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+
"""
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

64104
def 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+
68135
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()
72138
sys.exit()

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="movefile-restart",
8-
version="0.1.1",
8+
version="0.2.0",
99
author="hammy3502",
1010
author_email="hammy275@gmail.com",
1111
description="A small library for Windows to queue files to be moved, deleted, or renamed on reboot.",

0 commit comments

Comments
 (0)