file counter

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Alogon
Posts: 4
Joined: 06 Apr 2020 19:22

file counter

#1 Post by Alogon » 03 May 2020 10:19

This is offered more as something you might find useful than to ask a question. However, if you see possible improvements, I'm all ears.

I use several batch files to relieve the burden of handling gothic-novel file and directory names in command prompt. Today I encountered a need to ensure that a wildcard spec matches only one file. That particular task is easy to perform, but then I thought, why not extend the program to make it more flexible?
This is the result. I hope that no bugs remain (except that the quota argument could be non-numeric, translating to zero-- not worth the trouble to catch).

Code: Select all

@echo off
setlocal
set QUOTA=1
set CMP=EQU
if /I %1. EQU /G. (
  set CMP=GEQ
  shift
)
if /I %1. EQU /L. (
  set CMP=LEQ
  shift
)
if /I %1. EQU /N. (
  set CMP=NEQ
  shift
)
if /I %1. EQU /S. (
  set SHO=Y
  shift
)
if not %1.==. goto DO
echo I count the files implied in an argument (typically with wild-card characters),
echo compare to quota and return errorlevel 0 if true, else 1.
echo 	Syntax:  %0  [/G /L or /N]  [/S]  filespec  [quota]
echo Defaults:  quota = 1,  comparison op EQU
echo Option /G /L /N sets comparison op GEQ LEQ NEQ respectively.
echo Option /S shows list of matching files before returning 1.
goto end
:DO
if not %2.==. set QUOTA=%2
set CNT=0
for %%f in (%1) do set /A CNT+=1
if /I %CNT% %CMP% %QUOTA% exit /B 0
if not %SHO%.==. (
  dir /B %1
  echo %CNT% files match %1
)
exit /B 1
:END

Post Reply