Created
March 18, 2024 08:13
-
-
Save gsw945/5da7f592e62581528fa7dc2a656172c1 to your computer and use it in GitHub Desktop.
move symlinks from one folder to another folder on windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import pathlib | |
import subprocess | |
def create_symbolic_link(link_path, target_path, is_directory=False): | |
cmd = ['mklink'] | |
if is_directory: | |
cmd.append('/D') | |
cmd.extend([link_path, target_path]) | |
flags = subprocess.CREATE_NEW_CONSOLE | subprocess.CREATE_NO_WINDOW | |
subprocess.run(cmd, shell=True, check=True, creationflags=flags) | |
def do_move(source, target): | |
target_dir = pathlib.Path(target).resolve() | |
for link in pathlib.Path(source).resolve().iterdir(): | |
symlink = os.readlink(str(link)) | |
print('[{}] -> [{}]'.format(link.name, symlink)) | |
target_path = str(target_dir.joinpath(link.name)) | |
create_symbolic_link(target_path, symlink, is_directory=False) | |
def show_symlink(source): | |
for link in pathlib.Path(source).resolve().iterdir(): | |
symlink = os.readlink(str(link)) | |
print('[{}] -> [{}]'.format(link.name, symlink)) | |
if __name__ == '__main__': | |
source = r'<source>' | |
target = r'<target>' | |
do_move(source, target) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment