Skip to main content

How to Check if a String Matches a Pattern (Wildcards) in Batch Script

In scripting, you often need to check if a string conforms to a certain pattern. For example, you might want to see if a filename starts with "log_", ends with ".txt", or contains a specific date format. While some languages use complex regular expressions for this, batch scripting can handle many common cases with a much simpler tool: the wildcard matching of the IF command.

This guide will teach you how to use a clever workaround with a FOR loop to compare a string against a pattern containing wildcards like * and ?. We will also cover a more flexible alternative using the FINDSTR command.

The Challenge: IF Command Doesn't Directly Support Wildcards

A common mistake for beginners is to try using a wildcard directly in an IF statement.

Let's see a script with the error:

@ECHO OFF
SET "filename=log_2023.txt"
REM This does NOT work.
IF "%filename%"=="log_*.txt" (
ECHO It's a log file.
) ELSE (
ECHO It's not a log file.
)
note

This script will always output "It's not a log file" because the IF command performs a literal string comparison. It's looking for a string that is exactly log_*.txt, not a string that matches that pattern.

The Core Method: The FOR Loop Wildcard Trick

The standard "pure-batch" method to solve this is to use a FOR loop. A FOR loop is designed to expand wildcards to find matching files. We can exploit this behavior to check if a single string matches a pattern.

We treat our string as a "filename" and see if a FOR loop with our pattern can "find" it.

  1. Use FOR %%P IN (pattern) to define the wildcard pattern.
  2. Inside the loop, compare the current item (%%P) with our original string.
  3. If they match, it means our string conforms to the pattern. We can then set a flag and exit the loop.
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "MyString=log_2023.txt"
SET "Pattern=log_*.txt"
SET "IsMatch=0"

REM The FOR loop will check our pattern. ECHO "%MyString%" provides the "files" to check against.
FOR /F "delims=" %%A IN ('ECHO "%MyString%"') DO (
FOR %%P IN (%Pattern%) DO (
IF "%%~A"=="%%~P" (
SET "IsMatch=1"
GOTO :Done
)
)
)
:Done

IF !IsMatch! EQU 1 (
ECHO The string matches the pattern.
) ELSE (
ECHO The string does NOT match the pattern.
)

An Alternative Method: Using FINDSTR

The FINDSTR command supports a limited form of regular expressions, which can be used to match patterns. This is often simpler and more powerful than the FOR loop trick.

We ECHO the string and pipe it to FINDSTR, which uses a regular expression that mimics our wildcard pattern. ECHO "MyString" | FINDSTR /R "^log_.*\.txt$"

  • /R: Use Regular expressions.
  • ^: Matches the beginning of the string.
  • .*: The regex equivalent of the * wildcard (matches any character, zero or more times).
  • \.: A literal dot (the \ escapes it).
  • $: Matches the end of the string.

FINDSTR sets %ERRORLEVEL% to 0 if it finds a match.

Basic Example: Checking a Filename Pattern

Let's test both methods. String: log_20231027.txt Pattern: log_*.txt

Method 1: FOR Loop Script

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "MyString=log_2023.txt"
SET "Pattern=log_*.txt"
SET "IsMatch=0"

REM The FOR loop will check our pattern. ECHO "%MyString%" provides the "files" to check against.
FOR /F "delims=" %%A IN ('ECHO "%MyString%"') DO (
FOR %%P IN (%Pattern%) DO (
IF "%%~A"=="%%~P" (
SET "IsMatch=1"
GOTO :Done
)
)
)
:Done

IF !IsMatch! EQU 1 (
ECHO The string matches the pattern.
) ELSE (
ECHO The string does NOT match the pattern.
)

Output:

The string matches the pattern.

Method 2: FINDSTR Script

@ECHO OFF
SET "MyString=log_20231027.txt"

ECHO "%MyString%" | FINDSTR /R "^log_.*\.txt$" > NUL

IF %ERRORLEVEL% EQU 0 (
ECHO The string matches the pattern.
) ELSE (
ECHO The string does NOT match the pattern.
)

Output:

The string matches the pattern.

How the FOR Loop Trick Works

The command FOR /F "delims=" %%A IN ('ECHO "%MyString%"') DO ... is a bit unusual.

  • ECHO "%MyString%": This prints our string to the console.
  • FOR /F ... IN ('...'): This captures the output of the ECHO command, so %%A becomes log_2023.txt.
  • FOR %%P IN (%Pattern%): The inner loop is where the magic happens. It takes the pattern log_*.txt. Normally, it would look for files on the disk matching this. But since it's inside the outer loop's context, it tries to match the pattern against the value provided by ECHO.
  • IF "%%~A"=="%%~P": If log_2023.txt is a valid match for the pattern log_*.txt, the inner loop will "find" it, %%P will become log_2023.txt, and the comparison will be true.

Common Pitfalls and How to Solve Them

Problem: The Match is Case-Sensitive

Both the FOR loop trick and the standard FINDSTR method are case-sensitive. They would not match LOG_2023.txt with the pattern log_*.txt.

Solution: Use the /I switch with FINDSTR. This is FINDSTR's main advantage.

ECHO "LOG_2023.txt" | FINDSTR /I /R "^log_.*\.txt$" > NUL

This command will now correctly find a match.

Problem: Handling Spaces in the String or Pattern

Spaces can cause issues if not handled with careful quoting.

Solution: The FOR loop script provided previously above already uses robust quoting ("%%~A"=="%%~P") and should handle spaces correctly. The FINDSTR method also works well as long as the initial ECHO command quotes the string being tested.

Practical Example: Validating a Product Code Format

This script checks if a user-provided product code matches the required format: PROD- followed by exactly four digits (e.g., PROD-1234).

@ECHO OFF
SET /P "ProductCode=Enter the product code: "

REM We will use FINDSTR for this as it's better with specific digit counts.
REM The regex means: starts with "PROD-", followed by exactly 4 digits, then ends.
ECHO "%ProductCode%" | FINDSTR /R "^PROD-[0-9][0-9][0-9][0-9]$" > NUL

IF %ERRORLEVEL% EQU 0 (
ECHO [SUCCESS] "%ProductCode%" is a valid format.
) ELSE (
ECHO [FAILURE] "%ProductCode%" is not a valid format.
ECHO The required format is PROD-#### (e.g., PROD-1234).
)

Conclusion

While batch script's IF command can't handle wildcards on its own, you can effectively check if a string matches a pattern using two primary methods.

  • The FOR loop trick is a clever, "pure-batch" way to solve the problem for simple * and ? wildcards. It is reliable for case-sensitive matches.
  • The FINDSTR method is the more powerful and flexible tool. It allows for case-insensitive matching (/I) and can handle more complex patterns using regular expressions, making it the recommended choice for most validation tasks.