skip to Main Content

On Windows 10, running Debian in WSL2, I have a desktop shortcut to a bash script.
I’d like to drag-and-drop a number of files onto the shortcut, and have the script process them. It works fine if there ARE spaces in the filename, but if there are no spaces, then the path ends up with all slashes removed. For example, C:UsersMefile.txt would become C:UsersMefile.txt

The shortcut which receives the dropped files is C:WindowsSystem32wsl.exe -- /home/myuser/bin/hi.sh

The first two lines of the script "hi.sh"

#!/bin/bash
echo "Received $@"

If I drop these files on it:

C:UsersMeDesktopfile-no-spaces.wav
C:UsersMeDesktopFile With Spaces.wav

It outputs:

Received C:UsersMeDesktopfile-no-spaces.wav C:UsersMeDesktopFile With Spaces.wav

There are utilities like wslpath, but they’re of no use if the initial files are already stripped. Any suggestions how to get windows to not strip out the backslashes?

2

Answers


  1. Chosen as BEST ANSWER

    In the end, I'm going with this. %~1 as opposed to %1 or %1%, is the only method that is consistent with having no quotes. So this loop then adds quotes and collects all the parameters into %params%

    @echo off
    :loop
      set "params=%params% "%~1^""
      shift
    if not "%~1"=="" goto loop
    C:WindowsSystem32wsl.exe -- /home/rinzin/bin/hi.sh %params%
    

  2. Instead of shortcut, create hi.cmd as follows :

    @ECHO OFF
    C:/Windows/System32/wsl.exe -- /home/myuser/bin/hi.sh "%*"
    

    and drag&drop files onto hi.cmd

    Update
    This seems to work, but you need to process each file separately:

    @ECHO OFF
    for %%x in (%*) do (
       C:/Windows/System32/wsl.exe -- /home/myuser/bin/hi.sh "%%~x"
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search