说明
使用 mklink 链接原工程文件,减少磁盘空间、加载时间等。
python 版本:
import os, sys, glob, re, shutil
# evn settings
skips = ['Cache', 'Temp']
copies = ['Library']
if len(sys.argv) <= 2:
_, name = os.path.split(sys.argv[0])
print('Usage: ' + name + ' src dst')
exit()
src_path = sys.argv[1]
dst_path = sys.argv[2]
if not os.path.exists(src_path):
print(src_path + ' not exists')
exit()
if os.path.exists(dst_path):
print(dst_path + ' has been exists')
exit()
os.makedirs(dst_path)
pattern = re.compile(r'^(' + src_path + ')')
for src_file in glob.glob(src_path + '/*'):
dst_file = re.sub(pattern, dst_path, src_file)
if src_file == dst_file:
print('wrong path: ' + src_file)
exit()
_, name = os.path.split(src_file)
if name in skips:
print('skip: ' + src_file)
elif name in copies:
shutil.copytree(src_file, dst_file)
print('copy: ' + src_file + ' => ' + dst_file)
else:
if os.path.isdir(src_file):
os.popen('mklink /J ' + dst_file + ' ' + src_file)
else:
os.popen('mklink ' + dst_file + ' ' + src_file)
print('link: ' + src_file + ' => ' + dst_file)
bat 版本:
@echo off
rem evn settings
set skips=Cache Temp
set copies=Library
%~d0 && cd %~dp0
if %errorlevel% neq 0 goto done
set /p src_path=input src path:
if not exist %src_path% (
echo %src_path% not exists
goto done
)
set /p dst_path=input dst path:
if exist %dst_path% (
echo %dst_path% has been exists
goto done
)
md %dst_path%
if %errorlevel% neq 0 goto done
for %%i in (%src_path%\*) do (
call :do_link %%i
)
for /d %%i in (%src_path%\*) do (
call :do_link %%i
)
:do_link
setlocal EnableDelayedExpansion
set opt=
set src_file=%1
set dst_file=!src_file:%src_path%=%dst_path%!
set basename=!src_file:%src_path%\=!
for %%n in (%skips%) do (
if "%%n"=="!basename!" (
set opt=skip
)
)
for %%n in (%copies%) do (
if "%%n"=="!basename!" (
set opt=copy
)
)
if "!opt!"=="skip" (
echo skip: !src_file!
) else if "!opt!"=="copy" (
xcopy /s /e /y /q !src_file!\ !dst_file!\ >nul
if %errorlevel% neq 0 goto done
echo copy: !src_file! ==^> !dst_file!
) else if not "!src_file!"=="" (
if exist !src_file!\nul (
mklink /J !dst_file! !src_file! >nul
if %errorlevel% neq 0 goto done
) else (
mklink !dst_file! !src_file! >nul
if %errorlevel% neq 0 goto done
)
echo link: !src_file! ==^> !dst_file!
)
endlocal
goto:eof
:done
pause >nul
powershell 版本:
# evn settings
$skips = "Cache", "Temp"
$copies = "Library"
cd (Split-Path -Parent $MyInvocation.MyCommand.Definition)
if(-not $?) { exit }
$src_path = Read-Host -Prompt 'input src path'
if(-not (Test-Path $src_path))
{
echo "$src_path not exists"
exit
}
$dst_path = Read-Host -Prompt 'input dst path'
if(Test-Path $dst_path)
{
echo "$dst_path has been exists"
exit
}
New-Item -type dir "$dst_path" > $null
if(-not $?) { exit }
foreach($f in (Get-ChildItem -Path $src_path))
{
$src_file="$src_path\$f"
$dst_file="$dst_path\$f"
if($f.name -in $skips)
{
echo "skip: $src_file"
}
elseif($f.name -in $copies)
{
Copy-Item -Recurse "$src_file" "$dst_file" > $null
if(-not $?) { exit }
echo "copy: $src_file => $dst_file"
}
else
{
if(Test-Path $src_file -PathType Container)
{
New-Item -ItemType Junction -Value "$src_file" -Path "$dst_file" > $null
if(-not $?) { exit }
}
elseif(Test-Path $src_file)
{
New-Item -ItemType SymbolicLink -Value "$src_file" -Path "$dst_file" > $null
if(-not $?) { exit }
}
echo "link: $src_file => $dst_file"
}
}