How to check if a URL is valid or not in Windows batch script
I am a newbie in Windows batch scripting. I have a question though.
In Windows batch scripting, 开发者_开发百科how will I know if a variable is a valid URL or not?
Example:
Valid URL:
url=https://stackoverflow.com/questions/ask
Invalid URL:
url=not a valid url
You might use FINDSTR to validate your URL by matching it against a regular expression. See the answers to this Stack Overflow question Regular expressions in findstr
Basically you'll have to understand
how to use FINDSTR and its /R switch. See
HELP FINDSTR
how to code a regex for matching and validating URLs. Google for regex tutorial.
Hint:
(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
and how to interpret FINDSTR results in a batch file. See
HELP FOR
This is simple and should work in most cases:
setlocal enableextensions enabledelayedexpansion
set URL=url to test
if not x%URL:http://=%==x%URL% goto http
if not x%URL:https://=%==x%URL% goto http
if not x%URL:ftp://=%==x%URL% goto ftp
REM else
echo invalid url!
Don't bother. Whatever you use won't be identical to whatever rules the program you're about to run uses. Just let the program decide.
精彩评论