Skip to content

Commit 7714b62

Browse files
committed
Changed from xcopy to shutil and glob
1 parent 2cafa96 commit 7714b62

File tree

1 file changed

+68
-48
lines changed

1 file changed

+68
-48
lines changed

Windows Backup/windows_backup.py

Lines changed: 68 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,116 @@
1-
import os, sys
2-
from PySide6.QtWidgets import QMainWindow, QApplication, QPushButton, QVBoxLayout, QWidget, QFileDialog, QMessageBox
1+
import os, sys, glob, shutil
2+
from PySide6.QtWidgets import QMainWindow, QApplication, QPushButton, QWidget, QFileDialog, QMessageBox, QGridLayout
3+
34

45
class MainWindow(QMainWindow):
56
def __init__(self):
67
super().__init__()
78

89
self.setWindowTitle("Windows Backup Application")
910
self.setFixedSize(800, 600)
10-
11-
#Define Backup Location Variable
12-
self.backup_location = "" #Set by User
13-
11+
12+
# Define Backup Location Variable
13+
self.backup_location = "" # Set by User
14+
1415
central_widget = QWidget()
1516
self.setCentralWidget(central_widget)
16-
layout = QVBoxLayout()
17+
layout = QGridLayout()
1718
central_widget.setLayout(layout)
18-
19-
#Set Backup Location Button
19+
cols = 2
20+
21+
# Set Backup Location Button
2022
self.backup_location_button = QPushButton("Set Backup Location", self)
2123
self.backup_location_button.clicked.connect(self.set_backup_location)
22-
layout.addWidget(self.backup_location_button)
23-
24-
25-
#Create Backup Buttons
24+
layout.addWidget(self.backup_location_button, 0, 0, 1, cols) # Span top row
25+
26+
# Create Backup Buttons
2627
self.backup_buttons = []
27-
2828
self.backup_buttons.append(self.create_button("Backup Contacts", self.backup_contacts))
2929
self.backup_buttons.append(self.create_button("Backup Photos", self.backup_photos))
3030
self.backup_buttons.append(self.create_button("Backup Documents", self.backup_documents))
3131
self.backup_buttons.append(self.create_button("Backup Videos", self.backup_videos))
3232
self.backup_buttons.append(self.create_button("Backup Music", self.backup_music))
3333
self.backup_buttons.append(self.create_button("Backup Desktop", self.backup_desktop))
3434
self.backup_buttons.append(self.create_button("Backup Downloads", self.backup_downloads))
35+
self.backup_buttons.append(self.create_button("Backup Outlook Files", self.backup_outlook_files))
3536

36-
for btn in self.backup_buttons:
37-
btn.setFixedSize(150, 40) # Set button size
37+
# Add buttons to grid layout
38+
for idx, btn in enumerate(self.backup_buttons):
39+
btn.setFixedSize(150, 40)
3840
btn.setEnabled(False)
39-
layout.addWidget(btn)
41+
row = 1 + idx // cols
42+
col = idx % cols
43+
layout.addWidget(btn, row, col)
4044

41-
42-
4345
def create_button(self, text, command):
4446
btn = QPushButton(text, self)
4547
btn.clicked.connect(command)
4648
return btn
47-
48-
49+
4950
def set_backup_location(self):
5051
folder = QFileDialog.getExistingDirectory(self, "Select Backup Location")
5152
if folder:
5253
self.backup_location = folder
5354
for btn in self.backup_buttons:
5455
btn.setEnabled(True)
55-
56-
def run_backup(self, source, destination):
56+
57+
def copy_with_glob(self, source_dir, destination_name, patterns=["*"]):
58+
"""Copy files matching patterns from source_dir to backup_location/destination_name"""
5759
if not self.backup_location:
5860
QMessageBox.warning(self, "Error", "Please set a backup location first.")
5961
return
60-
61-
62-
destination = os.path.join(self.backup_location, os.path.basename(source))
62+
63+
source_dir = os.path.expanduser(source_dir)
64+
if not os.path.exists(source_dir):
65+
QMessageBox.warning(self, "Error", f"Source folder not found: {source_dir}")
66+
return
67+
68+
destination = os.path.join(self.backup_location, destination_name)
6369
os.makedirs(destination, exist_ok=True)
64-
cmd = f'xcopy /E /I /Y "{source}" "{destination}"'
65-
os.system(cmd)
66-
QMessageBox.information(self, "Success", f"Backup of {os.path.basename(source)} completed.")
67-
70+
71+
files_copied = 0
72+
for pattern in patterns:
73+
for file_path in glob.glob(os.path.join(source_dir, pattern), recursive=True):
74+
if os.path.isfile(file_path): # only copy files, not directories
75+
try:
76+
shutil.copy2(file_path, destination)
77+
files_copied += 1
78+
except Exception as e:
79+
QMessageBox.warning(self, "Error", f"Failed to copy {file_path}: {e}")
80+
81+
if files_copied > 0:
82+
QMessageBox.information(self, "Success", f"Backup of {files_copied} files completed in {destination_name}.")
83+
else:
84+
QMessageBox.information(self, "Info", f"No files found in {source_dir} to backup.")
85+
86+
# Backup Methods
6887
def backup_contacts(self):
69-
self.run_backup(os.path.expanduser("~/Contacts"))
70-
88+
self.copy_with_glob("~/Contacts", "Contacts")
89+
7190
def backup_photos(self):
72-
self.run_backup(os.path.expanduser("~/Pictures"))
73-
91+
self.copy_with_glob("~/Pictures", "Pictures")
92+
7493
def backup_documents(self):
75-
self.run_backup(os.path.expanduser("~/Documents"))
76-
94+
self.copy_with_glob("~/Documents", "Documents")
95+
7796
def backup_videos(self):
78-
self.run_backup(os.path.expanduser("~/Videos"))
79-
97+
self.copy_with_glob("~/Videos", "Videos")
98+
8099
def backup_music(self):
81-
self.run_backup(os.path.expanduser("~/Music"))
82-
100+
self.copy_with_glob("~/Music", "Music")
101+
83102
def backup_desktop(self):
84-
self.run_backup(os.path.expanduser("~/Desktop"))
85-
103+
self.copy_with_glob("~/Desktop", "Desktop")
104+
86105
def backup_downloads(self):
87-
self.run_backup(os.path.expanduser("~/Downloads"))
88-
89-
90-
91-
if __name__ == "__main__":
106+
self.copy_with_glob("~/Downloads", "Downloads")
107+
108+
def backup_outlook_files(self):
109+
self.copy_with_glob("~/AppData/Local/Microsoft/Outlook", "Outlook", ["*.pst", "*.ost", "*.nst"])
110+
111+
112+
if __name__ == "__main__":
92113
app = QApplication(sys.argv)
93114
window = MainWindow()
94115
window.show()
95116
app.exec()
96-

0 commit comments

Comments
 (0)