2

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

Community
  • 1
  • 1

3 Answers3

4

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

  1. how to use FINDSTR and its /R switch. See HELP FINDSTR

  2. how to code a regex for matching and validating URLs. Google for regex tutorial.

    Hint: (http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?

  3. and how to interpret FINDSTR results in a batch file. See HELP FOR

Community
  • 1
  • 1
PA.
  • 28,486
  • 9
  • 71
  • 95
1

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!
eadmaster
  • 1,347
  • 13
  • 23
-2

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.

user207421
  • 305,947
  • 44
  • 307
  • 483