From 92e62f6edf2fffd993830a90af0a8c69d45806e8 Mon Sep 17 00:00:00 2001 From: mr-south-guo Date: Sat, 15 Feb 2020 10:58:25 +0800 Subject: [PATCH] + New: `hardlink-runner.cmd` for easy hardlinking multiple runners. --- README.md | 7 ++- hardlink-runner.cmd | 102 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 hardlink-runner.cmd diff --git a/README.md b/README.md index 8fe17b1..e620a83 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,11 @@ A multicall Windows batch file (the _runner_) to provide a transparent method to - An tiny open-source utility to run commands as admin, from [here](http://code.kliu.org/misc/elevate/). - This file is **optional**. You don't need it if you are not planning to use the "Run as admin automatically" feature. (You can always start the _runner_ as admin manually.) - It can be in the same directory as the _runner_, or in the `PATH`. +- `hardlink-runner.cmd` + - A utility script to hardlink one _runner_ to multiple _runners_ for a set of sh-scripts, so that you don't have to do it one-by-one. + - If running it without parameters, it will create _runners_ for all the sh-script in the same directory, by hard-linking from `sh-runner.cmd`. + - Use option `-h` to show help messages for more usage details. + - This file is **optional**. ### Example files @@ -42,7 +47,7 @@ A multicall Windows batch file (the _runner_) to provide a transparent method to ## FAQ -### Will this work with WSL (Windows Subsystem for Linux)? +### Will this work with WSL, Cygwin, MSYS, etc.? I don't know. Maybe you can try it and let me know? diff --git a/hardlink-runner.cmd b/hardlink-runner.cmd new file mode 100644 index 0000000..c2a8d82 --- /dev/null +++ b/hardlink-runner.cmd @@ -0,0 +1,102 @@ +:::: Hardlink runner for multiple sh-scripts +:: +:: See `:showHelp` section at the bottom for details. + +@echo off + +setlocal EnableDelayedExpansion + +set "_SCRIPT_NAME=%~n0" +set "_SCRIPT_DIR=%~dp0" + +:parseArg +if "%1"=="" goto :parseArgEnd + +if "%1"=="-o" ( + set "optOverwrite=yes" + shift +) else if "%1"=="-r" ( + set "RUNNER_TARGET=%2" + shift & shift +) else if "%1"=="-s" ( + set "SH_PATTERN=%2" + shift & shift +) else if "%1"=="-h" ( + goto :showHelp +) else ( + echo "Unknow argument: %1" + exit /b +) +goto :parseArg +:parseArgEnd + +:: Default values +if "%RUNNER_TARGET%"=="" set "RUNNER_TARGET=%_SCRIPT_DIR%sh-runner.cmd" +if "%SH_PATTERN%"=="" set "SH_PATTERN=%_SCRIPT_DIR%*.sh" +if "%optOverwrite%"=="" set "optOverwrite=no" + +echo Hardlink runner +echo =============== +echo. +echo runner target : %RUNNER_TARGET% +echo sh pattern : %SH_PATTERN% +echo overwriting : %optOverwrite% +echo. + +if not exist "%RUNNER_TARGET%" ( + echo "%RUNNER_TARGET%" does not exist. Abort. + exit /b +) + +pause +echo. + +for %%I in (%SH_PATTERN%) do ( + set "skipThisItem=no" + if not exist "%%I" ( + echo No sh-script matching "%SH_PATTERN%" is found. + exit /b + ) + set "RUNNER_LINK=%%~dpnI.cmd" + + if "!RUNNER_LINK!"=="%RUNNER_TARGET%" ( + echo Will not link "%RUNNER_TARGET%" to itself. + set "skipThisItem=yes" + ) + + if exist "!RUNNER_LINK!" ( + if "%optOverwrite%"=="yes" ( + echo File exist, overwriting: "!RUNNER_LINK!" + del "!RUNNER_LINK!" + ) else ( + echo File exist, skipping: "!RUNNER_LINK!" + set "skipThisItem=yes" + ) + ) + + if "!skipThisItem!"=="no" ( + mklink /h "!RUNNER_LINK!" "%RUNNER_TARGET%" + ) +) +exit /b + +:showHelp +echo Usage: +echo %_SCRIPT_NAME% -h +echo %_SCRIPT_NAME% ^[-o^] ^[-r RUNNER_TARGET^] ^[-s SH_PATTERN^] +echo. +echo Create runners for multiple sh-scripts matching SH_PATTERN, by hardlinking from RUNNER_TARGET. +echo. +echo RUNNER_TARGET The runner script to link from. Default is "./sh-runner.cmd" +echo SH_PATTERN The pattern to match for sh-script. Default is "./*.sh" +echo. +echo OPTIONS: +echo. +echo -o Overwrite if the linking runner file exists. +echo -h Show this help. +echo. +echo Example: +echo. +echo %_SCRIPT_NAME% -o -r x:/dir1/sh-runner.cmd -s y:/dir2/*.sh + +endlocal