-
-
Save cat2151/56d5aa1e54f719bdc5efc0ed676ebb29 to your computer and use it in GitHub Desktop.
Windows bat file for creating symbolic link without cmd(run as administrator).
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
@echo off | |
setlocal | |
set invalidUsage=false | |
if "%~1"=="" set invalidUsage=true | |
if "%~2"=="" set invalidUsage=true | |
if "%invalidUsage%"=="true" ( | |
echo Usage: ln-s TARGET LINK_NAME | |
exit /b 1 | |
) | |
set target=%~1 | |
set linkName=%~2 | |
if "%target:~-1,1%"=="\" set target=%target:~0,-1% | |
if "%linkName:~-1,1%"=="\" set linkName=%linkName:~0,-1% | |
if not exist "%target%" ( | |
echo Target^(%target%^) does not found. | |
exit /b 1 | |
) | |
if exist "%linkName%" ( | |
echo Linkname^(%linkName%^) already exists | |
exit /b 1 | |
) | |
if not "%target:~1,1%"==":" set target=%CD%\%target% | |
if not "%linkName:~1,1%"==":" set linkName=%CD%\%linkName% | |
set arguments="'/c,mklink,\"%linkName%\",\"%target%\"'" | |
if exist "%target%\*" set arguments=%arguments%,"/d" | |
powershell.exe -Command Start-Process ^ | |
-FilePath "cmd" ^ | |
-ArgumentList %arguments% ^ | |
-Verb Runas | |
endlocal |
いつの間にか手元の環境で、すべてディレクトリとして扱われるような動作になっていました。
原因は特定できませんでしたが、取り急ぎ、
if exist "%target%"
を
if exist "%target%*"
にすることで対処できました。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
半角スペースを含むファイル名やディレクトリ名を扱えるようにしました。
参考ページ:batファイルからPowerShell引数にspaceのあるパスを渡したい