I am looking to basically check if a batch file is present by wildcard *.reg, but I would like it to error if there is more than 1 .reg file.

This is the code I have been trying to use, but my batch is fairly basic.

if "%%i" GTR 1 in (*.reg) == goto Error2
else do (regedit /s %%i)

Fairly sure I have the logic completely backwards! But I put it here so you have a rough idea of what I am trying to do.


2 Answers 2


From a batch script:

set "counter=0"
set "file="
for %%i in (*.reg) do (
    set /A "counter+=1"
    set "file=%%~i"
if %counter% EQU 1 (
    regedit /s "%file%"
) else (
    goto :error

Read more in An A-Z Index of the Windows CMD command line

  • 1
    You should quote the file names in case they contain spaces.
    – Karan
    Jun 19, 2015 at 16:09

If you don't want to create extra variables, here's a simpler method:

@echo off
for /f %%c in ('dir /b *.reg 2^>NUL ^| find /c ".reg"') do if [%%c]==[1] (
    for %%f in (*.reg) do regedit /s "%%f"
) else (
    echo Error!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.