Batch check for *.reg but only if = 1 file

0

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.

Thanks!

Ctrlaltdenied

Posted 2015-06-15T12:35:09.000

Reputation: 13

Answers

1

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

JosefZ

Posted 2015-06-15T12:35:09.000

Reputation: 9 121

1You should quote the file names in case they contain spaces. – Karan – 2015-06-19T16:09:47.323

0

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!
)

Karan

Posted 2015-06-15T12:35:09.000

Reputation: 51 857