0

What I'd like to do: Add an entry to a Windows 10 context menu for specific file types (e.g. .mp4) that allows me to search for the file name on a website (in my case, IMDB). I got the entry to show up fine, but file names cut off after any space character.

Question: How do I pass the full file name, including spaces, from the windows registry as a parameter to a batch file?

.reg

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\VLC.mp4\shell\Search IMDB\command]
@="\"C:\\test.bat\" \"%1\""

test.bat

SET a="%~n1"
SET z=https://www.imdb.com/search/title/?title=
SET zz=%z%%a%
start "" %zz%

For a file name like movie.mp4 this works. However, file name cool movie.mp4 will in this case open a search page for "cool" which I'm afraid does not help me.

Any help is greatly appreciated!

Lemodile
  • 3
  • 1

2 Answers2

0

Replace the spaces with + signs.

According to https://ss64.com/nt/syntax-replace.html that should be something like

SET zzz=%zz: =+%
dratenik
  • 2,098
  • 2
  • 5
  • 9
0

The batch file should be:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "Name=%~n1"
setlocal EnableDelayedExpansion
set "Name=!Name:%%=%%25!"
set "Name=!Name: =%%20!"
start "" "https://www.imdb.com/search/title/?title=!Name!"
endlocal
endlocal

First, read my answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? It explains in detail what is the difference between set variable="value" and set "variable=value". The latter is the recommended syntax.

Second, a URL must be percent-encoded. A normal space character must be encoded with %20 in a URL. The batch file above does that. It percent-encodes first also % in name by %25.

Note: The entire URL is not correct percent-encoded if the string assigned to the environment variable Name contains other characters than and % which need to be percent-encoded for a valid URL too.

Third, the character % must be escaped in a batch file with one more % to be interpreted as literal character even within a double quoted argument string.

Mofi
  • 38,783
  • 14
  • 62
  • 115