Can I drag and drop files to an .sh script using Bash on Ubuntu on Windows or Windows Subsystem for Linux (WSL)?
I want to pass multiple files as parameters of an .sh script in Windows 10 by simply dragging them over the script file; is this possible?
Windows 10 (v1607 and onward) still doesn't provide this feature by default.
Luckily you can enable it with a registry key (provided below)
The key does of course enable associating .sh script to bash.exe, thus also enables simply double-clicking the script
https://www.codeproject.com/Articles/1214365/Proper-Bash-scripting-on-Windows-Associate-SH-scri (UPDATE: added new icon options for Ubuntu, OpenSUSE and legacy install)
The key also enables the option to run the script in user and elevated mode (the former by double clicking, the latter is a right click option), while an extra (optional) key enables right click > edit with nano
Remember you first have to have install Windows Subsystem for Linux
and set it as default program to open .sh files (the path of the program is C:/Windows/System32/bash.exe)
The main registry key executes the following script
#This makes bash.exe silently execute the command below (whole code)
"%SYSTEMROOT%\System32\bash.exe" -c
#Gets all file paths without expansion/substitution
read -r -d '' path_param <<'EOF'
%*
EOF
read -r -d '' path_exec <<'EOF'
%L
EOF
#Parses all dragged files' paths from Windows paths to unix paths
path_param=$(echo $path_param | tr -d '"' | sed 's/[[:space:]]\([A-Z]:\)/\n\1/g' | sed 's/[A-Z]:/\/mnt\/\L&/g' | tr '\\' '\/'\');
mapfile -t path_param <<< "$path_param";
path_param=("${path_param[@]//:}");
#Same, but with the .sh script path
path_exec=$(echo $path_exec | sed 's/[[:space:]]\([A-Z]:\)/\n\1/g' | sed 's/[A-Z]:/\/mnt\/\L&/g' | tr '\\' '\/'\');
path_exec="${path_exec//:}";
#Sets working directory to the folder where the script is located
cd "${path_exec%\/*}";
#Executes script with or without parameters
if [[ ${path_param[@]} == "" ]];
then "$path_exec";
else "$path_exec" "${path_param[@]/#${path_exec%\/*}\/}";
fi;
#Leaves WSL console open after the .sh script finishes executing
cd ~; bash;