1

use cURL to get an MD5 value of a string that I have previously passed (% STR%).

This works fine, but as soon as there are spaces in %STR%, the code no longer works.

If I put the URL and %STR% in "" nothing works anymore.

Can you help me?

set STR=anystr
    
set CURL=C:\curl\bin\curl.exe
set MD5URL=https://anyurl.com/md5.php?hash=%STR%
    
for /F %%I in ('%CURL% -s %MD5URL%') do set HASH=%%I
    
echo HASH
pause
Gerhard
  • 22,678
  • 7
  • 27
  • 43
omnibut
  • 11
  • 1
  • 1
    A [URL](https://url.spec.whatwg.org/) containing a space character is an invalid URL. Every space character in a URL must be [percent encoded](https://url.spec.whatwg.org/#percent-encoded-bytes). So a space character in a URL must be replaced with `%20` for a valid URL. By the way: Use your favorite www search engine with `certutil md5` as search term and you can find pages describing how to use `%SystemRoot%\System32\certutil.exe` to get the MD5 sum for a file or a string. See also Stack Overflow [certutil md5](https://stackoverflow.com/search?q=certutil+md5) search results. – Mofi Jul 09 '20 at 16:09

2 Answers2

1

I'm going to assume you're looking for something like this:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

Set "HASH="
Set "CURL=C:\curl\bin\curl.exe"
Set "STR=any str"

Rem replace any space characters in %STR% with %20
SetLocal EnableDelayedExpansion
Set _=!STR: =%%20!
EndLocal & Set "STR=%_%"

Set "MD5URL=https://anyurl.com/md5.php?hash=%STR%"

For /F %%I In ('^""%CURL%" -s "%MD5URL%"^"') Do Set "HASH=%%I"

If Not Defined HASH GoTo :EOF

Echo %HASH%

Pause
Compo
  • 36,585
  • 5
  • 27
  • 39
0

Spaces are separators between commands and parameters within cmd, which is used by batch files. So double quote your strings.

@echo off
set "str=anystr"
    
set "_curlcmd=C:\curl\bin\curl.exe"
set "MD5URL=https://anyurl.com/md5.php?hash=%str%"
    
for /F %%I in ('"%_curlcmd% -s "%MD5URL%"') do set "_HASH=%%I"
    
echo %HASH%
pause
Gerhard
  • 22,678
  • 7
  • 27
  • 43