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
1
  • 1
    A URL containing a space character is an invalid URL. Every space character in a URL must be percent encoded. 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 search results. Commented Jul 9, 2020 at 16:09

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.