indy-pc
indy-pc

Reputation: 27

Check input for a specific string

I have a script that I am working on that I need some help with. In this script, I want to look at the input variable %1% to see if it contains a specific string. If it does, I want to do one thing. If not, I want it to do something else. The input variable is a UNC path and I want to check and see if it contains the string \\server\share and I don't want it to be case sensitive. Any ideas?

Upvotes: 0

Views: 64

Answers (1)

rojo
rojo

Reputation: 24476

Input variable is %1 or %~1 (with the tilde removing surrounding quotation marks).

There are a couple of ways to check. You can use variable substring substitution:

@echo off & setlocal

set "input=%~1"

if not "%input%"=="%input:specific string=%" (
    rem // input contained the string
) else (
    rem // input did not contain the string
)

The way that works is, %input:specific string=% replaces all instances of specific string with nothing within %input%. So if string before replacement does not equal string after replacement, then the variable contained what you were matching. See this page for full details on substring substitution.

Or you can use find or findstr.

@echo off & setlocal

echo(%1 | find /i "some string" && (
    rem // input contained the string
) || (
    rem // input did not contain the string
)

The substring substitution method is a little more efficient.

Upvotes: 2

Related Questions