-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjunctwin.py
More file actions
337 lines (287 loc) · 13.7 KB
/
Copy pathjunctwin.py
File metadata and controls
337 lines (287 loc) · 13.7 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
"""
Windows Junction/Hard Link Creator for 'Send To' Menu
Creates junction points for directories and hard links for files.
"""
import sys
import os
import subprocess
import ctypes
import tkinter as tk
from tkinter import filedialog, messagebox, ttk
from pathlib import Path
def is_admin():
"""Check if running with administrator privileges"""
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
def run_as_admin(source_path):
"""Restart the script with administrator privileges"""
try:
script = sys.argv[0]
params = f'"{source_path}"'
ctypes.windll.shell32.ShellExecuteW(
None, "runas", sys.executable, f'"{script}" {params}', None, 1
)
return True
except Exception as e:
messagebox.showerror("Error", f"Failed to elevate privileges:\n{str(e)}")
return False
class JunctionCreatorGUI:
def __init__(self, source_path):
self.source_path = Path(source_path).resolve()
self.is_file = self.source_path.is_file()
self.target_path = None
# Create main window first
self.root = tk.Tk()
self.root.title("junctwin")
self.root.geometry("550x300")
self.root.resizable(False, False)
# Initialize direction variable after root is created
self.direction = tk.StringVar(value="to_source")
# Center window
self.root.update_idletasks()
x = (self.root.winfo_screenwidth() // 2) - (550 // 2)
y = (self.root.winfo_screenheight() // 2) - (300 // 2)
self.root.geometry(f"+{x}+{y}")
self.setup_ui()
def setup_ui(self):
# Title
title_frame = tk.Frame(self.root, bg="#0078D4", height=50)
title_frame.pack(fill=tk.X, pady=(0, 15))
title_label = tk.Label(title_frame, text="junctwin",
font=("Segoe UI", 14, "bold"),
bg="#0078D4", fg="white")
title_label.pack(pady=10)
# Main content frame
content_frame = tk.Frame(self.root, padx=20)
content_frame.pack(fill=tk.BOTH, expand=True)
# Source display
source_type = "File" if self.is_file else "Folder"
source_label = tk.Label(content_frame, text=f"Current {source_type}:",
font=("Segoe UI", 9, "bold"))
source_label.pack(anchor=tk.W)
source_text = tk.Text(content_frame, height=2, wrap=tk.WORD,
font=("Segoe UI", 9), bg="#F0F0F0",
relief=tk.FLAT, padx=5, pady=5)
source_text.pack(fill=tk.X, pady=(2, 15))
source_text.insert(1.0, str(self.source_path))
source_text.config(state=tk.DISABLED)
# Direction options
link_type = "hard link" if self.is_file else "junction"
direction_frame = tk.LabelFrame(content_frame, text=f"{link_type.title()} Direction",
font=("Segoe UI", 9, "bold"), padx=10, pady=10)
direction_frame.pack(fill=tk.X, pady=(0, 15))
if self.is_file:
rb1_text = "Create link IN current file's folder → pointing TO target file"
rb2_text = "Create link IN target file's folder → pointing TO current file"
else:
rb1_text = "Create junction IN current folder → pointing TO target folder"
rb2_text = "Create junction IN target folder → pointing TO current folder"
rb1 = tk.Radiobutton(direction_frame,
text=rb1_text,
variable=self.direction, value="to_target",
font=("Segoe UI", 9))
rb1.pack(anchor=tk.W, pady=2)
rb2 = tk.Radiobutton(direction_frame,
text=rb2_text,
variable=self.direction, value="to_source",
font=("Segoe UI", 9))
rb2.pack(anchor=tk.W, pady=2)
# Buttons
button_frame = tk.Frame(content_frame)
button_frame.pack(fill=tk.X, pady=(10, 0))
target_type = "File" if self.is_file else "Folder"
select_btn = tk.Button(button_frame, text=f"Select Target {target_type}",
command=self.select_target,
font=("Segoe UI", 9, "bold"),
bg="#0078D4", fg="white",
padx=20, pady=8,
cursor="hand2")
select_btn.pack(side=tk.LEFT, padx=(0, 10))
cancel_btn = tk.Button(button_frame, text="Cancel",
command=self.root.quit,
font=("Segoe UI", 9),
padx=20, pady=8,
cursor="hand2")
cancel_btn.pack(side=tk.RIGHT)
def select_target(self):
"""Open file/folder picker and create link"""
if self.is_file:
if self.direction.get() == "to_target":
# Creating link in current folder pointing to target file
target = filedialog.askopenfilename(
title="Select Target File",
initialdir=self.source_path.parent
)
else:
# Creating link in target folder pointing to current file
target = filedialog.askdirectory(
title="Select Target Folder for Link",
initialdir=self.source_path.parent
)
else:
target = filedialog.askdirectory(
title="Select Target Folder",
initialdir=self.source_path.parent
)
if not target:
return
self.target_path = Path(target).resolve()
# Validate paths are different
if self.target_path == self.source_path:
messagebox.showerror("Error",
"Source and target must be different!")
return
# For file + to_source: validate target is a folder
if self.is_file and self.direction.get() == "to_source":
if not self.target_path.is_dir():
messagebox.showerror("Error",
"Please select a folder for the link location!")
return
# Create the link based on selected direction
self.create_link()
def create_link(self):
"""Create the junction point or hard link"""
try:
if self.is_file:
# Determine link paths and target for files
if self.direction.get() == "to_target":
# Create link IN source location pointing TO target file
# Check if cross-drive
source_drive = self.target_path.drive
link_drive = self.source_path.drive
is_cross_drive = source_drive.upper() != link_drive.upper()
link_suffix = "[symlink]" if is_cross_drive else "[link]"
link_name = f"{self.target_path.stem}{link_suffix}{self.target_path.suffix}"
link_path = self.source_path.parent / link_name
target = self.target_path
location_desc = f"in {self.source_path.parent.name}"
else: # to_source
# Create link IN target folder pointing TO source file
# Check if cross-drive
source_drive = self.source_path.drive
link_drive = self.target_path.drive
is_cross_drive = source_drive.upper() != link_drive.upper()
link_suffix = "[symlink]" if is_cross_drive else "[link]"
link_name = f"{self.source_path.stem}{link_suffix}{self.source_path.suffix}"
link_path = self.target_path / link_name
target = self.source_path
location_desc = f"in {self.target_path.name}"
else:
# Directory junctions
link_suffix = "[junct]"
if self.direction.get() == "to_target":
# Create junction IN source pointing TO target
link_name = f"{self.target_path.name}{link_suffix}"
link_path = self.source_path / link_name
target = self.target_path
location_desc = f"in {self.source_path.name}"
else: # to_source
# Create junction IN target pointing TO source
link_name = f"{self.source_path.name}{link_suffix}"
link_path = self.target_path / link_name
target = self.source_path
location_desc = f"in {self.target_path.name}"
# Check if link already exists
if link_path.exists():
link_type = "Symbolic link" if (self.is_file and "symlink" in link_name) else ("Hard link" if self.is_file else "Junction")
response = messagebox.askyesno(
f"{link_type} Exists",
f"'{link_name}' already exists {location_desc}.\n\n"
f"Do you want to delete it and create a new {link_type.lower()}?"
)
if not response:
return
# Remove existing link
if link_path.is_dir():
os.rmdir(link_path)
else:
link_path.unlink()
# Create hard link for files or junction for directories
if self.is_file:
# Check if files are on the same drive
source_drive = target.drive
link_drive = link_path.drive
if source_drive.upper() != link_drive.upper():
# Use symbolic link for cross-drive file links
# Check for admin privileges
if not is_admin():
response = messagebox.askyesno(
"Elevation Required",
f"Files are on different drives ({source_drive} and {link_drive}).\n\n"
f"Symbolic links across drives require administrator privileges.\n\n"
"Would you like to restart junctwin with elevated privileges?"
)
if response:
if run_as_admin(self.source_path):
self.root.quit()
return
cmd = f'mklink "{link_path}" "{target}"'
link_type = "Symbolic link"
else:
cmd = f'mklink /H "{link_path}" "{target}"'
link_type = "Hard link"
else:
cmd = f'mklink /J "{link_path}" "{target}"'
link_type = "Junction"
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
if result.returncode == 0:
messagebox.showinfo(
"Success",
f"{link_type} created successfully!\n\n"
f"Link: {link_path}\n"
f"Points to: {target}"
)
self.root.quit()
else:
error_msg = result.stderr or result.stdout
# Check if it's an access denied error and offer elevation
if "Access is denied" in error_msg or "access denied" in error_msg.lower():
if not is_admin():
response = messagebox.askyesno(
"Elevation Required",
f"Failed to create {link_type.lower()} due to insufficient privileges.\n\n"
f"Error: {error_msg}\n\n"
"Would you like to restart junctwin with elevated privileges?"
)
if response:
if run_as_admin(self.source_path):
self.root.quit()
return
messagebox.showerror("Error",
f"Failed to create {link_type.lower()}:\n{error_msg}")
except Exception as e:
messagebox.showerror("Error", f"Failed to create link:\n{str(e)}")
def run(self):
"""Run the GUI"""
self.root.mainloop()
self.root.destroy()
def main():
try:
# Check if folder argument provided
if len(sys.argv) < 2:
root = tk.Tk()
root.withdraw()
messagebox.showerror("Error",
"No file or folder specified!\n\n"
"This script should be called from the 'Send To' menu.")
return
source_path = sys.argv[1]
# Check if path exists
if not os.path.exists(source_path):
root = tk.Tk()
root.withdraw()
messagebox.showerror("Error",
f"Path not found: {source_path}")
return
# Create and run GUI (accepts both files and directories)
app = JunctionCreatorGUI(source_path)
app.run()
except Exception as e:
root = tk.Tk()
root.withdraw()
messagebox.showerror("Error",
f"An error occurred:\n\n{str(e)}")
if __name__ == "__main__":
main()