Last active
October 13, 2018 18:29
-
-
Save Vftdan/31c1a2e0ca2bc1a2c37921a1c99eef26 to your computer and use it in GitHub Desktop.
Script for windows that converts IntxLNK files to NTFS junctions
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
#! /C/Windows/System32/cmd.exe /s /c python | |
from subprocess import run, PIPE | |
from sys import argv, executable | |
from os.path import realpath | |
import os | |
try: | |
run(['mklink'], shell=True, stdout=PIPE, stderr=PIPE) | |
except FileNotFoundError: | |
print('Python seems to be launched not from cmd.exe.\nPress enter to restart in cmd....', end='') | |
input() | |
run(['C:/Windows/System32/cmd.exe', '/s', '/c', executable] + argv) | |
argv0, *argv = argv | |
reps = [] | |
files = [] | |
nolog = False | |
nokeep = False | |
logfile = None | |
def proceed_file(f): | |
ff = open(f, 'rb') | |
data = ff.read() | |
ff.close() | |
if data[:8] != b'IntxLNK\x01': | |
if not nolog: | |
logfile.write('{0} - not IntxLNK\r\n'.format(f)) | |
return | |
p = data[8:].decode('utf-16') | |
for r in reps: | |
l = len(r[0]) | |
if p[:l] == r[0]: | |
p = r[1] + p[l:] | |
break | |
if nokeep: | |
os.remove(f) | |
else: | |
os.rename(f, f + '.i2j_old') | |
pr = run(['mklink', '/j', f, realpath(p)], shell=True) | |
rc = pr.returncode | |
if not nolog: | |
logfile.write('{0} - {1}\r\n'.format(f, 'OK' if rc == 0 else 'error ' + str(rc))) | |
while len(argv) != 0: | |
a, *argv = argv | |
if len(a) == 0: | |
continue | |
if a[0] != '-': | |
files.append(a) | |
elif a in ('-n', '--nolog'): | |
nolog = True | |
elif a in ('-N', '--nokeep'): | |
nokeep = True | |
elif a in ('-r', '--replace'): | |
if len(argv) < 2: | |
print('Key `-r\' requires 2 arguments') | |
exit(1) | |
reps.append(argv[:2]) | |
argv = argv[2:] | |
elif a in ('-h', '--help'): | |
print('Usage:\n{0} [{{-n|--nolog}}] [{{-N|--nokeep}}] [{{-r|--replace}} <POSIX mount point> <DOS disk path>] <files...>'.format(argv0)) | |
exit(0) | |
else: | |
print('Unknown key: `{0}\''.format(a)) | |
exit(1) | |
if not nolog: | |
logfile = open('.i2j.log', 'a') | |
try: | |
for f in files: | |
proceed_file(f) | |
except BaseException as e: | |
if logfile: | |
logfile.close() | |
raise e | |
if logfile: | |
logfile.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment