Search code examples
batch-filewindows-xp-sp3

How to get accurate file count using batch (windows xp)?


Trolled through similar questions and am stuck on my script.

Basically, I need this .bat to check the directory for the number of files with the Lockbox prefix, store the count to a variable, and eventually call an .exe to import each of the files.

Here is what I have so far. My problem is that the test directory has a total of 12 txt files, but I only need the ones with the Lockbox prefix (11 of them):

@echo off

set count=0

for %%a in ('dir /a-d /a-h /a-s "\\ip_of_server\Directory\LockBox*.txt"') do set /a count+=1

@echo File count = %count%
pause

Solution

  • If you don't want to show directories and subdirectories, there is no need to use the "dir" command since FOR won't include them. However, it will likely include any hidden files should they start with LockBox. Just change line 5 to (you may need to use parentheses if spaces are in the path):

    for %%a in (\\ip_of_server\Directory\LockBox*.txt) do set /a count+=1